State variables

I’m new to both lua and coding. It’s something I’ve wanted to give a go for a while. My question is pretty basic but I can’t for the life of me work out why what I’m trying won’t work.

I need my program to be able to keep track of different stages, and do different things during those stages, so I thought I would create a state variable and run various bits of the draw() process within an if statement to delineate those stages. But for some reason the if statement completely screws everything up. I’ve tried doing it with a if statement, a while loop and a repeat loop. All mess up the program. It’s just meant to put up some text, slowly fade to a background colour, then terminate so I can introduce the next stage. But the if, while or repeat statements seem to make everything go by instantly (defeating the purpose of a slow fade!).

As you can tell, I’m learning by doing, but I’ve been through quite a few tutorials which haven’t given me an answer. Am also reading Ignatz’s e-book.

--# Main

-- Use this function to perform your initial setup

function setup()
    fader = Fader(3, 30, 60, 30)
    playername = nil
    stage = "start"

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
    while stage == "start" do

        fader.fading = true
        if fader.finished ~= true then
            stage = "Getname"
--# Fader
Fader = class()

function Fader:init(fadeSpeed, r, g, b)
    -- you can accept and set parameters here
    self.r = r
    self.g = g
    self.b = b 
    self.alpha = 0
    self.fadeSpeed = fadeSpeed or 1
    self.finished = false

function Fader:draw()
-- If it is time to fade then...

    if self.fading then

-- No smooth and no stroke are used to ensure we get an acceptable frame rate


-- fill is set to black and to the current alpha. Initially alpha will be zero

        fill(self.r, self.g, self.b, self.alpha)
        --change that color if you want to fade to a color other than black
        -- draw rectangle
        self.alpha = self.alpha + self.fadeSpeed
        -- The maximum alpha value is 255

        if self.alpha >= 255 then 
            self.alpha = 255 
            self.fading = false --fade complete
            self.finished = true

@epicurus101 - the mistake is very minor

while stage == "start" do should be if stage == "start" then

because you only want to run this loop once for each frame you draw.

Instead, a while loop will keep looping over and over (without ever leaving the draw function) until the fader finishes.

This prevents anything from being drawn, because you only get to see the result on screen when Codea gets to the end of the draw function, and with a while statement, that doesn’t happen until all the fading is done.

Still, making mistakes is a good way to learn, don’t feel bad. We all do that. :stuck_out_tongue:

I could swear that that’s one of the many things I had tried, but I just tried it again and sure enough it works fine! Anyway, thanks, the sheer amount of free material you’ve put out there for the community is astonishing and hugely appreciated.

If it makes you feel better, I didn’t see it straight away, either.