4

我正在尝试使用陀螺仪创建游戏,但遇到了一个非常奇怪的问题。我有以下两个场景

在此处输入图像描述

在此处输入图像描述

我想在第一个场景中的球与门相撞时移动到第二个场景。当球移动缓慢并撞到门时一切正常,下一个场景开始正常,但如果第一个场景中的球移动得非常快并且用很大的力量撞到门,下一个场景开始结霜,就像进入场景一样事件未触发。任何想法?

我的代码:

场景1:

---------------------------------------------------------------------------------
--
-- scene1.lua
--
---------------------------------------------------------------------------------

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

system.setIdleTimer( false )

local physics = require "physics"
local physicsData = (require "myphysics").physicsData(1.0)
---------------------------------------------------------------------------------
-- BEGINNING OF  IMPLEMENTATION
---------------------------------------------------------------------------------
local displayTime,background,ball,maze,maze2,borders,exitscn
local startTime=0
local levelTime = 20

local function onGyroscopeDataReceived( event )
    local deltaRadiansX = event.xRotation * event.deltaTime
    local deltaDegreesX = deltaRadiansX * (180 / math.pi)
    local deltaRadiansY = event.yRotation * event.deltaTime
    local deltaDegreesY = deltaRadiansY * (180 / math.pi)
    ball:applyForce( -deltaDegreesX*6, -deltaDegreesY*6, ball.x, ball.y )
end



function nextScene()
    storyboard.gotoScene( "loadscene2")
end

local function onCollision( event )
    if ( event.phase == "ended" ) then
       if(event.object1.name=="exitscn" or event.object2.name=="exitscn") then
            timer.performWithDelay ( 500, nextScene )
        end 
    end

end

local function gameOver()
    storyboard.gotoScene( "menu", "fade", 300)
end

local function checkTime(event)
  local now = os.time()
  displayTime.text = levelTime - (now - startTime)
  if ( levelTime - (now - startTime)==0) then
    gameOver()
  end
end





-- Called when the scene's view does not exist:
function scene:createScene( event )
    local screenGroup = self.view
    physics.start(); 
    physics.setGravity( 0,0 )

    displayTime = display.newText(levelTime, 0, 0, "Helvetica", 20)
    displayTime.isVisible=false

    background=display.newImage("bcklevel1.png")
    background.x=display.contentCenterX
    background.y=display.contentCenterY

    ball=display.newImage("ball1.png")
    ball.x=30
    ball.y=display.contentCenterY
    ball.name="ball"

    maze=display.newImage( "maze1.png" )
    maze.x=display.contentCenterX
    maze.y=display.contentCenterY
    maze.name="maze"

    maze2=display.newImage( "maze1.png" )
    maze2.x=display.contentCenterX
    maze2.y=display.contentCenterY
    maze2.name="maze2"

    borders=display.newImage( "borders.png" )
    borders.x=display.contentCenterX
    borders.y=display.contentCenterY
    borders.name="borders"
    borders.alpha=0.1

    exitscn=display.newImage("exit.png")
    exitscn.x=display.contentWidth-30
    exitscn.y=display.contentCenterY
    exitscn.name="exitscn"

    physics.addBody (ball, "dynamic",physicsData:get("ball"))
    physics.addBody (maze, "static",physicsData:get("mazelevel1_1"))
    physics.addBody (maze2, "static",physicsData:get("mazelevel1_2"))
    physics.addBody (borders, "static",physicsData:get("borders"))
    physics.addBody (exitscn, "dynamic",physicsData:get("exitscn"))

    --ball:addEventListener ( "touch", nextScene )
    Runtime:addEventListener("enterFrame", checkTime)
    Runtime:addEventListener( "gyroscope", onGyroscopeDataReceived )
    Runtime:addEventListener( "collision", onCollision )

    screenGroup:insert( background )
    screenGroup:insert(displayTime)
    screenGroup:insert( ball )
    screenGroup:insert( maze )
    screenGroup:insert( maze2 )
    screenGroup:insert( borders )
    screenGroup:insert( exitscn )


    print( "\n1: createScene event")
end




-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )

    print( "1: enterScene event" )
    physics.start()

    startTime = os.time()
    displayTime.isVisible=true

end


-- Called when scene is about to move offscreen:
function scene:exitScene( event )

    print( "1: exitScene event" )
    physics.stop( )


    Runtime:removeEventListener( "enterFrame", checkTime )
    Runtime:removeEventListener( "gyroscope", onGyroscopeDataReceived )
    Runtime:removeEventListener( "collision", onCollision )

end


-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )

    print( "((destroying scene 1's view))" )
    package.loaded[physics] = nil
    physics = nil
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 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

场景二:

---------------------------------------------------------------------------------
--
-- scene2.lua
--
---------------------------------------------------------------------------------

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

system.setIdleTimer( false )

local physics = require "physics"
local physicsData = (require "myphysics").physicsData(1.0)
---------------------------------------------------------------------------------
-- BEGINNING OF  IMPLEMENTATION
---------------------------------------------------------------------------------
local displayTime,background,ball,maze,maze2,borders,exitscn
local startTime=0
local levelTime = 20

local function onGyroscopeDataReceived( event )
    local deltaRadiansX = event.xRotation * event.deltaTime
    local deltaDegreesX = deltaRadiansX * (180 / math.pi)
    local deltaRadiansY = event.yRotation * event.deltaTime
    local deltaDegreesY = deltaRadiansY * (180 / math.pi)
    ball:applyForce( -deltaDegreesX*6, -deltaDegreesY*6, ball.x, ball.y )
