Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Stack Traceback NIL?

edited May 8 in General Posts: 23

I am following this tutorial:

After gathering the code for the interlude 5, mentioned in the guide, now I am receiving the following error:

Main: 39: attempt to call a nil
value (global ‘drawButton’)
Stack traceback:
Main: 39: in function ‘draw’

I have attached the images of both the error and my code. I didn’t type it, I just copied and pasted what was on the site.

I also noticed someone else in the comments, below the lesson, has same issue I describe above. They mentioned it had something to do with spelling. Since I am new to all of this, I have no idea what would be wrong with the spelling, since I am copying and pasting the code.

Can anybody guide me on what I am supposed to do here?
I even checked the twinkle class (maybe error is in there?), but I have no idea what I am looking for.

Here are BOTH of my codes (Main & Twinkle Class):


function setup()

    -- Keep track of Game State

    stateSplash = 0
    stateMenu = 1
    stateRun = 2
    stateWon = 3
    stateLost = 4

    gameState = stateSplash

    -- Create our menu button

    button = Button("    Start    ")
    button.action = function() buttonPressed() end

    -- Create the splash screen

    splashScreen = SplashScreen("Spacewar!", 10)

    -- Create the twinkling star background

    twinkleBackground = Twinkle(100)


function draw()

    -- Set background to black

    background(0, 0, 0)

    -- Draw the appropriate screen based on gameState

    if gameState == stateSplash then
        splashScreen: draw()
    elseif gameState == stateMenu then
    elseif gameState == stateRun then
        twinkleBackground: draw()


function buttonPressed()

    -- If the menu button is pressed we transition to the next state.

    gameState = stateRun


function fadeAnimationDone()

-- Call back function for Fader complete
-- Splash screen done, transition to menu state

gameState = stateMenu


Twinkle = class()

function Twinkle:init(numberOfStars)
    -- you can accept and set parameters here
    st = {}
    ns = numberOfStars -- number of stars
    for i = 1,ns do
        if math.random(2) == 1 then g = .25 else g = -.25 end
        i = 5 + (math.random(39)/4)
        st[i] = {g=g,l=l,x=math.random(WIDTH),y=math.random(HEIGHT)}

function Twinkle:draw()
    -- Codea does not automatically call this method
    -- background(20, 46, 181, 255), this is Ipad41001 original background colour
    background(0, 0, 0)
    stroke(255, 255, 255, 255)
    fill(255, 255, 255, 255)
    for i = 1,ns do
        if st[i].l <= 5 and math.random(2) == 1 then st[i].g = .25
        elseif st[i].l >= 15 and math.random(2) == 1 then st[i].g = -.25 end
        st[i].l = st[i].l + st[i].g 
        ll = (15 - st[i].l)/2


  • SimeonSimeon Admin Mod
    Posts: 4,888

    Ah I think you might need the code from the previous tutorial? There is a note in the tutorial you linked stating

    The drawButton() and touched(touch) functions are unchanged from the previous tutorial. Modify buttonPressed so that it looks like this:

  • edited May 8 Posts: 23

    Ah,, damn it. It’s me causing this unnecessary drama.

    4 hours and I couldn’t detect that? Man, that’s super embarrassing.

    What you said makes perfect sense. I did overwrite the existing code, by copying and pasting the new code he demonstrated in this current tutorial I am working on.

    Wow @Simeon I truly apologize for pulling you off the work you are doing with Codea, The Shader and your family (saw the images you put up on twitter) beautiful child. I truly apologize man.

    I will try better before you pulling you in again. Thank you.

  • SimeonSimeon Admin Mod
    Posts: 4,888

    @tactfulgamer no problem! This place is here so you can ask questions

  • edited May 9 Posts: 23

    Image attached. Wow! As a noob, this stuff really plays the minds perspective, goes deep into those channels, so many small things to stay on top of.

    What a exercise for the mental synapses, keep those electric impulses moving! Image of resolved attached.

    Original Post (Want to see my mistakes with my future improved self - will leave here)
    Yep tried it. Still same issue. I will try to find the answer then share. At least I am learning “debugging” I think it’s called.

    I’m down. New energy has entered me to fight this problem.

  • SimeonSimeon Admin Mod
    Posts: 4,888

    Awesome! Well done

Sign In or Register to comment.