Hey guys i’m really stuck on something: I cannot seem to get the game to go into a diffrent scene fromt he game itself.
I have a menu where i can go around the scenes but as soon as i enter the game scene i cant go into the menu or any other scene.
I made a check in my winscene, if the winscene gets called it prints WINSCREEN in the debug window, as soon as the condition is met inside the game this message does show in the debug window but the application itself wont go to the winscreen.
This is the code I have so far:
[lua]local storyboard = require(“storyboard”)
local scene = storyboard.newScene()
–hide status bar from the beginning
display.setStatusBar( display.HiddenStatusBar)
–start physics
local physics = require(“physics”)
physics.start()
local gameUI = require(“gameUI”)
require “sprite”
local Feeding = false
local obstacleColl = false
local Win = false
local Food = {}
local obstacle = {}
local score = 0
_W = display.contentWidth; – get the width of the screen
_H = display.contentHeight; – get the Height of the screen
displayGroupBG = display.newGroup()
displayGroupAnimals = display.newGroup()
displayGroupFood = display.newGroup()
displayGroupGUI = display.newGroup()
displayGroupBad = display.newGroup()
local Background = display.newImage (displayGroupBG, “Assets/background.png”, true)
Background.x = _W/2; Background.y = _H/2;
local myText = {}
local TimeLeft = {}
–init animal png’s
–Animal 1 = Gorilla
–Animal 2 = Giraffe
–Animal 3 = Penguin
–Animal 4 = Snake
–Animal 5 = Tiger
for i=1,5,1 do
local Animal
if i == 1 then
Animal = display.newImage(displayGroupAnimals, “Assets/Gorilla.png”, true)
Animal.type = “Gorilla”
end
if i == 2 then
Animal = display.newImage(displayGroupAnimals, “Assets/Giraffe.png”, true)
Animal.type = “Giraffe”
end
if i == 3 then
Animal = display.newImage(displayGroupAnimals, “Assets/Penguin.png”, true)
Animal.type = “Penguin”
end
if i == 4 then
Animal = display.newImage(displayGroupAnimals, “Assets/Snake.png”, true)
Animal.type = “Snake”
end
if i == 5 then
Animal = display.newImage(displayGroupAnimals, “Assets/Tiger.png”, true)
Animal.type = “Tiger”
end
if i < 4 then
Animal.x = 75 * i
Animal.y = 400
end
if i > 3 then
Animal.x = 75 * i - 200
Animal.y = 475
end
physics.addBody ( Animal, “static”, { friction=0.5, bounce= 0.3} )
Animal.isSensor = true
end
local function Boundries()
local size = 3
local WallBottom
WallBottom = display.newRect(displayGroupBG, 0,0,_W,size)
WallBottom:setFillColor(0,0,255,255)
physics.addBody ( WallBottom, “static”, { friction=0.5, bounce= 0.1} )
WallBottom.x = _W /2
WallBottom.y = 530
local WallLeft
WallLeft = display.newRect(displayGroupBG, 0,0,size,display.contentHeight+100)
WallLeft:setFillColor(0,0,255,255)
physics.addBody ( WallLeft, “static”, { friction=0.5, bounce= 0.1} )
--WallLeft.x = -11
WallLeft.y = _H / 2
local WallRight
WallRight = display.newRect(displayGroupBG, 0,0,size,display.contentHeight+100)
WallRight:setFillColor(0,0,255,255)
physics.addBody ( WallRight, “static”, { friction=0.5, bounce= 0.1} )
WallRight.x = _W
WallRight.y = _H / 2
local WallStart
WallStart = display.newRect(displayGroupBG, 0,0,100,size)
WallStart:setFillColor(0,0,255,255)
physics.addBody ( WallStart, “static”, { friction=0.5, bounce= 0.1} )
WallStart.x = _W / 2
WallStart.y = 100
end
Boundries()
local function FoodCollision(self, event)
if event.phase == “began” then
if event.target.type == “Banana” and event.other.type == “Gorilla” then
print(“Gorilla X Banana”)
Feeding = true
print(Feeding)
end
if event.target.type == “Leaves” and event.other.type == “Giraffe” then
print(“Giraffe X Leaves”)
Feeding = true
print(Feeding)
end
if event.target.type == “Fish” and event.other.type == “Penguin” then
print(“Penguin X Fish”)
Feeding = true
print(Feeding)
end
if event.target.type == “Mouse” and event.other.type == “Snake” then
print(“Snake X Mouse”)
Feeding = true
print(Feeding)
end
if event.target.type == “Meat” and event.other.type == “Tiger” then
print(“Tiger X Meat”)
Feeding = true
print(Feeding)
end
end
end;
local obstacleArray = {} – global obstacle array
function Obstacles()
local numObstacles = 3
for i=1,numObstacles,1 do
--Animal = display.newImage(displayGroupAnimals, “Assets/Gorilla.png”, true)
obstacleArray[i] = display.newImage(displayGroupBad, “Assets/obstacle.png”, true)
obstacleArray[i].x = 400
obstacleArray[i].y = math.random(125,400)
obstacleArray[i].speed = math.random(4,8)
obstacleArray[i].initY = obstacleArray[i].y
obstacleArray[i].type = “obstacle”
physics.addBody(obstacleArray[i], “static”, {density=0.1, bounce=0.1, friction=.2, radius = 12})
end
end
Obstacles()
function moveObstacles()
for i=1,#obstacleArray,1 do
if obstacleArray[i].x > -15 then
if obstacleArray[i].y > 800 then
obstacleArray[i].y = 0
obstacleArray[i].x = math.random(100,320)
obstacleArray[i].speed = math.random(1,4)
else
obstacleArray[i].x = obstacleArray[i].x - obstacleArray[i].speed
--print(“obstacles should move”)
end
else
obstacleArray[i].y = math.random(110,400)
obstacleArray[i].x = 400
end
end
end
local myText = display.newText(“Your Score:”, 100, -30, native.systemFont, 24)
local function CreateFood()
– load in food types
local RFood = math.random(5)
--RFood 1 = Banana
--RFood 2 = Leaves
--RFood 3 = Fish
--RFood 4 = Mouse
--RFood 5 = Meat
if RFood == 1 then
Food = display.newImage (displayGroupFood, “Assets/Banana.png”, true)
Food.type = “Banana”
end
if RFood == 2 then
Food = display.newImage (displayGroupFood, “Assets/Leaves.png”, true)
Food.type = “Leaves”
end
if RFood == 3 then
Food = display.newImage (displayGroupFood, “Assets/Fish.png”, true)
Food.type = “Fish”
end
if RFood == 4 then
Food = display.newImage (displayGroupFood, “Assets/Mouse.png”, true)
Food.type = “Mouse”
end
if RFood == 5 then
Food = display.newImage (displayGroupFood, “Assets/Meat.png”, true)
Food.type = “Meat”
end
Food.x = _W / 2
Food.y = 25
function Food:touch( event )
if event.phase == “began” then
– begin focus
display.getCurrentStage():setFocus( self, event.id )
self.isFocus = true
self.markX = self.x
self.markY = self.y
elseif self.isFocus then
if event.phase == “moved” then
– drag touch object
self.x = event.x - event.xStart + self.markX
self.y = event.y - event.yStart + self.markY
elseif event.phase == “ended” or event.phase == “cancelled” then
– end focus
display.getCurrentStage():setFocus( self, nil )
self.isFocus = false
end
end
– event handled
return true
end
Food:addEventListener( “touch”, gameUI.dragBody )
local collisionFunc = function(self, event)
if event.phase == “began” then
--Banana X Gorilla
if event.target.type == “Banana” and event.other.type == “Gorilla” then
print(“Gorilla X Banana”)
Feeding = true
print(Feeding)
end
--Leaves X Giraffe
if event.target.type == “Leaves” and event.other.type == “Giraffe” then
print(“Giraffe X Leaves”)
Feeding = true
print(Feeding)
end
--Fish X Penguin
if event.target.type == “Fish” and event.other.type == “Penguin” then
print(“Penguin X Fish”)
Feeding = true
print(Feeding)
end
--Mouse X Snake
if event.target.type == “Mouse” and event.other.type == “Snake” then
print(“Snake X Mouse”)
Feeding = true
print(Feeding)
end
--Meat X Tiger
if event.target.type == “Meat” and event.other.type == “Tiger” then
print(“Tiger X Meat”)
Feeding = true
print(Feeding)
end
--Banana X obstacle
if event.target.type == “Banana” and event.other.type == “obstacle” then
obstacleColl = true
print(“obstacleCollilision”)
end
--Leaves X obstacle
if event.target.type == “Leaves” and event.other.type == “obstacle” then
obstacleColl = true
print(“obstacleCollilision”)
end
--Fish X obstacle
if event.target.type == “Fish” and event.other.type == “obstacle” then
obstacleColl = true
print(“obstacleCollilision”)
end
--Mouse X obstacle
if event.target.type == “Mouse” and event.other.type == “obstacle” then
obstacleColl = true
print(“obstacleCollilision”)
end
--Meat X obstacle
if event.target.type == “Meat” and event.other.type == “obstacle” then
obstacleColl = true
print(“obstacleCollilision”)
end
end
if obstacleColl == true then
timer.performWithDelay(10, CreateFood)
event.target:removeSelf()
event.target = nil;
obstacleColl = false
end
local WinCheck = function()
if Win == true then
print(“level won”)
storyboard.gotoScene(“WinScreen”, “fade”, 400)
end
end
--print(“feeding checked”)
if Feeding == true then
--print(“feeding checked true”)
timer.performWithDelay( 10, CreateFood )
--print(Feeding)
--print(score)
score = score + 100
myText:setTextColor(0,0,0)
myText.text = "Your score: "…score
Feeding = false
event.target:removeSelf()
event.target = nil;
if score > 200 then
Win = true
timer.performWithDelay(2500, WinCheck, 1)
end
--[[local FeedSound = function()
local audioLoaded = audio.loadStream(“Assets/FeedingSound.wav”)
--play adio
local channel = audio.play(audioLoaded)
return channel
end
local channel = FeedSound()–]]
else
print(“feeding checked false”)
end
end;
Food.collision = collisionFunc
Food:addEventListener(“collision”, Food)
physics.addBody ( Food, “dynamic”, { friction=0.5, bounce =0.3} )
return Food
end
CreateFood()
local lastTimeChecked = 0 – Global timekeeping
local function onEnterFrame(event)
local currentTime = os.time()
--print(" – checking objects.")
lastTimeChecked = currentTime
moveObstacles()
end
– Now lets keep regularly checking if objects are offscreen
Runtime:addEventListener(“enterFrame”, onEnterFrame)
print(" main() done")
function GameOver()
--game over here
print(“gameover”)
end
function displayScore()
myText:setTextColor(0,0,0)
myText.text = "Your score: "…score
end
local timeLimit = 50
timeLeft = display.newText(timeLimit, 10, -30, native.systemFontBold, 24)
timeLeft:setTextColor(0,0,0)
local function timerDown()
timeLimit = timeLimit-1
timeLeft.text = timeLimit
if(timeLimit==0)then
print(“Time Out”) – or do your code for time out
GameOver()
end
end
timer.performWithDelay(1000,timerDown,timeLimit)
return scene
[/lua]