剛剛開始玩弄真棒電暈sdk。如何使用Corona SDK有效地處理對象的對象刪除
我開始構建一個簡單的射擊遊戲。
我有以下代碼:
-- Global Variables
local shot = audio.loadSound('shot.mp3')
local bg = display.newImage('bg.png')
local shoot = {}
local Main = {}
local Init = {}
local bullets = display.newGroup()
function update()
if(bullets.numChildren ~= 0) then
for i = 1, bullets.numChildren do
bullets[i].y = bullets[i].y - 8
-- Destroy Offstage Bullets
if(bullets[i].y < (-bullets[i].height-5)) then
-- bullets[i]:removeSelf()
bullets:remove(bullets[i])
display.remove(bullets[i])
return
end
end
end
end
-- Initialisation functions
function Init()
display.setStatusBar(display.HiddenStatusBar)
local movieclip = require('movieclip')
local physics = require('physics')
physics.start()
physics.setGravity(0, 0)
end
function shoot:tap(e)
for i = 1, 15 do
local bullet = display.newImage('bullet.png')
bullet.x = 150
bullet.y = 470
bullet.name = 'bullet'
physics.addBody(bullet)
bullets.insert(bullets, bullet)
end
audio.play(shot)
end
-- Main routine
function Main()
Init()
bg:addEventListener('tap', shoot)
Runtime:addEventListener('enterFrame', update)
end
Main()
現在它 '作品';但是當子彈出現在屏幕上時,整個「遊戲」變慢,我可以清楚地看到每顆子彈都被移除了,這會降低遊戲速度。
也許我做得不對;還嘗試了:removeSelf()函數;相同的結果。
爲什麼 - 子彈[我]:removeSelf()註釋掉了,這似乎是錯誤的。你能否在子彈撞到邊緣之前儘量去除子彈,以便檢查它們是否被移除。 – tomdemuyt
removeSelf應該可以正常工作。你能上傳一個示例項目,以便我們可以輕鬆測試嗎? – Vitaly