Corona故事板给出了黑色图像

时间:2013-06-29 22:15:49

标签: api sdk storyboard corona

我创建了一个带有2个按钮的菜单,我正在使用storyboard API来移动场景,但它只适用于第一个按钮,而另一个按钮则会产生黑屏,我很肯定SCENE没有错2,因为我试图将两个场景(SCENE 1和SCENE 2)设置为相同的代码,并且它与SCENE 1配合使用但仍然为SCENE 2提供了黑色图像,这是我的菜单代码,希望您可以修复错误:

local storyboard = require ("storyboard")
local scene = storyboard.newScene()


   function scene:createScene(event)

local screenGroup = self.view

background = display.newImage("start.png")
background:setReferencePoint(display.BottomLeftReferencePoint)
background.x = 0
background.y = 320
background.speed = 1
screenGroup:insert(background)

city2 = display.newImage("city2.png")
city2:setReferencePoint(display.BottomLeftReferencePoint)
city2.x = 0
city2.y = 320
screenGroup:insert(city2)



play = display.newImage("play.png")
play.x = 242
play.y = 161
screenGroup:insert(play)

controls = display.newImage("controls.png")
controls.x = 144
controls.y = 201 
screenGroup:insert(controls)



function SCENE1(event)
if event.phase == "began" then
    storyboard.gotoScene("SCENE1", "fade", 400)
end
end

function SCENE2(event)
if event.phase == "began" then
    storyboard.gotoScene("SCENE2", "fade", 400)
end
end

function scene:enterScene(event)

 play:addEventListener("touch", SCENE1)
 controls:addEventListener("touch", SCENE2)
 end



function scene:exitScene(event)
play:removeEventListener("touch", SCENE1)
controls:removeEventListener("touch", SCENE2)
end

function scene:destroyScene(event)

end

scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)

return scene

FOR SCENE 2:

   module(..., package.seeall)

   -- requires 

   local physics = require "physics"
   physics.start()

   require "sprite"

   score = require ("score")

   local storyboard = require ("storyboard")
   local scene = storyboard.newScene()
  -- background

  function scene:createScene(event)

local screenGroup = self.view

background = display.newImage("bg.png")
screenGroup:insert(background)

scoreInfo = score.getInfo()
score.init({
x = 40,
y = 5}
)
score.setScore(0)

ceiling = display.newImage("invisibleTile.png")
ceiling:setReferencePoint(display.BottomLeftReferencePoint)
ceiling.x = 0
ceiling.y = 0
physics.addBody(ceiling, "static", {density=.1, bounce=0.1, friction=.2})
screenGroup:insert(ceiling)

theFloor = display.newImage("invisibleTile.png")
theFloor:setReferencePoint(display.BottomLeftReferencePoint)
theFloor.x = 0
theFloor.y = 340
physics.addBody(theFloor, "static", {density=.1, bounce=0.1, friction=.2})
screenGroup:insert(theFloor)


city1 = display.newImage("city1.png")
city1:setReferencePoint(display.BottomLeftReferencePoint)
city1.x = 0
city1.y = 320
city1.speed = 1
screenGroup:insert(city1)

city2 = display.newImage("city1.png")
city2:setReferencePoint(display.BottomLeftReferencePoint)
city2.x = 480
city2.y = 320
city2.speed = 1
screenGroup:insert(city2)

city3 = display.newImage("city2.png")
city3:setReferencePoint(display.BottomLeftReferencePoint)
city3.x = 0
city3.y = 320
city3.speed = 2
screenGroup:insert(city3)

city4 = display.newImage("city2.png")
city4:setReferencePoint(display.BottomLeftReferencePoint)
city4.x = 480
city4.y = 320
city4.speed = 2
screenGroup:insert(city4)


jetSpriteSheet = sprite.newSpriteSheet("jet.png", 50, 17)
jetSprites = sprite.newSpriteSet(jetSpriteSheet, 1, 4)
sprite.add(jetSprites, "jets", 1, 4, 1000, 0)
jet = sprite.newSprite(jetSprites)
jet.x = -80
jet.y = 100
jet:prepare("jets")
jet:play()
jet.collided = false
physics.addBody(jet, "static", {density=.1, bounce=0.1, friction=.2, radius=12})
screenGroup:insert(jet)
jetIntro = transition.to(jet,{time=2000, x=100, onComplete=jetReady})

explosionSpriteSheet = sprite.newSpriteSheet("explosion.png", 24, 23)
explosionSprites = sprite.newSpriteSet(explosionSpriteSheet, 1, 8)
sprite.add(explosionSprites, "explosions", 1, 8, 2000, 1)
explosion = sprite.newSprite(explosionSprites)
explosion.x = 100
explosion.y = 100
explosion:prepare("explosions")
 -- explosion:play()
explosion.isVisible = false
-- physics.addBody(jet, "dynamic", {density=.1, bounce=0.1, friction=.2, radius=12})
screenGroup:insert(explosion)

mine1 = display.newImage("mine.png")
mine1.x = math.random(480,500)
mine1.y = math.random(1,100)
mine1.speed = math.random(2,6)
mine1.initY = mine1.y
mine1.amp = math.random(20,90)
mine1.angle = math.random(1,360)
physics.addBody(mine1, "static", {density=.1, bounce=0.1, friction=.2, radius=12})
screenGroup:insert(mine1)

mine2 = display.newImage("mine.png")
mine2.x = math.random(900,950)
mine2.y = math.random(1,100)
mine2.speed = math.random(2,6)
mine2.initY = mine2.y
mine2.amp = math.random(20,100)
mine2.angle = math.random(1,360)
physics.addBody(mine2, "static", {density=.1, bounce=0.1, friction=.2, radius=12})
screenGroup:insert(mine2)


