【问题标题】:How do transition scenes?如何过渡场景?
【发布时间】:2014-09-26 04:40:23
【问题描述】:

我正在构建一个游戏,并且正在使用情节提要。在 enterScene 上,我从另一个模块调用我的显示对象 lilEnem。当我改变场景时,我无法将他从记忆中删除,而且我不知道如何将他插入到 scene.view 中,因为它是一个单独的文件。

function level1:enterScene()
local level1Group = level1.view
print( "enteredScene" )

local function goHome(event)
    storyboard.removeAll( )
    storyboard.gotoScene( "mainMenu" )
end

local function pauseUi (event)
    local pauseButton = display.newImage("assets/graphics/gameplay/UI/pause/pause.png", display.contentWidth- 25, 25)

    pauseButton:addEventListener( "tap", goHome )

    level1Group:insert( pauseButton )
end

pauseUi()
--Load and play game song when entered scene
--
--
gameAudio.gamePlay()

-- Spawns lilEnems
--
--
local function spawnLilEnem (event)
        -- Checking myData module for lilEnem ID
        --Storing in local id
        --
        --
        local id = myData.lilEnemId

        --lil Enem will spawn in time math.random gives
        --
        --
        --
        local randomSpawnTime = math.random(0,5000)


        --Calls spawnEnem Module
        --
        --
        lilEnem.spawnEnem(id)

        --Adds 1 to id to give each enem unique id
        --
        --
        myData.lilEnemId = myData.lilEnemId + 1

        --timer will call this function at random times
        --
        --
        local spawnEnemTimer = timer.performWithDelay( randomSpawnTime, spawnLilEnem, 1 )

        --When id reachs == number it will stop spawning enems
        --Number is the highest numbered lil Enem ID
        --This statement decides how many lilEnems spawn
        --
        --
        if id == 5 then
            timer.cancel( spawnEnemTimer )
        end
end
spawnLilEnem()

--Call score from scoreData module
--score = 0 in scoreDate module
--
--
local score = display.newText(scoreData.score, display.contentWidth/2, 50, nil, 30)

结束

【问题讨论】:

    标签: lua storyboard coronasdk


    【解决方案1】:

    首先,您需要使用 Composer,因为 Storyboard 将被弃用。有一个转换页面...不是很困难:

    Storyboard to Composer Migration

    当您调用 composer.gotoScene(或 Storyboard)时,您可以将参数传递给该场景:

     (main.lua)
    
     storyboard.gotoScene("MyScene", 
      { 
         params = 
            { 
               param1 = "something", 
               player = playerObj, etc. 
            } 
      }
    

    然后,在被调用的场景中,在“show”方法中,你可以读取那些参数(这段代码使用了composer):

    (MyScene.lua)
    
    local scene = composer.newScene()
    
    function scene:show(event)
      if event.phase == "did" then 
         if event.params then
           something = event.params.something
           player = event.params.player 
         end
      end
    end
    
    scene:addEventListener("show", scene)
    
    return scene
    

    【讨论】:

      【解决方案2】:

      要在离开场景时移除项目,请使用exitScene() 方法:移除事件监听器、停止动画、暂停物理、移除对显示对象的引用您不再需要的对象,等等。

      为了插入单独的视图,场景和其他 Lua 模块一样,所以你可以调用模块的函数来进行设置:

      -- yourGotoScene.lua
      local value -- hide it from other modules
      
      function setValue(newValue)
          value = newValue
          print('setting value to new:', value)
      end
      
      -- yourCallingScene.lua
      ...
      require 'yourGotoScene'
      yourGotoScene.setValue(123)
      storyboard.gotoScene('yourGotoScene')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多