Strange bug. :(

Hey guys. My problem is that, well this code does work. But when I click the main trigger I see the newly rendered lane Sprite, but the problem is, the old button/menu screen is still being rendered! Why is this?

function setup()
    Aiming = false
    Frame = "Menu"
    pinInfo = {}
    menuDraw = {
        Text = {"text",WIDTH / 2 + 100,680,color(27, 255, 0, 255),"Bowling!!",40};
        Rect = {"rect",WIDTH / 2,500,color(70, 255, 0, 255),200,50,4,color(0, 47, 255, 255)};
    }
    menuTriggers = {
        playGame = {WIDTH / 2, WIDTH / 2 + 200};
    }
    gameDraw = {
        Sprite = {"sprite","Project:lane",50,50,50,50};
    }
    X,Y = 0,0
end

function drawFrame(data)
    local frameData
    frameData = (data == "Menu" and menuDraw or data == "Game" and gameDraw)
    for i,v in pairs(frameData) do
        local Type = v[1]
        if Type == "text" then
            fontSize(v[6])
            fill(v[4])
            text(v[5],v[2],v[3])
        elseif Type == "rect" then
            strokeWidth(v[7])
            stroke(v[8])
            fill(v[4])
            rect(v[2],v[3],v[5],v[6])
        elseif Type == "sprite" then
            sprite(v[2],v[3],v[4],v[5],v[6])
        end
    end
end

function touched(t)
    X = t.x
    Y = t.y
end

function draw()
    drawFrame(Frame)
    if X > menuTriggers.playGame[1] and X < menuTriggers.playGame[2] and Y > 500 and Y < 500 + 50  and Frame == "Menu" then
        Frame = "Game"
    end    
end

@Coder1500 I’d say 100% that this isn’t a bug, but an error in your coding. One thing you should know is that any Sprite that isn’t provided with Codea can’t be seen by anyone who uses this code. So when we run your code, we don’t see the sprite Project:lane.

Here’s a simple state code example that I think you’re trying to do. Also, when you show code in the forum, put 3 ~'s on a line before and after your code. I added them to your code above.

supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)

function setup()
    state=menu
end

function draw()
    background(40, 40, 50)
    state()
end

function menu()
    -- menu code goes here
    fill(15, 255, 0, 255)
    fontSize(40)
    text("Tap screen for Bowling",500,400)
end

function playGame()
    -- game code goes here
    fontSize(20)
    text("Tap screen for menu",WIDTH/2,50)
    stroke(255)
    strokeWidth(2)
    line(300,100,400,700)
    line(700,100,600,700)
    line(300,100,700,100)
    line(400,700,600,700)
end

function touched(t)
    if t.state==BEGAN then
        if state==menu then
            state=playGame
        elseif state==playGame then
            -- whatever play game touches goes here
            state=menu
        end
    end
end

I’m surprised anything draws at all

In your drawFrame function, you set frameData equal to true or false, then try to do a pairs loop on it - but frameData is not a table.

Also, the Boolean tests you use to set frameData are ambiguous. I would put brackets round like this to be sure it does what you want, ie

(data == "Menu" and menuDraw) or (data == "Game" and gameDraw)

@Coder1500 Add the line background(0) after function draw().

function draw()
    background(0)
    drawFrame(Frame)
    if X > menuTriggers.playGame[1] and X < menuTriggers.playGame[2] and 
            Y > 500 and Y < 500 + 50 and Frame == "Menu" then
        Frame = "Game"
    end
end

@Coder1500 I don’t see you call background() anywhere, put it at the start of draw so previous frames are overwritten, like

function draw()
        background()
        drawFrame(Frame)
        -- rest of code

Ah thanks everyone. Refreshing the background was what I needed. I guess I forgot :stuck_out_tongue: