I need help with setContext() and Codea Crashes.

This code:

function CreateGraphic:formula()
    --[[
    Function is called like this in 'draw':
    
    local img = self:formula()
    sprite(img, (WIDTH+225)/2, HEIGHT/2+50, 700, 450)
    ]]
    
    img = image(450, 300)
    setContext(img)
    fill(127, 127, 127, 255)
    rect(0, 0, 450, 300)
    setContext()
    return img
end

It’s definitely this function, because when commenting it, it won’t crash Codea. :neutral:

The function looks like it calls itself, so it goes round and round until it crashes

What do you mean by “calls itself”?
I am not calling the function anywhere else…

Instead of making another function, I have put the code part in ‘draw’, and it was still crashing:

    -- Show Diagramm
    img = image(450, 300)
    setContext(img)
    fill(127, 127, 127, 255)
    rect(0, 0, 450, 300)
    setContext()
    sprite(img, (WIDTH+225)/2, HEIGHT/2+50, 700, 450)

Still crashes :confused:

Why are you using setContext every time you draw?

Okey, Fixed, creating new images 60 times a miliseconds is crashing Codea.

Just made it being elements…

Hi,

Try putting your image generation in setup then just sprite the image in draw:


displayMode(OVERLAY)
function setup()

    img = image(450, 300)
    setContext(img)
    fill(127, 127, 127, 255)
    rect(0, 0, 449, 299)
    setContext()

end

function draw()

    background(50,50,50,255)

    sprite(img, WIDTH/2, HEIGHT/2)

end

Make sure the size of your img is the same size as the rectangle.

Bri_G

But it should be updated automatically…