mine3 = display.newImage("mine.png")
mine3.x = math.random(1450,1570)
mine3.y = math.random(300,340)
mine3.speed = math.random(2,6)
mine3.initY = mine3.y
mine3.amp = math.random(20,70)
mine3.angle = math.random(1,360)
physics.addBody(mine3, "static", {density=.1, bounce=0.1, friction=.2, radius=12})
screenGroup:insert(mine3)

mine4 = display.newImage("mine.png")
mine4.x = math.random(2500,2770)
mine4.y = math.random(330,330)
mine4.speed = math.random(2,6)
mine4.initY = mine3.y
mine4.amp = math.random(20,100)
mine4.angle = math.random(1,360)
physics.addBody(mine4, "static", {density=.1, bounce=0.1, friction=.2, radius=12})
screenGroup:insert(mine4)

mine6 = display.newImage("mine.png")
mine6.x = math.random(4000,4770)
mine6.y = math.random(1,320)
mine6.speed = math.random(2,6)
mine6.initY = mine6.y
mine6.amp = math.random(20,80)
mine6.angle = math.random(1,360)
physics.addBody(mine6, "static", {density=.1, bounce=0.1, friction=.2, radius=12})
screenGroup:insert(mine6)

mine5 = display.newImage("mine.png")
mine5.x = math.random(5500,5770)
mine5.y = math.random(1,130)
mine5.speed = math.random(2,6)
mine5.initY = mine5.y
mine5.amp = math.random(20,80)
mine5.angle = math.random(1,360)
physics.addBody(mine5, "static", {density=.1, bounce=0.1, friction=.2, radius=12})
screenGroup:insert(mine5)
end

 function scrollCity(self,event)
if self.x < -477 then
    self.x = 480
else 
    self.x = self.x - self.speed
end
 end

  function moveMines(self,event)
if self.x < -50 then
     self.x = 500
     self.y = math.random(90,220)
     self.speed = math.random(2,6)
     self.amp = math.random(20,100)
     self.angle = math.random(1,360)
else 
    self.x = self.x - self.speed
    self.angle = self.angle + .1
    self.y = self.amp*math.sin(self.angle)+self.initY
end
end

function addtoit (event)
if event.phase == "ended" then
score.setScore (score.getScore()+1)
end
end


function jetReady()
jet.bodyType = "dynamic"
end

function activateJets(self,event)
self:applyForce(0, -1.5, self.x, self.y)
print("run")
end

function touchScreen(event)
print("touch")
if event.phase == "began" then
 jet.enterFrame = activateJets
     Runtime:addEventListener("enterFrame", jet)
end

if event.phase == "ended" then
 Runtime:removeEventListener("enterFrame", jet)
  end

end

function gameOver()
storyboard.gotoScene("restart", "fade", 400)
end

function explode()

explosion.x = jet.x
explosion.y = jet.y
explosion.isVisible = true
explosion:play()
jet.isVisible = false
timer.performWithDelay(3000, gameOver, 1)

end
 local taSound = audio.loadSound("GAME.mp3")
 local tapSound = audio.loadSound("explosion-01.wav")
function onCollision(event)
if event.phase == "began" then
  if jet.collided == false then 
    jet.collided = true
    jet.bodyType = "static"
    explode()
     audio.stop()
     audio.play(tapSound ,{ duration=3100 } )
     background:removeEventListener("touch", addtoit)
  end
end
end


 function scene:enterScene(event)


storyboard.purgeScene("start")
storyboard.purgeScene("restart")

Runtime:addEventListener("touch", touchScreen)

city1.enterFrame = scrollCity
Runtime:addEventListener("enterFrame", city1)

city2.enterFrame = scrollCity
Runtime:addEventListener("enterFrame", city2)

city3.enterFrame = scrollCity
Runtime:addEventListener("enterFrame", city3)

city4.enterFrame = scrollCity
Runtime:addEventListener("enterFrame", city4)

mine1.enterFrame = moveMines
Runtime:addEventListener("enterFrame", mine1)

mine2.enterFrame = moveMines
Runtime:addEventListener("enterFrame", mine2)

mine3.enterFrame = moveMines
Runtime:addEventListener("enterFrame", mine3)

mine4.enterFrame = moveMines
Runtime:addEventListener("enterFrame", mine4)

mine5.enterFrame = moveMines
Runtime:addEventListener("enterFrame", mine5)

 mine6.enterFrame = moveMines
 Runtime:addEventListener("enterFrame", mine6)

 Runtime:addEventListener("collision", onCollision)

audio.play(taSound)
 background:addEventListener("touch", addtoit)
end

function scene:exitScene(event)

Runtime:removeEventListener("touch", touchScreen)
Runtime:removeEventListener("enterFrame", city1)
Runtime:removeEventListener("enterFrame", city2)
Runtime:removeEventListener("enterFrame", city3)
Runtime:removeEventListener("enterFrame", city4)
Runtime:removeEventListener("enterFrame", mine1)
Runtime:removeEventListener("enterFrame", mine2)
Runtime:removeEventListener("enterFrame", mine3)
Runtime:removeEventListener("enterFrame", mine4)
Runtime:removeEventListener("collision", onCollision)

end

function scene:destroyScene(event)

end
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)
return scene

1 个答案:

答案 0 :(得分:0)

我认为问题在于scene:enterScene你试图一遍又一遍地加载城市对象,同时代码中有如此多的运行时监听器,它重复调用同一个对象。 你在创造一个侧面游戏吗?如果是这样,您可以尝试查看如何从头开始创建侧滚动游戏的链接http://mobile.tutsplus.com/tutorials/corona/corona-sdk-create-a-side-scroller-from-scratch/ 希望这有帮助