【问题标题】:Corona SDK - Removing objects on changing storyboardCorona SDK - 在改变故事板上删除对象
【发布时间】:2014-03-18 20:13:12
【问题描述】:

我是 lua 编程的初学者,在更改情节提要后删除对象时遇到问题。我想删除对象:zly、dobry 和 cewka,当新的故事板(restart.lua)将启动时。你能帮我吗,我不知道该怎么做。在此先感谢:)

    local storyboard = require ("storyboard")

local scene = storyboard.newScene()


local physics = require "physics"
physics.start()
physics.setGravity(0, 3)

local ego = require "ego"
local saveFile = ego.saveFile
local loadFile = ego.loadFile

motionx = 0; 
speed = 7; 

local ego = require "ego"
local saveFile = ego.saveFile
local loadFile = ego.loadFile

local newestHighScore = loadFile( "filename.txt", highScore )
local newestHighScoreValue = tonumber(newestHighScore) 

local playerScore = 0
local highScore = newestHighScore

local zycie = 3

function scene:createScene(event)

local screenGroup = self.view

tlo = display.newImage("t3.png")
     tlo.x = 250
     tlo.y =150
     screenGroup:insert(tlo)

gui_PlayerScore = display.newText("Wynik: ",-10,21,native.SystemFontBold,18)
      gui_PlayerScore:setFillColor(255,255,255,10)
      screenGroup:insert(gui_PlayerScore)
 gui_Score = display.newText(""..playerScore,gui_PlayerScore.x +45,21,native.SystemFontBold,18)
      gui_Score:setFillColor(255,255,255,10)
      screenGroup:insert(gui_Score)
zycia = display.newText("Życia: ",460,21,native.SystemFontBold,18)
      zycia:setFillColor(255,255,255,10)
      screenGroup:insert(zycia)
zyc = display.newText(""..zycie,zycia.x +45,21,native.SystemFontBold,18)
      zycia:setFillColor(255,255,255,10)
      screenGroup:insert(zyc)
gui_Best = display.newText("Najlepszy:",2, 40,native.SystemFont,18)
      gui_Best:setFillColor(255,255,255,10)
screenGroup:insert(gui_Best)
gui_highScore = display.newText(""..highScore,gui_Best.x + 65,40,native.SystemFont,18)
      gui_highScore:setFillColor(255,255,255,10)
      screenGroup:insert(gui_highScore)
left = display.newImage ("left.png")
     left.x = 0; left.y = 280;
 screenGroup:insert(left)
 right = display.newImage ("right.png")
     right.x = 480; right.y = 282;
 screenGroup:insert(right)
 tesla = display.newImage("teslamaly.png")
     tesla.x = 250
     tesla.y = 300
     physics.addBody( tesla, "static", { filter = {maskBits = 2, categoryBits = 2} } )
     screenGroup:insert(tesla)


end

local function networkListener( event )
        if ( event.isError ) then
                print( "Network error!")
        else
                print ( "RESPONSE: " .. event.response )
        end
end

function reset(event)
    if event.phase == "began" then
        storyboard.gotoScene("start", "fade", 500)
    end
end

function lefttouch()
 motionx = -speed;
 end


 function righttouch()
 motionx = speed;
 end


 local function movetesla (event)
 tesla.x = tesla.x + motionx;
 if tesla.x >440 then
 motionx =0;
 end
 if tesla.x < 25 then
 motionx = 0;
 end
 end


 local function stop (event)
 if event.phase =="ended" then
 motionx = 0;
 end
 end


 spawnTrue = true;

zly = {}
i = 1
local function getCoins(event)

    if event.phase == "ended" then    
    backgroundMusic = audio.loadStream("zly.wav")
    backgroundMusicChannel = audio.play( backgroundMusic, { channel=2, loops=0}  )
    coin = event.target
    coin:removeSelf()
    coin = nil
    playerScore = playerScore + 1
    gui_Score.text = "" .. playerScore

    if playerScore > newestHighScoreValue then

    highScore = playerScore


    gui_highScore.text = "" .. highScore

    print ("saving highScore")

    saveFile( "filename.txt", highScore)

end
end

    end


local function spawnStuff( event )
    if spawnTrue == true then
    zly[i] = display.newImage("sad.png")
    zly[i].x = math.random( 0, 320 )
    zly[i].y = -100
    zly[i]:addEventListener( "collision", getCoins )
    physics.addBody(zly[i], {bounce = .2, density = 1, filter = {maskBits = 2, 4, categoryBits = 2, 4}})
    i = i + 1

   return zly[i]
 end

 end

timer.performWithDelay( 1900, spawnStuff, 10000 )












---------------------------------------------------------------------------------------------------
dobry = {}
i = 1
function getDobry(event)
    if event.phase == "ended" then    
    ass = audio.loadStream("dobry.wav")
    ass = audio.play( ass, { channel=3, loops=0}  )
    test = event.target
    test:removeSelf()
    test = nil
    playerScore = playerScore + 5
    gui_Score.text = "" .. playerScore

    end