end

function nextScene()
    storyboard.gotoScene( "menu", "fade", 1000 )
end

local function onCollision( event )
    if ( event.phase == "ended" ) then
       if(event.object1.name=="exitscn" or event.object2.name=="exitscn") then
            timer.performWithDelay ( 500, nextScene )
        end 
    end

end

 local function gameOver()
    storyboard.gotoScene( "menu", "fade", 300)
end

local function checkTime(event)
  local now = os.time()
  displayTime.text = levelTime - (now - startTime)
  if ( levelTime - (now - startTime)==0) then
    gameOver()
  end
end





-- Called when the scene's view does not exist:
function scene:createScene( event )
    local screenGroup = self.view
    physics.start(); 
    physics.setGravity( 0,0 )

    displayTime = display.newText(startTime, 0, 0, "Helvetica", 20)
    displayTime.isVisible=false

    background=display.newImage("bcklevel1.png")
    background.x=display.contentCenterX
    background.y=display.contentCenterY

    ball=display.newImage("ball1.png")
    ball.x=30
    ball.y=display.contentCenterY
    ball.name="ball"

    maze=display.newImage( "maze2.png" )
    maze.x=display.contentCenterX
    maze.y=display.contentCenterY
    maze.name="maze"

    maze2=display.newImage( "maze2.png" )
    maze2.x=display.contentCenterX
    maze2.y=display.contentCenterY
    maze2.name="maze2"

    borders=display.newImage( "borders.png" )
    borders.x=display.contentCenterX
    borders.y=display.contentCenterY
    borders.name="borders"
    borders.alpha=0.1

    exitscn=display.newImage("exit.png")
    exitscn.x=display.contentWidth-30
    exitscn.y=display.contentCenterY
    exitscn.name="exitscn"

    physics.addBody (ball, "dynamic",physicsData:get("ball"))
    physics.addBody (maze, "static",physicsData:get("mazelevel2_1"))
    physics.addBody (maze2, "static",physicsData:get("mazelevel2_2"))
    physics.addBody (borders, "static",physicsData:get("borders"))
    physics.addBody (exitscn, "dynamic",physicsData:get("exitscn"))

    --ball:addEventListener ( "touch", nextScene )
    Runtime:addEventListener("enterFrame", checkTime)
    Runtime:addEventListener( "gyroscope", onGyroscopeDataReceived )
    Runtime:addEventListener( "collision", onCollision )

    screenGroup:insert( background )
    screenGroup:insert(displayTime)
    screenGroup:insert( ball )
    screenGroup:insert( maze )
    screenGroup:insert( maze2 )
    screenGroup:insert( borders )
    screenGroup:insert( exitscn )


    print( "\n1: createScene event")
end




-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )

    print( "1: enterScene event" )
    physics.start()
    startTime = os.time()
    displayTime.isVisible=true

end


-- Called when scene is about to move offscreen:
function scene:exitScene( event )

    print( "1: exitScene event" )
    physics.stop( )

    Runtime:removeEventListener( "enterFrame", checkTime )
    Runtime:removeEventListener( "gyroscope", onGyroscopeDataReceived )
    Runtime:removeEventListener( "collision", onCollision )

end


-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )

    print( "((destroying scene 2's view))" )
    package.loaded[physics] = nil
    physics = nil
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 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
4

4 回答 4

0

我认为代码是正确的。在进入场景和情节提要场景的内存重新加载中存在一些问题。请尝试以下解决方案:

  1. 我认为您在创建场景中编写了整个代码,这将每次调用一次故事板场景。您应该尝试在 enterScene 中实现可重用代码,并且应该使用 local group = self.view 进入场景对象。

  2. 您应该尝试在 DOOR 与球碰撞时或从当前故事板的退出场景中刷新或释放当前故事板场景的内存。

于 2014-01-21T06:39:44.463 回答
0

在第二个文件中,我敢打赌有一些令人毛骨悚然的东西

function nextScene()
    storyboard.gotoScene( "menu", "fade", 1000 )
end

local function onCollision( event )
    if ( event.phase == "ended" ) then
       if(event.object1.name=="exitscn" or event.object2.name=="exitscn") then
            timer.performWithDelay ( 500, nextScene )
        end 
    end

end

首先延迟 500 毫秒,然后再延迟 1000 毫秒过渡到下一个场景。也许您知道这一点,但您在此处处理此转换的方式与在第一个文件中的方式不同。

于 2013-07-07T20:30:09.313 回答
0

我认为问题在于,当您延迟调用 nextScene 时,碰撞事件会被多次触发。

尝试做这样的事情:

local hasNextSceneEventHappened = false

local function onCollision( event )
    if ( event.phase == "ended" ) then
       -- Ignore the call if we have already collided for next level
       if(event.object1.name=="exitscn" or event.object2.name=="exitscn") then
            if (hasNextSceneEventHappened == true ) then return end
            hasNextSceneEventHappened = true
            timer.performWithDelay ( 500, nextScene )
       end 
    end
end

此代码将确保您不会向 goToScene() 函数发送垃圾邮件并获得不希望的行为。我希望这个对你有用。

于 2014-04-08T18:58:45.917 回答
0

使用像didGo. 在您的文件主体上初始化此标志false并编写如下内容:

-- body of your file:
local didGo = false

-- when you want to go to another scene
if ( not didGo) then
 didGo = true
 storyboard.gotoScene...
end
于 2015-05-22T07:21:30.540 回答