Lua 游戏速度会在每次重启时自动增加-Corona
我想在科罗纳岛上做一个喷气背包游戏。首先,与其他sdk不同的是,当您更改场景时,前一个场景会自动删除,在corona中,您必须删除每个对象和功能 一周后,我终于能够在变换场景时移除对象,但现在我遇到了一个新问题。每次当我改变场景,回到游戏屏幕时,激光和导弹的速度每增加一次(也许两次?)。如何重置其速度? 我甚至添加了另一个“gameStatus”变量来删除事件侦听器,并尝试打印它,但游戏状态实际上从未返回“ended”,它始终是“running”。这里是游戏场景屏幕,一切都发生在这里Lua 游戏速度会在每次重启时自动增加-Corona,lua,coronasdk,corona-storyboard,corona-director,Lua,Coronasdk,Corona Storyboard,Corona Director,我想在科罗纳岛上做一个喷气背包游戏。首先,与其他sdk不同的是,当您更改场景时,前一个场景会自动删除,在corona中,您必须删除每个对象和功能 一周后,我终于能够在变换场景时移除对象,但现在我遇到了一个新问题。每次当我改变场景,回到游戏屏幕时,激光和导弹的速度每增加一次(也许两次?)。如何重置其速度? 我甚至添加了另一个“gameStatus”变量来删除事件侦听器,并尝试打印它,但游戏状态实际上从未返回“ended”,它始终是“running”。这里是游戏场景屏幕,一切都发生在这里 编辑:我
编辑:我刚刚注意到一些事情,例如,如果3个对象已经繁殖,那么在重新加载屏幕后,只有前3个对象看起来很快,从第四个开始是正常的
local composer = require( "composer" )
local scene = composer.newScene()
local physics = require( "physics" )
physics.start()
local image, text1
local function onSceneTouch( self, event )
if event.phase == "began" then
composer.gotoScene( "startScreen", "fade", 400 )
return true
end
end
function scene:create( event )
local sceneGroup = self.view
-- Variable
---------------------------------------
sW = display.contentWidth
sH = display.contentHeight
-- trying to reset gameStatus on restart
gameStatus = "ended"
gameStatus = "running"
--------------------------------------------------------
backgroundTiles = {}
drawBackground = function()
cieling = display.newRect( 0, 0, 2 * sW, sH * 0.05)
physics.addBody( cieling, "static", { density=0, friction=0, bounce=0 } )
cieling.x, cieling.y = cieling.contentWidth / 4, cieling.contentHeight * 0.25
cieling.alpha = 0
cieling.name = "ceiling"
ground = display.newRect( 0, sH, 2 * sW, sH * 0.05)
physics.addBody( ground, "static", { density=3, friction=1, bounce=0.1 } )
ground.x, ground.y = ground.contentWidth / 4, sH - ground.contentHeight * 0.25
ground.alpha = 0
ground.name = "ground"
end
drawBackground()
--------------------------------------------------------
--------------------------------------------------------
drawBird = function()
bird = display.newImageRect( "a/img/hero/hero.png", 80, 58)
bird.x, bird.y = 0 - sW * 0.1, ground.y - ground.contentHeight - 10
bird.isFixedRotation = true; bird.angularVelocity = 0;
physics.addBody( bird, { density=1, friction=0.5, bounce=0.1 } )
bird.name = "bird"
sceneGroup:insert( bird )
bird:addEventListener( "collision", onCollision )
end
coinsCollected, tokensCollected = 0, 0
birdFlight = function()
if ( gameStatus == "running" ) then
transition.to(bird, {
y = bird.y - 75,
transition = easing.outQuad,
onComplete = function() end
})
function birdFlight()
--transition.to( bird, { rotation=-45, time=300 } )
end
birdFlight()
function birdFall()
--transition.to( bird, { rotation=90, time=300 } )
end
timer.performWithDelay(700, birdFall, 1)
end
end
display.currentStage:addEventListener( "tap", birdFlight )
function onCollision(event)
if event.phase == "began" then
if event.other.name == "coin" then
coinsCollected = coinsCollected + 1
end
if event.other.name == "token" then
tokensCollected = tokensCollected + 1
end
if event.other.name == "missile" or event.other.name == "laser" or event.other.name == "rod" then
-- game ended
end
end
end
drawBird()
function atFrame(event)
if gameStatus == "running" then
bird.x,bird.y = sW / 3,bird.y + 9
if ( bird.y > sH - 70) then bird.y = sH - 70 end
if ( gameStatus == "ended" ) then bird.y = bird.y + 15 end
end
end
Runtime:addEventListener( "enterFrame", atFrame )
--------------------------------------------------------
--------------------------------------------------------
missile, missileAlert, missileMoving = {}, {}, {}
function removeMissile(i)
local onEnterFrame = function( event )
if missile[i] ~= nil and missile[i].x ~= nil and gameStatus == "running" then
if missile[i].x < -100 then
display.remove( missile[i] )
missile[i] = nil
end
end
end
Runtime:addEventListener( "enterFrame", onEnterFrame )
print(gameStatus)
if gameStatus == "ended" then
Runtime:removeEventListener( "enterFrame", onEnterFrame )
end
end
function flyMissile(i)
local onEnterFrame = function( event )
if missile[i] ~= nil and missile[i].x ~= nil and missile[i].y ~= nil and gameStatus == "running" then
if missileMoving[i] == true then
missile[i].y = bird.y
end
missile[i].x = missile[i].x - 5
if missileAlert[i] ~= nil then
if missileMoving[i] == true then
missileAlert[i].y = bird.y
end
if missile[i].x < sW then
display.remove( missileAlert[i] )
missileAlert[i] = nil
end
end
end
end
Runtime:addEventListener( "enterFrame", onEnterFrame )
print(gameStatus)
if gameStatus == "ended" then
Runtime:removeEventListener( "enterFrame", onEnterFrame )
end
end
function holdMissile(i)
local onEnterFrame = function( event )
if missile[i] ~= nil and missile[i].x ~= nil and gameStatus == "running" then
if missile[i].x < sW * 1.5 then
if missileAlert[i] ~= nil then
missileAlert[i]:setFillColor(1,1,0)
end
missileMoving[i] = false
end
end
end
Runtime:addEventListener( "enterFrame", onEnterFrame )
print(gameStatus)
if gameStatus == "ended" then
Runtime:removeEventListener( "enterFrame", onEnterFrame )
end
end
function spawnMissile(i)
missile[i] = display.newRect( sW*2, sH/2, 80, 80 )
missileAlert[i] = display.newRect( sW-80, bird.y, 80, 80 )
physics.addBody(missile[i],"kinematic",{isSensor=true})
missile[i].name = "missile"
sceneGroup:insert( missile[i] )
sceneGroup:insert( missileAlert[i] )
missileMoving[i] = true
flyMissile(i)
removeMissile(i)
holdMissile(i)
end
spawnMissile(1)
--------------------------------------------------------
--------------------------------------------------------
laser = {}
function moveAndRemovelaser(i)
local onEnterFrame = function( event )
if laser[i] ~= nil and laser[i].x ~= nil and gameStatus == "running" then
laser[i].x = laser[i].x - 5
if laser[i].x < 0 - sW / 2 then
display.remove( laser[i] )
laser[i] = nil
end
end
end
Runtime:addEventListener( "enterFrame", onEnterFrame )
print(gameStatus)
if gameStatus == "ended" then
Runtime:removeEventListener( "enterFrame", onEnterFrame )
end
end
function spawnlaser(i)
laserSize = math.random(1,2)
laserPosition = math.random(1,3)
laserRotation = math.random(1,4)
if laserSize == 1 then
laser[i] = display.newRect( 100,100,50,sH/3 )
else
laser[i] = display.newRect( 100,100,50,sH/2 )
end
sceneGroup:insert( laser[i] )
laser[i].x = sW * 2
laser[i].y = sH / 2
laser[i].name = "laser"
if laserPosition == 1 and laserRotation ~= 4 then
laser[i].y = sH * 0.05 + 12
laser[i].anchorY = 0
elseif laserPosition == 3 and laserRotation ~= 4 then
laser[i].y = sH * 0.95 - 12
laser[i].anchorY = 1
end
if laserPosition == 1 and laserRotation == 4 then
laser[i].y = sH * 0.05 + laser[i].contentHeight / 2
elseif laserPosition == 3 and laserRotation == 4 then
laser[i].y = sH * 0.95 - laser[i].contentHeight / 2
end
if laserRotation == 1 then
laser[i].rotation = -45
elseif laserRotation == 2 then
laser[i].rotation = 0
elseif laserRotation == 3 then
laser[i].rotation = 45
elseif laserRotation == 4 then
local onEnterFrame = function( event )
if laser[i] ~= nil and laser[i].rotation ~= nil then
laser[i].rotation = laser[i].rotation + 5
end
end
Runtime:addEventListener( "enterFrame", onEnterFrame )
end
laser[i]:setFillColor(1,1,0)
physics.addBody(laser[i],"kinematic",{isSensor=true})
moveAndRemovelaser(i)
end
spawnlaser(1)
--------------------------------------------------------
image = display.newRect (100,100,100,100)
image.x = display.contentCenterX
image.y = display.contentCenterY
sceneGroup:insert( image )
image.touch = onSceneTouch
text1 = display.newText( "Game Screen", 0, 0, native.systemFontBold, 24 )
text1:setFillColor( 255 )
text1.x, text1.y = display.contentWidth * 0.5, 50
sceneGroup:insert( text1 )
end
function scene:show( event )
local phase = event.phase
if "will" == phase then
gameStatus = "ended"
end
if "did" == phase then
print( "4" )
composer.removeScene( "startScreen" )
image:addEventListener( "touch", image )
gameStatus = "running"
end
end
function scene:hide( event )
local phase = event.phase
if "will" == phase then
print( "5" )
gameStatus = "ended"
image:removeEventListener( "touch", image )
Runtime:removeEventListener( "enterFrame", onEnterFrame )
end
end
function scene:destroy( event )
print( "6" )
end
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
return scene
localcomposer=require(“composer”)
本地场景=composer.newScene()
本地物理=要求(“物理”)
物理开始
本地图像,文本1
本地函数onSceneTouch(自身、事件)
如果event.phase==“开始”,则
作曲家。歌托森(“星绿”,“褪色”,400)
返回真值
结束
结束
功能场景:创建(事件)
本地场景组=self.view
--变数
---------------------------------------
sW=display.contentWidth
sH=display.contentHeight
--尝试在重新启动时重置游戏状态
gameStatus=“结束”
gameStatus=“正在运行”
--------------------------------------------------------
backgroundTiles={}
牵引杆接地=功能()
cieling=display.newRect(0,0,2*sW,sH*0.05)
addBody(cieling,“静态”{密度=0,摩擦=0,反弹=0})
cieling.x,cieling.y=cieling.contentWidth/4,cieling.contentHeight*0.25
cieling.alpha=0
cieling.name=“天花板”
地面=显示.newRect(0,sH,2*sW,sH*0.05)
物理。addBody(地面,“静态”{密度=3,摩擦=1,反弹=0.1})
ground.x,ground.y=ground.contentWidth/4,sH-ground.contentHeight*0.25
地面α=0
ground.name=“地面”
结束
退税地()
--------------------------------------------------------
--------------------------------------------------------
drawBird=函数()
bird=display.newImageRect(“a/img/hero/hero.png”,80,58)
bird.x,bird.y=0-sW*0.1,ground.y-ground.contentHeight-10
bird.isFixedRotation=true;bird.angularVelocity=0;
物理。addBody(鸟,{密度=1,摩擦=0.5,反弹=0.1})
bird.name=“bird”
场景组:插入(鸟)
鸟类:addEventListener(“碰撞”,onCollision)
结束
coinsCollected,tokensCollected=0,0
birdFlight=函数()
如果(游戏状态=“正在运行”),则
过渡到(鸟{
y=bird.y-75,
transition=easing.outQuad,
onComplete=函数()结束
})
函数birdFlight()
--转换到(bird,{rotation=-45,time=300})
结束
鸟灯
函数birdFall()
--转换到(bird,{旋转=90,时间=300})
结束
定时器。延时性能(700,鸟巢,1)
结束
结束
display.currentStage:addEventListener(“轻敲”,鸟灯)
函数冲突(事件)
如果event.phase==“开始”,则
如果event.other.name==“coin”,则
coinsCollected=coinsCollected+1
结束
如果event.other.name==“令牌”,则
tokensCollected=tokensCollected+1
结束
如果event.other.name==“导弹”或event.other.name==“激光”或event.other.name==“棒”,则
--比赛结束了
结束
结束
结束
drawBird()
功能atFrame(事件)
如果gameStatus==“正在运行”,则
bird.x,bird.y=sW/3,bird.y+9
如果(bird.y>sH-70),则bird.y=sH-70结束
如果(gameStatus==“end”),则bird.y=bird.y+15 end
结束
结束
运行时:addEventListener(“enterFrame”,atFrame)
--------------------------------------------------------
--------------------------------------------------------
导弹
功能移除导弹(一)
局部ONNETFRAME=函数(事件)
如果导弹[i]~=nil和导弹[i].x~=nil和gameStatus==“正在运行”,那么
如果导弹[i].x<-100那么
显示。删除(导弹[i])
导弹[i]=零
结束
结束
结束
运行时:addEventListener(“enterFrame”,onEnterFrame)
打印(游戏状态)
如果gameStatus==“已结束”,则
运行时:removeEventListener(“enterFrame”,onEnterFrame)
结束
结束
飞行导弹的功能(一)
局部ONNETFRAME=函数(事件)
如果导弹[i]~=nil和导弹[i].x~=nil和导弹[i].y~=nil和gameStatus==“正在运行”,那么
如果missileMoving[i]==真,则
导弹[i].y=bird.y
结束
导弹[i].x=导弹[i].x-5
如果misslealert[i]~=nil,那么
如果missileMoving[i]==真,则
Missellelert[i].y=bird.y
结束
如果导弹[i].x