2014-09-26 85 views
0

我正在構建一個遊戲,並正在使用故事板。在enterScene上,我從另一個模塊調用顯示對象lilEnem。當我改變場景時,我無法將他從memeory中刪除,並且我不知道如何將他插入到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) 

回答

0

首先,你需要使用作曲家故事板將被棄用。有一個轉換頁面......這不是很困難:

Storyboard to Composer Migration

當你調用composer.gotoScene(或故事板),您可以通過PARAMS到的那一幕:

(main.lua) 

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

然後,在被叫場景,在「秀」的方法,你可以閱讀這些PARAMS(該代碼使用的是作曲家):

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

要刪除某個項目時離開現場時,使用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')