У меня есть 2 экрана, один level1
сцена & другого level2
сцена, после того, как игрок умер в level1
сцены все переменные уничтожаются & level2
сцены загружается, я у вас есть сенсорный прослушиватель в level2
, который перезагружает level1
, но проблема в том, что все переменные в level1
не переинициализируются &, а всегда вызывается метод scene:hide
, я не могу понять, что не так с моим кодом, ниже мой кодМоя сцена не перегрузочная все переменные после прихода из другого экрана
level1 code
--function to deal obs collision
local function obscols(self,event)
if(event.phase=="began") then
--print(event.other.myName)
if (self.myName=='stack1') then
obs[1].isVisible=false
obs[1].isAlive=false
elseif (self.myName=='stack2') then
obs[2].isVisible=false
obs[2].isAlive=false
elseif (self.myName=='stack3') then
obs[3].isVisible=false
obs[3].isAlive=false
elseif (self.myName=='stack4') then
obs[4].isVisible=false
obs[4].isAlive=false
elseif (self.myName=='stack5') then
obs[5].isVisible=false
obs[5].isAlive=false
elseif (self.myName=='stack6') then
obs[6].isVisible=false
obs[6].isAlive=false
elseif (self.myName=='stack7') then
obs[7].isVisible=false
obs[7].isAlive=false
elseif (self.myName=='stack8') then
obs[8].isVisible=false
obs[8].isAlive=false
elseif (self.myName=='stack9') then
obs[9].isVisible=false
obs[9].isAlive=false
elseif (self.myName=='stack10') then
obs[10].isVisible=false
obs[10].isAlive=false
elseif (self.myName=='stack11') then
obs[11].isVisible=false
obs[11].isAlive=false
elseif (self.myName=='stack12') then
obs[12].isVisible=false
obs[12].isAlive=false
elseif (self.myName=='stack13') then
obs[13].isVisible=false
obs[13].isAlive=false
elseif (self.myName=='stack14') then
obs[14].isVisible=false
obs[14].isAlive=false
elseif (self.myName=='stack15') then
obs[15].isVisible=false
obs[15].isAlive=false
elseif (self.myName=='stack16') then
obs[16].isVisible=false
obs[16].isAlive=false
elseif (self.myName=='stack17') then
obs[17].isVisible=false
obs[17].isAlive=false
elseif (self.myName=='stack18') then
obs[18].isVisible=false
obs[18].isAlive=false
elseif (self.myName=='stack19') then
obs[19].isVisible=false
obs[19].isAlive=false
elseif (self.myName=='stack20') then
obs[20].isVisible=false
obs[20].isAlive=false
elseif (self.myName=='stack21') then
obs[21].isVisible=false
obs[21].isAlive=false
elseif (self.myName=='stack22') then
obs[22].isVisible=false
obs[22].isAlive=false
elseif (self.myName=='stack23') then
obs[23].isVisible=false
obs[23].isAlive=false
elseif (self.myName=='stack24') then
obs[24].isVisible=false
obs[24].isAlive=false
--coins handling
elseif (event.other.myName=='coin_num1') then
coinsg[1].isVisible=false
coinsg[1].isAlive=false
elseif (event.other.myName=='coin_num2') then
coinsg[2].isVisible=false
coinsg[2].isAlive=false
elseif (event.other.myName=='coin_num3') then
coinsg[3].isVisible=false
coinsg[3].isAlive=false
elseif (event.other.myName=='coin_num4') then
coinsg[4].isVisible=false
coinsg[4].isAlive=false
elseif (event.other.myName=='coin_num5') then
coinsg[5].isVisible=false
coinsg[5].isAlive=false
elseif (event.other.myName=='coin_num6') then
coinsg[6].isVisible=false
coinsg[6].isAlive=false
elseif (event.other.myName=='coin_num7') then
coinsg[7].isVisible=false
coinsg[7].isAlive=false
elseif (event.other.myName=='coin_num8') then
coinsg[8].isVisible=false
coinsg[8].isAlive=false
elseif (event.other.myName=='coin_num9') then
coinsg[9].isVisible=false
coinsg[9].isAlive=false
elseif (event.other.myName=='coin_num10') then
coinsg[10].isVisible=false
coinsg[10].isAlive=false
elseif (event.other.myName=='coin_num11') then
coinsg[11].isVisible=false
coinsg[11].isAlive=false
elseif (event.other.myName=='coin_num12') then
coinsg[12].isVisible=false
coinsg[12].isAlive=false
elseif (event.other.myName=='coin_num13') then
coinsg[13].isVisible=false
coinsg[13].isAlive=false
elseif (event.other.myName=='coin_num14') then
coinsg[14].isVisible=false
coinsg[14].isAlive=false
elseif (event.other.myName=='coin_num15') then
coinsg[15].isVisible=false
coinsg[15].isAlive=false
elseif (event.other.myName=='coin_num16') then
coinsg[16].isVisible=false
coinsg[16].isAlive=false
elseif (event.other.myName=='coin_num17') then
coinsg[17].isVisible=false
coinsg[17].isAlive=false
elseif (event.other.myName=='coin_num18') then
coinsg[18].isVisible=false
coinsg[18].isAlive=false
elseif (event.other.myName=='coin_num19') then
coinsg[19].isVisible=false
coinsg[19].isAlive=false
elseif (event.other.myName=='coin_num20') then
coinsg[20].isVisible=false
coinsg[20].isAlive=false
elseif (event.other.myName=='coin_num21') then
coinsg[21].isVisible=false
coinsg[21].isAlive=false
elseif (event.other.myName=='coin_num22') then
coinsg[22].isVisible=false
coinsg[22].isAlive=false
elseif (event.other.myName=='coin_num23') then
coinsg[23].isVisible=false
coinsg[23].isAlive=false
elseif (event.other.myName=='coin_num24') then
coinsg[24].isVisible=false
coinsg[24].isAlive=false
end
--composer.gotoScene("level2",{ effect = "fade", time = 3000 } )
end
end
--function to move tiles
local function movet(event)
local tDelta = event.time - tPrevious
tPrevious = event.time
local xOffset = (0.05 * tDelta)
bbgtile.x=bbgtile.x-xOffset*bgspeed
bgtile.x=bgtile.x-xOffset*bgspeed
rtile.x=rtile.x-xOffset*rspeed
rrtile.x=rrtile.x-xOffset*rspeed
if bgtile.x < -480 + bgtile.width/2 then
bgtile:translate(480*2 , 0)
elseif bbgtile.x < -480 + bbgtile.width/2 then
bbgtile:translate(480*2 , 0)
elseif rtile.x<-480 +rtile.width/2 then
rtile:translate(480*2, 0)
elseif rrtile.x<-480 + rrtile.width/2 then
rrtile:translate(480*2, 0)
end
end
--function to deal coins group pooling
local function getcoin()
for j=1, #coinsg do
if not coinsg[j].isAlive then
return coinsg[j]
end
end
return nil
end
--function to deal ob1 group pooling
local function getobs()
local somerand=math.random(1,24)
--calling ob1 from pool
for i=1, #obs do
if not obs[somerand].isAlive then
return obs[somerand]
end
end
return nil
end
-- --independent coin spawning function
local function spcoin()
local c_elem = require("mydata")
c_elem.new=getcoin()
if(c_elem.new~=nil) then
c_elem.new.isVisible=true
c_elem.new.isAlive=true
c_elem.new.isBodyActive=true
c_elem.new.x=350
c_elem.new.y=140
--adding event listener for each coin
c_elem.new:setLinearVelocity(-100,0)
c_elem.new.collision=cclean
c_elem.new:addEventListener("collision",c_elem.new)
end
end
--independent obstacles spawning function
local function spobs()
local obs_elem = require("mydata")
obs_elem.new=getobs()
if(obs_elem.new~=nil) then
obs_elem.new.isVisible=true
obs_elem.new.isAlive=true
obs_elem.new.isBodyActive=true
obs_elem.new.isFixedRotation=true
obs_elem.new.x=math.random(320,380)
obs_elem.new.y=252
--adding event listener for each coin
obs_elem.new:setLinearVelocity(-250,0)
obs_elem.new.collision=obscols
obs_elem.new:addEventListener("collision",obs_elem.new)
end
end
--tile speed controlling function
local function setspeed(event)
if mytime==120 then
bgspeed=bgspeed+0.05
rspeed=rspeed+0.5
elseif mytime>140 then
mytime=0
end
end
-- --function to decide obstacle spawning
local function obdecide(event)
if(shut==0) then
if (mytime==100) then
spobs()
elseif (mytime>100) then
mytime=0
end
end
end
--function to spawn coins
local function cdecide(event)
if(shut==0) then
if(ctimer==15) then
spcoin()
elseif(ctimer==30) then
spcoin()
elseif(ctimer==45) then
spcoin()
elseif(ctimer==60) then
spcoin()
elseif(ctimer==75) then
spcoin()
elseif(ctimer>90) then
ctimer=0
end
end
end
local ovtimer=0
--game update loop
local function move(event)
if(shut==0) then
ctimer=ctimer+1
mytime=mytime+1
--setspeed(event)
movet(event)
obdecide(event)
cdecide(event)
elseif(shut==1) then
--ovtimer=ovtimer+1
--print(ovtimer)
--if(ovtimer==200) then
--allclean(event)
--elseif(ovtimer>200) then
--composer.removeScene("level1")
composer.gotoScene("level2")
--end
end
end
--player collision event with base & obstacles
local function colls(self,event)
if(event.phase=="began") then
--print(self.myName)
--print(event.other.myName)
if(self.myName=="hero" and event.other.myName=="base") then
player:setSequence("run")
player:play()
bird:play()
allow=1
else
--handle player death function
shut=1
print("player dead")
--composer.gotoScene("level2")
end
end
end
--coin collision with bird
local function cclean(self,event)
if(event.phase=="began") then
--print("got here")
--print(event.other.myName)
if (event.other.myName=='coin_num1') then
coinsg[1].isVisible=false
coinsg[1].isAlive=false
elseif (event.other.myName=='coin_num2') then
coinsg[2].isVisible=false
coinsg[2].isAlive=false
elseif (event.other.myName=='coin_num3') then
coinsg[3].isVisible=false
coinsg[3].isAlive=false
elseif (event.other.myName=='coin_num4') then
coinsg[4].isVisible=false
coinsg[4].isAlive=false
elseif (event.other.myName=='coin_num5') then
coinsg[5].isVisible=false
coinsg[5].isAlive=false
elseif (event.other.myName=='coin_num6') then
coinsg[6].isVisible=false
coinsg[6].isAlive=false
elseif (event.other.myName=='coin_num7') then
coinsg[7].isVisible=false
coinsg[7].isAlive=false
elseif (event.other.myName=='coin_num8') then
coinsg[8].isVisible=false
coinsg[8].isAlive=false
elseif (event.other.myName=='coin_num9') then
coinsg[9].isVisible=false
coinsg[9].isAlive=false
elseif (event.other.myName=='coin_num10') then
coinsg[10].isVisible=false
coinsg[10].isAlive=false
elseif (event.other.myName=='coin_num11') then
coinsg[11].isVisible=false
coinsg[11].isAlive=false
elseif (event.other.myName=='coin_num12') then
coinsg[12].isVisible=false
coinsg[12].isAlive=false
elseif (event.other.myName=='coin_num13') then
coinsg[13].isVisible=false
coinsg[13].isAlive=false
elseif (event.other.myName=='coin_num14') then
coinsg[14].isVisible=false
coinsg[14].isAlive=false
elseif (event.other.myName=='coin_num15') then
coinsg[15].isVisible=false
coinsg[15].isAlive=false
elseif (event.other.myName=='coin_num16') then
coinsg[16].isVisible=false
coinsg[16].isAlive=false
elseif (event.other.myName=='coin_num17') then
coinsg[17].isVisible=false
coinsg[17].isAlive=false
elseif (event.other.myName=='coin_num18') then
coinsg[18].isVisible=false
coinsg[18].isAlive=false
elseif (event.other.myName=='coin_num19') then
coinsg[19].isVisible=false
coinsg[19].isAlive=false
elseif (event.other.myName=='coin_num20') then
coinsg[20].isVisible=false
coinsg[20].isAlive=false
elseif (event.other.myName=='coin_num21') then
coinsg[21].isVisible=false
coinsg[21].isAlive=false
elseif (event.other.myName=='coin_num22') then
coinsg[22].isVisible=false
coinsg[22].isAlive=false
elseif (event.other.myName=='coin_num23') then
coinsg[23].isVisible=false
coinsg[23].isAlive=false
elseif (event.other.myName=='coin_num24') then
coinsg[24].isVisible=false
coinsg[24].isAlive=false
end
end
end
--function to make player jump when screen is tapped
local function jump(event)
if allow==1 and shut==0 then
player:setLinearVelocity(0, -400)
player:setSequence("lup")
bird:setLinearVelocity(0,-400)
bird:pause()
allow=0
end
return true
end
function scene:create(event)
local sceneGroup=self.view
print("i got called")
-- important libs loading
physics=require("physics")
tPrevious = system.getTimer()
--game variables
shut=0
mytime=0
ctimer=0
bgspeed = 0.5
rspeed = 4
allow=0
kill = 0
collisionFilter = { groupIndex = -2 }
--intro = audio.loadSound("images/rintro.mp3")
--theme = audio.play(intro, { channel=1, loops=-1 })
physics.start()
physics.setGravity(0, 36)
--adding centering position variables
x = display.contentCenterX
y = display.contentCenterY
--adding player animations
options=
{
frames = {
{ x = 0, y = 188, width = 28, height = 45, sourceX=0, sourceY=0, sourceWidth=28 , sourceHeight=45 },
{ x = 0, y = 94, width = 31, height = 45, sourceX=0, sourceY=0, sourceWidth=31 , sourceHeight=45 },
{ x = 0, y = 0, width = 37, height = 45, sourceX=0, sourceY=0, sourceWidth=37 , sourceHeight=45 },
{ x = 0, y = 47, width = 33, height = 45, sourceX=0, sourceY=0, sourceWidth=33 , sourceHeight=45 },
{ x = 0, y = 141, width = 30, height = 45, sourceX=0, sourceY=0, sourceWidth=30 , sourceHeight=45 },
{ x = 30, y = 188, width = 25, height = 45, sourceX=0, sourceY=0, sourceWidth=25 , sourceHeight=45 },
{ x = 35, y = 47, width = 20, height = 45, sourceX=0, sourceY=0, sourceWidth=20 , sourceHeight=45 },
{ x = 33, y = 94, width = 20, height = 45, sourceX=0, sourceY=0, sourceWidth=20 , sourceHeight=45 },
{ x = 32, y = 141, width = 23, height = 45, sourceX=0, sourceY=0, sourceWidth=23 , sourceHeight=45 }
},
sheetContentWidth = 64,
sheetContentHeight = 256
}
psheet=graphics.newImageSheet("images/sprites.png", options)
sequenceData =
{
{name="run", start=1, count=9, time=250},
{name="lup", start=7, count=1}
}
--bird data
boptions=
{
frames = {
{ x = 0, y = 28, width = 28, height = 19, sourceX=2, sourceY=0, sourceWidth=35 , sourceHeight=30 },
{ x = 0, y = 12, width = 33, height = 14, sourceX=2, sourceY=5, sourceWidth=35 , sourceHeight=30 },
{ x = 0, y = 0, width = 33, height = 10, sourceX=2, sourceY=11, sourceWidth=35 , sourceHeight=30 },
{ x = 30, y = 28, width = 28, height = 18, sourceX=2, sourceY=11, sourceWidth=35 , sourceHeight=30 },
{ x = 60, y = 17, width = 24, height = 20, sourceX=4, sourceY=10, sourceWidth=35 , sourceHeight=30 },
{ x = 30, y = 48, width = 26, height = 14, sourceX=2, sourceY=11, sourceWidth=35 , sourceHeight=30 },
{ x = 0, y = 49, width = 26, height = 8, sourceX=2, sourceY=10, sourceWidth=35 , sourceHeight=30 },
{ x = 35, y = 0, width = 25, height = 15, sourceX=3, sourceY=4, sourceWidth=35 , sourceHeight=30 }
},
sheetContentWidth = 128,
sheetContentHeight = 64
}
bsheet=graphics.newImageSheet("images/bsheet.png", boptions)
bdata =
{
{name="fly", start=1, count=8, time=600},
}
--adding bird
bfilter={ groupIndex = -4 }
--creating coins group
climit=24
coinsg={}
--creating obstacles group
obslimit=24
obs={}
Runtime:addEventListener("tap", jump);
end
function scene:show(event)
local sceneGroup=self.view
local phase = event.phase
-- local previousScene = composer.getSceneName("previous")
-- if(previousScene~=nil) then
-- composer.removeScene(previousScene)
-- end
if(phase=="will") then
bgtile = display.newImage("images/bgtile.png",x,y)
bbgtile=display.newImage("images/bgtile.png", x+480 ,y)
rtile=display.newImage("images/rtile.png", x ,y+122)
rrtile=display.newImage("images/rtile.png", x+480, y+122)
--adding player base to stand on
base=display.newImage("images/base.png", 80,272)
physics.addBody(base, "static",{ bounce=0.0, friction=1,filter=collisionFilter})
base.isVisible=false
base.myName="base"
--adding base to bird
bbase=display.newImage("images/bbase.png",80,150)
physics.addBody(bbase,"static",{bounce=0.0,friction=1,filter=collisionFilter})
bbase.isVisible=false
bbase.myName="bbase"
--adding stopper to destroy out of bound objects
stopper=display.newImage("images/stopper.png", -20,158)
physics.addBody(stopper, "static", {bounce=0.0,friction=1,gravityScale=0,filter=bfilter})
stopper.myName="stopper"
stopper.isVisible=false
--coins setter lines
seta=display.newRect(1,128,1200,1)
physics.addBody(seta,"static",{friction=0,bounce=0.0,filter=bfilter})
seta.isVisible=false
setb=display.newRect(1,150,1200,1)
physics.addBody(setb,"static",{friction=0,bounce=0.0,filter=bfilter})
setb.isVisible=false
for j=1,climit do
coinsg[j]=display.newImage("images/coin.png", 250,250)
coinsg[j].isVisible=false
coinsg[j].isAlive=false
physics.addBody(coinsg[j], "dynamic", {friction=0,bounce=0.0,filter=collisionFilter,density=0})
coinsg[j].myName="coin_num"..j
coinsg[j].gravityScale=0
coinsg[j].isBodyActive=false
end
obs[1]=display.newImage("images/ob1.png", 250,250)
obs[2]=display.newImage("images/ob2.png", 250,250)
obs[3]=display.newImage("images/ob3.png", 250,250)
obs[4]=display.newImage("images/ob4.png", 250,250)
obs[5]=display.newImage("images/ob1.png", 250,250)
obs[6]=display.newImage("images/ob2.png", 250,250)
obs[7]=display.newImage("images/ob3.png", 250,250)
obs[8]=display.newImage("images/ob4.png", 250,250)
obs[9]=display.newImage("images/ob1.png", 250,250)
obs[10]=display.newImage("images/ob2.png", 250,250)
obs[11]=display.newImage("images/ob3.png", 250,250)
obs[12]=display.newImage("images/ob4.png", 250,250)
obs[13]=display.newImage("images/ob1.png", 250,250)
obs[14]=display.newImage("images/ob2.png", 250,250)
obs[15]=display.newImage("images/ob3.png", 250,250)
obs[16]=display.newImage("images/ob4.png", 250,250)
obs[17]=display.newImage("images/ob1.png", 250,250)
obs[18]=display.newImage("images/ob2.png", 250,250)
obs[19]=display.newImage("images/ob3.png", 250,250)
obs[20]=display.newImage("images/ob4.png", 250,250)
obs[21]=display.newImage("images/ob1.png", 250,250)
obs[22]=display.newImage("images/ob2.png", 250,250)
obs[23]=display.newImage("images/ob3.png", 250,250)
obs[24]=display.newImage("images/ob4.png", 250,250)
for i=1,obslimit do
obs[i].isVisible=false
obs[i].isAlive=false
physics.addBody(obs[i], "dynamic", {friction=1,bounce=0.0,filter=collisionFilter})
obs[i].myName="stack"..i
obs[i].gravityScale=0
obs[i].isBodyActive=false
end
player=display.newSprite(psheet, sequenceData)
player.x=80
player.y=248
player:setSequence("run")
bird=display.newSprite(bsheet,bdata)
bird.x=88
bird.y=140
bird:setSequence("fly")
elseif(phase=="did") then
player:play()
physics.addBody(player, "dynamic",{ friction=1,density=100 })
player.myName="hero"
bird:play()
physics.addBody(bird, "dynamic",{ friction=1 ,bounce=0.0, filter=bfilter,density=100})
bird.myName="bird"
player.collision=colls
player:addEventListener("collision",player)
stopper.collision=obscols
stopper:addEventListener("collision", stopper)
bird.collision=cclean
bird:addEventListener("collision",bird)
Runtime:addEventListener("enterFrame", move);
end
end
function scene:hide(event)
local phase = event.phase
if (phase == "will") then
--physics.stop()
player.collision=nil
stopper.collision=nil
bird.collision=nil
Runtime:removeEventListener("tap", jump)
player:removeEventListener("collision",player)
stopper:removeEventListener("collision", stopper)
bird:removeEventListener("collision",bird)
print("destroyed")
player:removeSelf()
player=nil
bird:removeSelf()
bird=nil
base:removeSelf()
base=nil
bbase:removeSelf()
bbase=nil
bbgtile:removeSelf()
bbgtile=nil
bgtile:removeSelf()
bgtile=nil
rtile:removeSelf()
rtile=nil
rrtile:removeSelf()
rrtile=nil
for k=1,24 do
obs[k]:removeSelf()
obs[k]=nil
coinsg[k]:removeSelf()
coinsg[k]=nil
end
end
end
function scene:destroy(event)
local sceneGroup = self.view
-- Runtime:removeEventListener("tap", jump)
-- player:removeEventListener("collision",player)
-- stopper:removeEventListener("collision", stopper)
-- bird:removeEventListener("collision",bird)
end
scene:addEventListener("create", scene)
scene:addEventListener("show", scene)
scene:addEventListener("hide", scene)
scene:addEventListener("destroy", scene)
-------------------------scene ending------------------------------------------------------
return scene
level2 code
local composer=require("composer")
local scene=composer.newScene("level2")
local function reload(event)
composer.gotoScene("level1")
end
function scene:create(event)
local sceneGroup=self.view
--tap event listener
Runtime:addEventListener("tap", reload);
end
function scene:show(event)
local sceneGroup=self.view
end
function scene:destroy(event)
local sceneGroup = self.view
--banner:removeSelf()
Runtime:removeEventListener("tap", reload)
end
scene:addEventListener("create", scene)
scene:addEventListener("show", scene)
scene:addEventListener("destroy", scene)
local x = display.contentCenterX
local y = display.contentCenterY
--local banner=display.newImage("images/gover.png",x,y)
return scene