刚刚开始玩弄真棒电晕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