Corona SDK在场景变化方面遇到麻烦

时间:2014-04-16 17:10:33

标签: events lua storyboard corona listeners

添加错误的故事板项目链接

项目 - http://forums.coronalabs.com/index.php?app=core&module=attach&section=attach&attach_id=2062

原始主题 - http://forums.coronalabs.com/topic/46884-scene-change-pleeeeease-help/

我尝试过很多次改变场景,它要么让玩家出现故障并在任何地方四处移动,要么我试图运用forceforce(等等等等等等)或者说是一个零值代理,以及所有其他疯狂的东西。为了让我们俩都更加简单,我已经发送了代码而没有改变场景,因为它完全是一团糟。我已经试图这样做了几个星期,但不能正确。我知道感觉我没有做任何事情,只要问你看起来像是不可能的代码,但我无法在没有大量错误的情况下改变现场。

请告诉我要执行的代码:

local function onCollision( event )
  if event.phase == "began" then
    -- go to nex​t lua/scene completely obliterating this one as 
    -- if I'm opening a whole new game
  end
  return true
end

如果您真的不想为我做代码,我愿意为此付费

我只是想通过这个......请

display.setStatusBar( display.HiddenStatusBar )

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

physics.setGravity( 0, 15 ) -- set x & y gravity to 0

local background = display.newImage( "background.jpg" )
background.x = display.contentCenterX
background.y = display.contentCenterY

local ground = display.newImage( "ground.png" )
ground.x = display.contentCenterX
ground.y = 480
physics.addBody( ground, "static", {density=1, friction=1, bounce=0.3 } )

local flag = display.newImage( "flag.png" )
flag.x = display.contentCenterX
flag.y = 50
physics.addBody( flag, "static", {density=0, friction=1, bounce=0.3 } )

local player = display.newImage( "player.png" )
player.x = display.contentCenterX
player.y = 425
physics.addBody( player, {density=0, friction=1, bounce=0.3 } )
player.isFixedRotation = true

system.setAccelerometerInterval( 100.0 )

local tiltSpeed = 2

local function onTilt( event )
        movementX = tiltSpeed * event.xGravity
        player.x = player.x + movementX
                -- prevent player from moving offscreen
        if player.x <= 30 then 
                player.x = 30 
        elseif player.x >= 310 then
                player.x = 310
        end
end

local function onCollision( event )
  if event.phase == "began" then
    -- go to next lua/scene completely obliterating this one as if I'm opening a whole new game
  end
  return true
end

local function onScreenTouch( event )
    if event.phase == "began" then
        -- make player jump
        player:applyForce( 0, -4, player.x, player.y )
    end
end

Runtime:addEventListener( "accelerometer", onTilt )
Runtime:addEventListener( "touch", onScreenTouch )
flag:addEventListener("collision", onCollision )

2 个答案:

答案 0 :(得分:0)

在碰撞过程中无法离开当前场景。你可以做的是,在碰撞后1毫秒使定时器调用一个函数。 请尝试使用此代码来代替碰撞事件检测器:

local function onCollision( event )
    if event.phase == "began" then
       function(sceneChange)
           storyboard.gotoScene(Your next scene here")
       end
       timer.performWithDelay(1,sceneChange,1)
    end
    return true
end

&#39;希望有所帮助!继续编码!

答案 1 :(得分:0)

只需创建一个更改进行到下一个场景的函数

local function changeScene()
  composer.gotoScene("nextScene")
end

local function onCollision( event )
    if event.phase == "began" then
     changeScene()
    end
    return true
end

我建议使用composer api比故事板api更新。

作为建议,删除所有已调用的事件监视器,尤其是那些运行时事件监视器。即使你改变了场景,那个事件仍会触发,因此它会给你和错误,因为它引用它被删除了。经验法则,“如果您不需要它,请将其删除。”