4

ジャイロスコープを使用してゲームを作成しようとしていますが、非常に奇妙な問題に直面しています。次の2つのシーンがあります

ここに画像の説明を入力

ここに画像の説明を入力

最初のシーンでボールがドアにぶつかったときに、2 番目のシーンに移動したいです。ボールがゆっくりと動いてドアに衝突すると、すべてが正常に機能し、次のシーンが正常に開始されますが、最初のシーンのボールが非常に速く移動し、大きな力でドアに衝突すると、次のシーンが霜で覆われ始めます。イベントはトリガーされません。何か案が?

私のコード:

シーン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

シーン 2:

---------------------------------------------------------------------------------
--
-- 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

2 番目のファイルには、不気味な何かがあるに違いない

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

コードは適切だと思います。ストーリーボード シーンのエンター シーンとメモリ リロードで物事をグループ化する際に問題があります。以下の解決策を試してください。

  1. シーンの作成でコード全体を記述したと思いますが、これはストーリーボード シーンに対して 1 回呼び出されます。enterScene に再利用可能なコードを実装するようにし、シーン オブジェクトに入るには local group = self.view を使用する必要があります。

  2. ボールとの DOOR 衝突時、または現在のストーリーボードの終了シーンから、現在のストーリーボード シーンのメモリを更新または解放する必要があります。

于 2014-01-21T06:39:44.463 に答える