I am following this tutorial:
http://codeatuts.blogspot.com/2012/07/tutorial-5-finite-state-machines.html
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):
MAIN CODE:
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)
end
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
drawButton()
elseif gameState == stateRun then
twinkleBackground: draw()
end
end
function buttonPressed()
-- If the menu button is pressed we transition to the next state.
gameState = stateRun
end
function fadeAnimationDone()
-- Call back function for Fader complete
-- Splash screen done, transition to menu state
gameState = stateMenu
end
TWINKLE CLASS CODE
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)}
end
end
function Twinkle:draw()
-- Codea does not automatically call this method
pushStyle()
rect(0,0,1,1)
-- background(20, 46, 181, 255), this is Ipad41001 original background colour
background(0, 0, 0)
strokeWidth(3)
lineCapMode(SQUARE)
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
line(st[i].x-st[i].l,st[i].y,st[i].x+st[i].l,st[i].y)
line(st[i].x,st[i].y-st[i].l,st[i].x,st[i].y+st[i].l)
line(st[i].x-ll,st[i].y-ll,st[i].x+ll,st[i].y+ll)
line(st[i].x+ll,st[i].y-ll,st[i].x-ll,st[i].y+ll)
ellipse(st[i].x,st[i].y,6,6)
end
popStyle()
end