I made a game for my son that works and I want to take it a bit further so I am trying to get it to work with storyboard so I can add other levels. This is the first program I’ve ever written so I’m new to this (but love it).
The game is simple- letters of the alphabet bounce around the screen and when a letter is touched audio plays the letter name and it disappears. I decided to try and get storyboard to work with just two letters first. I have main.lua, menu.lua and game.lua. The menu launches fine but when I touch the play button object I get the following error: ?:0: attempt to call method ‘getOrCreateTable’ (a nil value)
I tested the play buttons gotoScene with a very simple test.lua file that just changes the background and it worked so I’m pretty sure it’s an issue with my game.lua scene but I can’t figure out the problem. The game and menu scene code is below and I would appreciate any help/advice:
Game.lua:
[lua]local storyboard = require “storyboard”
local scene = storyboard.newScene()
local physics = require(“physics”);
local gameTimer;
local Random = math.random;
function scene:createScene(event)
local group = self.view
local background = display.newImage(“images/clouds.png”);
group:insert(background)
– Load audio files
local music = audio.loadStream(“sounds/music.mp3”);
group:insert(music)
local voicea = audio.loadSound(“sounds/a.wav”);
group:insert(voicea)
local voiceb = audio.loadSound(“sounds/b.wav”);
group:insert(voiceb)
local alphabet = 2;
group:insert(alphabet)
–Game over sequence- when no more letters exist then game over
local function gameOver(condition)
if (condition == “winner”) then
storyboard.gotoScene(“menu”, “fade”, 200)
end
end
– Letters removed after touched
local function removeLettera(obj)
obj:removeSelf();
--lettera = lettera - 1;
alphabet = alphabet - 1;
if (alphabet == 0) then
gameOver(“winner”)
end
end
local function removeLetterb(obj)
obj:removeSelf();
--letterb = letterb - 1;
alphabet = alphabet - 1;
if (alphabet == 0) then
gameOver(“winner”)
end
end
– 2. Set gravity to be inverted
physics.start()
physics.setGravity(0, -0.7)
physics.setDrawMode(“normal”)
–Create boundries to keep letters from floating away
local leftWall = display.newRect (0, 0, 1, display.contentHeight);
local rightWall = display.newRect (display.contentWidth, 0, 1, display.contentHeight);
local ceiling = display.newRect (0, 0, display.contentWidth, 1);
local ground = display.newRect (0, display.contentHeight, display.contentWidth, 1);
physics.addBody (leftWall, “static”, { bounce = 0.2 } );
physics.addBody (rightWall, “static”, { bounce = 0.3 } );
physics.addBody (ceiling, “static”, { bounce = 1 } );
physics.addBody (ground, “static”, { bounce = 0.1 } );
group:insert(leftWall)
group:insert(rightWall)
group:insert(ceiling)
group:insert(ground)
–Start of game
local function startGame()
– 3. Create a letters
myLettera = display.newImageRect(“images/LetterA.jpg”, 50, 50);
myLetterb = display.newImageRect(“images/LetterB.jpg”, 50, 50);
myLettera:setReferencePoint(display.CenterReferencePoint);
myLetterb:setReferencePoint(display.CenterReferencePoint);
myLettera.x = Random(50, _W-50);
myLetterb.x = Random(50, _W-50);
myLettera.y = (_H-80);
myLetterb.y = (_H-10);
physics.addBody(myLettera, “dynamic”, {density=0.1, friction=0.0, bounce=0.9, radius=20});
physics.addBody(myLetterb, “dynamic”, {density=0.1, friction=0.0, bounce=0.9, radius=20});
group:insert(myLettera)
group:insert(myLetterb)
–Touch functions for letters
function myLettera:touch(e)
if (e.phase == “ended”) then
audio.play(voicea);
removeLettera(self);
end
end
function myLetterb:touch(e)
if (e.phase == “ended”) then
audio.play(voiceb);
removeLetterb(self);
end
end
myLettera:addEventListener(“touch”, myLettera);
myLetterb:addEventListener(“touch”, myLetterb);
end
gameTimer = timer.performWithDelay(20, startGame());
end
function scene:enterScene(event)
local group = self.view
end
function scene:exitScene(event)
local group = self.view
end
function scene:destroyScene(event)
local group = self.view
end
scene.addEventListener(“createScene”)
scene.addEventListener(“enterScene”)
scene.addEventListener(“exitScene”)
scene.addEventListener(“destroyScene”)
return scene[/lua]
Menu.lua:
[lua]
local storyboard = require (“storyboard”)
local scene = storyboard.newScene()
local widget = require “widget”
function scene:createScene(event)
local group = self.view
local background = display.newImage(“images/clouds.png”);
group:insert(background);
local function onPress()
storyboard.gotoScene(“game”, “fade”, 200)
return true;
end
local playBtn = widget.newButton{
label=“Play Now”,
onRelease = onPress
}
playBtn.x = _W /2
playBtn.y = _H - 80
group:insert( playBtn )
end
function scene:enterScene(event)
local group = self.view
if(storyboard.getPrevious() ~= nil) then
storyboard.purgeScene(storyboard.getPrevious())
storyboard.removeScene(storyboard.getPrevious())
end
end
function scene:exitScene(event)
local group = self.view
end
function scene:destroyScene(event)
local group = self.view
end
scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene)
scene:addEventListener( “destroyScene”, scene )
return scene [/lua]