end

spawnDwbry = true;
function spawnDobry( event )

    if spawnDwbry == true then
    dobry[i] = display.newImage("happy.png")
    dobry[i].x = math.random( 0, 320 )
    physics.addBody(dobry[i], "dynamic")
    dobry[i].y = -100
    dobry[i]:addEventListener( "touch", getDobry )
    i = i + 1
    end
 end

timer.performWithDelay( 15000, spawnDobry, 10000 )
 ------------------------------------------------------------------------------------
cewka = {}
i = 1
function getCewka(event)
    if event.phase == "ended" then    
    cewka1 = audio.loadStream("cewka.wav")
    cewka1 = audio.play( cewka1, { channel=4, loops=0}  )
    test = event.target
    test:removeSelf()
    test = nil
    zycie = zycie + 1
    zyc.text ="" .. zycie
    end
end

dawid = true;
function spawnCewka( event )

    if dawid == true then
    cewka[i] = display.newImage("cewka.png")
    cewka[i].x = math.random( 0, 320 )
    physics.addBody(cewka[i], "dynamic")
    cewka[i].y = -100
    cewka[i]:addEventListener( "touch", getCewka )
    i = i + 1
    end
 end

timer.performWithDelay( 50000, spawnCewka, 10000 )
-----------------------------------------------------------------------------------------------

 liniaspawn = true;

linia = {}
i = 1
function getLinia(event)

    if event.phase == "ended" then    
    obj = event.target
    obj:removeSelf()
    obj = nil

    zycie = zycie - 1
    zyc.text ="" .. zycie

    end

end
function spawnLinie( event )
    if liniaspawn == true then
    linia[i] = display.newImage("linia.png")
    linia[i].x = 50
    linia[i].y = 400
    linia[i]:addEventListener( "collision", getLinia )
    physics.addBody( linia[i], "static", { filter = {maskBits = 2, categoryBits = 2} } )
    i = i + 1

 end
 end
timer.performWithDelay( 1700, spawnLinie, 10000 )




function scene:enterScene(event)
    left:addEventListener("touch",lefttouch)

     right:addEventListener("touch",righttouch)

     Runtime:addEventListener("enterFrame", movetesla)

     Runtime:addEventListener("touch", stop )
    end



function scene:exitScene(event)

    left:removeEventListener("touch", lefttouch)
    right:removeEventListener("touch",righttouch)
    Runtime:removeEventListener("enterFrame", movetesla)
    Runtime:removeEventListener("touch", stop )

 end



function scene:destroyScene(event)

end


scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)

return scene 

【问题讨论】:

    标签: android lua storyboard coronasdk


    【解决方案1】:

    要从场景中移除一个对象(并删除它),您可以使用object:removeSelf()。但是,还有其他几个考虑因素:对于要被垃圾处理的对象,您必须 nil 对它的任何引用;您有存储对显示对象的引用的表,因此必须取消这些表;您必须使这些对象和表格可以从您的其他场景中访问(“重新启动”)。您可以使用全局表:

    main.lua:

    objectsToClearOnRestart = {}
    
    ...
    
    -- add dobry etc to objectsToClearOnRestart 
    objectsToClearOnRestart.dobry = dobry
    objectsToClearOnRestart.zly = zly
    ...
    

    重启.lua:

    -- called by enterScene or such:
    function cleanup()
        for tblName, tbl in pairs(objectsToClearOnRestart) do
            local numObj = #tbl
            for j=numObj,1,-1 do
                tbl[j]:removeSelf() -- remove from scene
                table.remove(tbl)   -- remove from table
            end
        end
    end
    

    如果你不想使用全局变量(应该避免),你可以require 'restart' 并调用一个函数来获取或设置objectsToClearOnRestart。例如:

    main.lua:

    restart = require 'restart'
    
    ...
    
    function createScene(event)
        ...
        -- add dobry table etc to restart.objectsToClearOnRestart 
        -- add table not individual objects so all refs can be nilled in restart
        restart.objectsToClearOnRestart.dobry = dobry
        restart.objectsToClearOnRestart.zly = zly
        ...
    end
    

    重启.lua:

    local M = {} -- module table
    
    M.objectsToClearOnRestart = {}
    
    function M.cleanup()
        ... same as previous example but use M.objectsToClearOnRestart ...
    end
    
    ... other scene based stuff, something calls cleanup() like enterScene() ...
    
    return M
    

    【讨论】:

      【解决方案2】:

      据我所知,dobryzlycewka 是全局表。为了在你离开场景后移除它们,你应该在你的 exitScene() 函数中将它们设置为nil,同时移除你的事件监听器。

          function scene:exitScene( event )
             dobry = nil
             zly = nil
             cewka = nil
          end
      

      【讨论】:

      • 将它们设置为 nil 是不够的,请参阅其他答案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多