Hi Everyone,
When I go from my scene one to scene two with a button, using gotoScene, the speed is 10 times accelerated in scene 2 when it is supposed to be the exact same as scene 1. I am not sure what the error is. Here is my scene 1 entire code:
[code]
–
– level1.lua
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
local widget = require “widget”
– forward declarations and other locals
local LevelComplete
– ‘onRelease’ event listener for MenuBtn
local function onLevelCompleteRelease()
storyboard.gotoScene(“level2”)
return true – indicates successful touch
end
local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5
– BEGINNING OF YOUR IMPLEMENTATION
– NOTE: Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.
local physics = require(“physics”)
physics.start()
– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view
– create a grey rectangle as the backdrop
local background = display.newImage(“blueback.png”)
local backgroundnear2 = display.newImage(“trees.gif”)
backgroundnear2.x = 760
backgroundnear2.y = 220
local clouds = display.newImage(“clouds1.gif”)
clouds.x = 200
clouds.y = 80
local plane = display.newImage(“plane.gif”)
plane.x = 300
plane.y = 55
local skyline = display.newImage(“blueskyline.gif”)
skyline.x = 220
skyline.y = 195
–
LevelComplete = widget.newButton{
label=“Next Level”,
labelColor = { default={30}, over={128} },
default=“macaqua.gif”,
font = “MarkerFelt-Thin”,
fontSize = 17,
over=“button-over.png”,
width=100, height=50,
onRelease = onLevelCompleteRelease – event listener function
}
LevelComplete:setReferencePoint( display.CenterReferencePoint )
LevelComplete.x = display.contentWidth*.9
LevelComplete.y = display.contentHeight - 500
group:insert(LevelComplete)
– audio files
local shot = audio.loadSound(‘shot.mp3’)
local jump = audio.loadSound(‘jump.mp3’)
local gameover = audio.loadSound(‘gameover.mp3’)
— ADD HAWK INSTEAD OF GHOST
– create our animated player
local sprite = require(“sprite”)
local inEvent = 0
local eventRun = 0
– create scoring system
local score = 0
local scoreText = display.newText("Score: " … score, 0, 0, “MarkerFelt-Thin”, 50)
fontColor = 22
scoreText:setReferencePoint(display.CenterLeftReferencePoint)
scoreText.x = 0
scoreText.y = 30
–
local gameOver = display.newImage(“gameover.png”)
gameOver.name = “gameOver”
gameOver.x = 0
gameOver.y = 500
–
local pause = display.newImage(“pausebutton.gif”)
pause.name = “pause”
pause.x = 450
pause.y = 30
–
local play = display.newImage(“play-button.gif”)
play.name = “play”
play.x = 0
play.y = 500
–
local nextlevel = display.newImage(“levelbutton.png”)
nextlevel.name = “nextlevel”
nextlevel.x = 0
nextlevel.y = 500
– create group to hold our blocks
local blocks = display.newGroup()
local player = display.newGroup()
local screen = display.newGroup()
local ghosts = display.newGroup()
local spikes = display.newGroup()
local blasts = display.newGroup()
local stumps = display.newGroup()
local groundMin = 420
local groundMax = 300
local groundLevel = groundMin
local speed = 10;
–create ghosts and set their position to be off-screen
for a = 1, 3, 1 do
ghost = display.newImage(“eagle.gif”)
ghost.name = (“ghost” … a)
ghost.id = a
ghost.x = 800
ghost.y = 600
ghost.speed = 0
–variable used to determine if they are in play or not
ghost.isAlive = false
–make the ghosts transparent and more… ghostlike!
ghosts:insert(ghost)
end
–create spikes
for a = 1, 3, 1 do
spike = display.newImage(“cactus.gif”)
spike.name = (“spike” … a)
spike.id = a
spike.x = 900
spike.y = 500
spike.isAlive = false
spikes:insert(spike)
end
–create blasts
for a=1, 5, 1 do
blast = display.newImage(“bullet.png”)
blast.name = (“blast” … a)
blast.id = a
blast.x = 800
blast.y = 500
blast.isAlive = false
blasts:insert(blast)
end
–create tree stumps
for a=1, 4, 1 do
stump = display.newImage(“tntblock.gif”)
stump.name = (“stump” … a)
stump.id = a
stump.x = 800
stump.y = 100
stump.isAlive = false
stumps:insert(stump)
end
– GROUND
for a = 1, 9, 1 do
isDone = false
numGen = math.random(1)
local newBlock
print (numGen)
if(numGen == 1 and isDone == false) then
newBlock = display.newImage(“groundconc.png”)
isDone = true
end
if(numGen == 2 and isDone == false) then
newBlock = display.newImage(“concrete.png”)
isDone = true
end
if(numGen == 3 and isDone == false) then
newBlock = display.newImage(“ground3.png”)
isDone = true
end
if(isDone == false) then
newBlock = display.newImage(“conrete.png”)
end
newBlock.name = (“block” … a)
newBlock.id = a
newBlock.x = (a * 79) - 10
newBlock.y = groundLevel
blocks:insert(newBlock)
end
–create our sprite sheet
local spriteSheet = sprite.newSpriteSheet(“motorbiker.gif”, 98, 97)
local monsterSet = sprite.newSpriteSet(spriteSheet, 1, 1)
sprite.add(monsterSet, “running”, 1, 1, 500, 0)
sprite.add(monsterSet, “jumping”, 1, 1, 1, 1)
–set the different variables we will use for our monster sprite
–also sets and starts the first animation for the monster
local monster = sprite.newSprite(monsterSet)
monster:prepare(“running”)
monster:play()
monster.x = 110
monster.y = 170
–these are 2 variables that will control the falling and jumping of the monster
monster.gravity = -11
monster.accel = 10
monster.isAlive = true
–rectangle used for our collision detection
–it will always be in front of the monster sprite
–that way we know if the monster hit into anything
local collisionRect = display.newRect(monster.x + 30, monster.y, 1, 70)
collisionRect.strokeWidth = 1
collisionRect:setFillColor(140, 140, 140)
collisionRect:setStrokeColor(180, 180, 180)
collisionRect.alpha = 0
–used to put everything on the screen into the screen group
–this will let us change the order in which sprites appear on
–the screen if we want. The earlier it is put into the group the
–further back it will go
screen:insert(skyline)
screen:insert(clouds)
screen:insert(backgroundnear2)
screen:insert(blocks)
screen:insert(stumps)
screen:insert(spikes)
screen:insert(blasts)
screen:insert(ghosts)
screen:insert(monster)
screen:insert(collisionRect)
screen:insert(scoreText)
screen:insert(gameOver)
screen:insert(pause)
screen:insert(play)
screen:insert(nextlevel)
screen:insert(LevelComplete)
–the update function will control most everything that happens in our game
–this will be called every frame(30 frames per second in our case(the default in corona))
local function update( event )
updateBackgrounds()
updateSpeed()
updateMonster()
updateBlocks()
checkCollisions()
updateBlasts()
updateSpikes()
updateGhosts()
updateStumps()
end
function checkCollisions()
–boolean variable so we know if we were on the ground in the last frame
wasOnGround = onGround
for a = 1, blocks.numChildren, 1 do
if(collisionRect.y - 10> blocks[a].y - 170 and blocks[a].x - 40 < collisionRect.x and blocks[a].x + 40 > collisionRect.x) then
–stop the monster
speed = 0
monster.isAlive = false
monster:pause()
gameOver.x = display.contentWidth*.65
gameOver.y = display.contentHeight/2
end
end
–stop the game if the monster runs into a spike wall
for a = 1, spikes.numChildren, 1 do
if(spikes[a].isAlive == true) then
if(collisionRect.y - 10> spikes[a].y - 170 and spikes[a].x - 40 < collisionRect.x and spikes[a].x + 40 > collisionRect.x) then
–stop the monster
speed = 0
monster.isAlive = false
monster:pause()
monster.accel = monster.accel + 60
gameOver.x = display.contentWidth*.65
gameOver.y = display.contentHeight/2
end
end
end
–stop the game if the monster runs into a spike wall
for a = 1, stumps.numChildren, 1 do
if(stumps[a].isAlive == true) then
if(collisionRect.y - 5> stumps[a].y - 90 and stumps[a].x - 25 < collisionRect.x and stumps[a].x + 40 > collisionRect.x) then
–stop the monster
speed = 0
monster.isAlive = false
–this simply pauses the current animation
monster:pause()
monster:rotate(20)
gameOver.x = display.contentWidth*.65
gameOver.y = display.contentHeight/2
end
end
end
–make sure the player didn’t get hit by a ghost!
for a = 1, ghosts.numChildren, 1 do
if(ghosts[a].isAlive == true) then
if((( ((monster.y-ghosts[a].y))<70) and ((monster.y - ghosts[a].y) > -70)) and (ghosts[a].x - 40 < collisionRect.x and ghosts[a].x + 40 > collisionRect.x)) then
–stop the monster
speed = 0
monster.isAlive = false
–this simply pauses the current animation
monster:pause()
gameOver.x = display.contentWidth*.65
gameOver.y = display.contentHeight/2
end
end
end
for a = 1, blocks.numChildren, 1 do
if(monster.y >= blocks[a].y - 180 and blocks[a].x < monster.x + 60 and blocks[a].x > monster.x - 60) then
monster.y = blocks[a].y - 171
onGround = true
break
else
onGround = false
end
end
end
–update the ghosts if they are alive
function updateGhosts()
for a = 1, ghosts.numChildren, 1 do
if(ghosts[a].isAlive == true) then
(ghosts[a]):translate(speed * -1, 0)
if(ghosts[a].y > monster.y) then
ghosts[a].y = ghosts[a].y - 1
end
if(ghosts[a].y < monster.y) then
ghosts[a].y = ghosts[a].y + 1
end
if(ghosts[a].x < -80) then
ghosts[a].x = 800
ghosts[a].y = 600
ghosts[a].speed = 0
ghosts[a].isAlive = false;
end
end
end
end
–check to see if the spikes are alive or not, if they are
–then update them appropriately
function updateSpikes()
for a = 1, spikes.numChildren, 1 do
if(spikes[a].isAlive == true) then
(spikes[a]):translate(speed * -1, 0)
if(spikes[a].x < -80) then
spikes[a].x = 900
spikes[a].y = 500
spikes[a].isAlive = false
end
end
end
end
– update tree stumps
function updateStumps()
for a = 1, stumps.numChildren, 1 do
if(stumps[a].isAlive == true) then
(stumps[a]):translate(speed * -1, 0)
if(stumps[a].x < -80) then
stumps[a].x = 800
stumps[a].y = 100
stumps[a].isAlive = false
end
end
end
end
function updateBlasts()
–for each blast that we instantiated check to see what it is doing
for a = 1, blasts.numChildren, 1 do
–if that blast is not in play we don’t need to check anything else
if(blasts[a].isAlive == true) then
(blasts[a]):translate(25, 0)
–if the blast has moved off of the screen, then kill it and return it to its original place
if(blasts[a].x > 550) then
blasts[a].x = 800
blasts[a].y = 500
blasts[a].isAlive = false
end
end
–check for collisions between the blasts and the spikes
for b = 1, spikes.numChildren, 1 do
if(spikes[b].isAlive == true) then
if(blasts[a].y - 25 > spikes[b].y - 120 and blasts[a].y + 25 < spikes[b].y + 120 and spikes[b].x - 40 < blasts[a].x + 25 and spikes[b].x + 40 > blasts[a].x - 25) then
blasts[a].x = 800
blasts[a].y = 500
blasts[a].isAlive = false
spikes[b].x = 900
spikes[b].y = 500
spikes[b].isAlive = false
end
end
end
–check for collisions between the blasts and the ghosts
for b = 1, ghosts.numChildren, 1 do
if(ghosts[b].isAlive == true) then
if(blasts[a].y - 25 > ghosts[b].y - 120 and blasts[a].y + 25 < ghosts[b].y + 120 and ghosts[b].x - 40 < blasts[a].x + 25 and ghosts[b].x + 40 > blasts[a].x - 25) then
blasts[a].x = 800
blasts[a].y = 500
blasts[a].isAlive = false
ghosts[b].x = 800
ghosts[b].y = 600
ghosts[b].isAlive = false
ghosts[b].speed = 0
end
end
end
end
end
function updateMonster()
–if our monster is jumping then switch to the jumping animation
–if not keep playing the running animation
if(monster.isAlive == true) then
if(onGround) then
if(wasOnGround) then
else
monster:prepare(“running”)
monster:play()
end
else
monster:prepare(“jumping”)
monster:play()
end
if(monster.accel > 0) then
monster.accel = monster.accel - 1
end
monster.y = monster.y - monster.accel
monster.y = monster.y - monster.gravity
end
–update the collisionRect to stay in front of the monster
collisionRect.y = monster.y
end
function restartGame()
–move menu
gameOver.x = 0
gameOver.y = 500
–reset the score
score = 0
–reset the game speed
speed = 10
–reset the monster
monster.isAlive = true
monster.x = 110
monster.y = 160
monster:prepare(“running”)
monster:play()
monster.rotation = 0
–reset the groundLevel
groundLevel = groundMin
for a = 1, blocks.numChildren, 1 do
blocks[a].x = (a * 79) - 79
blocks[a].y = groundLevel
end
–reset the ghosts
for a = 1, ghosts.numChildren, 1 do
ghosts[a].x = 800
ghosts[a].y = 600
end
–reset the spikes
for a = 1, spikes.numChildren, 1 do
spikes[a].x = 900
spikes[a].y = 500
end
–reset the blasts
for a = 1, blasts.numChildren, 1 do
blasts[a].x = 800
blasts[a].y = 500
end
– reset tree stumps
for a=1, stumps.numChildren, 1 do
stumps[a].x = 800
stumps[a].y = groundLevel - 148
end
–reset the backgrounds
backgroundnear2.x = 760
backgroundnear2.y = 220
clouds.x = 200
clouds.y = 80
plane.x = 300
plane.y = 55
end
–this is the function that handles the jump events. If the screen is touched on the left side
–then make the monster jump
function touched( event )
if(event.x < gameOver.x + 20 and event.x > gameOver.x - 100 and event.y < gameOver.y + 50 and event.y > gameOver.y - 50) then
restartGame()
else
if(monster.isAlive == true) then
if(event.phase == “began”) then
if(event.x < 145) then
if(onGround) then
monster.accel = monster.accel + 29
audio.play(jump)
end
else
for a=1, blasts.numChildren, 1 do
if(blasts[a].isAlive == false) then
blasts[a].isAlive = true
blasts[a].x = monster.x + 10
blasts[a].y = monster.y
audio.play(shot)
break
else
if(event.x < play.x + 50 and event.x > play.x - 50 and event.y < play.y + 50 and event.y > play.y - 50) then
speed = 10
play.x = 500
play.y = 0
pause.x = 450
pause.y = 30
else
if(event.x < pause.x + 20 and event.x > pause.x - 20 and event.y < pause.y + 20 and event.y > pause.y - 20) then
speed = 0
monster.alive = false
monster:pause()
pause.x = 500
pause.y = 0
play.x = 450
play.y = 30
else
if(event.x < nextlevel.x + 20 and event.x > nextlevel.x - 20 and event.y < nextlevel.y + 20 and event.y > nextlevel.y - 20) then
end
end
end
end
end
end
end
end
end
end
function updateSpeed()
speed = speed + .000
end
function updateBlocks()
for a = 1, blocks.numChildren, 1 do
if(a > 1) then
newX = (blocks[a - 1]).x + 79
else
newX = (blocks[8]).x + 79 - speed
end
if((blocks[a]).x < -40) then
score = score + 1
scoreText.text = "Score: " … score
scoreText:setReferencePoint(display.CenterLeftReferencePoint)
scoreText.x = 0
scoreText.y = 30
if score == 20 then
speed = 0
LevelComplete.x = display.contentWidth*.5
LevelComplete.y = display.contentHeight - 240
end
if(inEvent == 11) then
(blocks[a]).x, (blocks[a]).y = newX, 100
else
(blocks[a]).x, (blocks[a]).y = newX, groundLevel
end
–by setting up the spikes this way we are guaranteed to
–only have 3 spikes out at most at a time.
if(inEvent == 12) then
for a=1, spikes.numChildren, 1 do
if(spikes[a].isAlive == true) then
–do nothing
else
spikes[a].isAlive = true
spikes[a].y = groundLevel - 197
spikes[a].x = newX
break
end
end
end
– setting up tree stumps
if(inEvent == 14) then
for a=1, stumps.numChildren, 1 do
if(stumps[a].isAlive == true) then
–do nothing
else
stumps[a].isAlive = true
stumps[a].y = groundLevel - 158
stumps[a].x = newX
break
end
end
end
checkEvent()
else
(blocks[a]):translate(speed * -1, 0)
end
end
end
function checkEvent()
–first check to see if we are already in an event, we only want 1 event going on at a time
if(eventRun > 0) then
eventRun = eventRun - 1
if(eventRun == 0) then
inEvent = 0
end
end
–if we are in an event then do nothing
if(inEvent > 0 and eventRun > 0) then
–Do nothing
else
check = math.random(90)
if(check > 40 and check < 60) then
inEvent = math.random(10)
eventRun = 1
end
if(check > 98) then
inEvent = 11
eventRun = 2
end
–the more frequently you want events to happen then
–greater you should make the checks
if(check > 25 and check < 30) then
inEvent = 12
eventRun = 1
end
–ghost event
if(check > 30 and check < 31) then
inEvent = 13
eventRun = 1
end
–tree stump event
if(check > 40 and check < 43) then
inEvent = 14
eventRun = 1
end
end
–if we are in an event call runEvent to figure out if anything special needs to be done
if(inEvent > 0) then
runEvent()
end
end
function runEvent()
if(inEvent < 6) then
groundLevel = groundLevel + 40
end
if(inEvent > 5 and inEvent < 11) then
groundLevel = groundLevel - 40
end
if(groundLevel < groundMax) then
groundLevel = groundMax
end
if(groundLevel > groundMin) then
groundLevel = groundMin
end
–this will be a little bit different as we want this to really
–make the game feel even more random. change where the ghosts
–spawn and how fast they come at the monster.
–this will be a little bit different as we want this to really
–make the game feel even more random. change where the ghosts
–spawn and how fast they come at the monster.
if(inEvent == 13) then
for a=1, ghosts.numChildren, 1 do
if(ghosts[a].isAlive == false) then
ghosts[a].isAlive = true
ghosts[a].x = 500
ghosts[a].y = math.random(0, 0)
ghosts[a].speed = math.random(2,4)
break
end
end
end
end
function updateBackgrounds()
clouds.x = clouds.x - (speed + 1)
if(clouds.x < -239) then
clouds.x = 760
end
backgroundnear2.x = backgroundnear2.x - (speed)
if(backgroundnear2.x < -239) then
backgroundnear2.x = 760
end
plane.x = plane.x - (speed + 5)
if(plane.x < - 239) then
plane.x = 760
end
end
–this is how we call the update function, make sure that this line comes after the actual function or it will not be able to find it
–timer.performWithDelay(how often it will run in milliseconds, function to call, how many times to call(-1 means forever))
– all display objects must be inserted into group
timer.performWithDelay(1, update, -1)
Runtime:addEventListener(“touch”, touched, -1)
— END OF ALL ACTIONS OF GAME
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
end
– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group = self.view
if LevelComplete then
LevelComplete:removeSelf()
LevelComplete = nil
end
end
– END OF YOUR IMPLEMENTATION
– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched whenever before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )
return scene
[import]uid: 175550 topic_id: 31286 reply_id: 331286[/import]