2011-10-09 47 views
3

刚刚开始玩弄真棒电晕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()函数;相同的结果。

+1

为什么 - 子弹[我]:removeSelf()注释掉了,这似乎是错误的。你能否在子弹撞到边缘之前尽量去除子弹,以便检查它们是否被移除。 – tomdemuyt

+0

removeSelf应该可以正常工作。你能上传一个示例项目,以便我们可以轻松测试吗? – Vitaly

回答

3

我面临同样的问题......而得到了这个溶液:

您正在使用循环删除对象: 如果您删除在循环对象说:你在索引1中删除的对象,然后对象2点移动到对象1 ...所以当它循环反对它不会检查对象2(bcoz其移动到对象1的地方,你正在检查对象3)

for j = 1, buttonGroup.numChildren do 
    buttonGroup[1]:removeSelf();  --this removes all childrens 
end 


for j = 1, buttonGroup.numChildren do 
    buttonGroup[j]:removeSelf();  --this removes alternative childrens 
end 

我希望它有用

0

我在很久很久的时间里因为移除物体而战斗为我的游戏使用了很多标签视图。我发现的解决方案是在需要删除的所有对象上使用“= nil”,“:removeSelf()”和“.alpha = 0”。如果它们都加入到同一组中,并且其中没有其他内容,那么也可以使用它们,但是由于各种原因,组织在幕后建立的方式并不总是有效。

看来你在那里做的是删除“子弹”的内容,但这只是一个参考,所以在你说每个子弹被删除的时候,它实际上只是从数组中删除 - 对象本身仍然存在并且需要被禁止以防止内存泄漏和应用程序的放慢(您可能会发现每个子弹都会使应用程序更慢,对吗?)

在删除和您之后添加此条件应设置:

if(not(bullet == nil)) then 
    bullet.alpha = 0; 
    bullet:removeSelf(); 
    bullet = nil; 
end 
0

它更容易简单地创建一个表像这样

在你的子弹创建代码
local bulletCache = {} 

然后加入

table.insert(bulletCache, myBulletObject) 

然后在退出代码和/或你的破坏代码说

for i = 1, #bulletCache do 

--[[ 
--Below is not needed but just to keep your code consitant 
    pcall(function() 
     bullet.alpha = 0 
    end) 
--]] 

    pcall(function() 
     bullet:removeSelf() 
    end) 
    pcall(function() 
     bullet = nil 
    end) 
end 
0

首先,从来没有尝试执行任何种类的在GameLoop中循环。它确实降低了游戏的fps,因为它通常需要更多的内存。

现在看来,你只是想在屏幕消失后消灭子弹,并且你也在使用物理,那么为什么你没有利用这些优点呢?

以下是您应遵循的步骤。 (如果您发现任何疑问或疑问,请询问我)

1.在屏幕上方稍微画一点静态物理线。假设y是-20。

local _yLimit = -20 
    local _limitLine = display.newLine(0,_yLimit,display.contentWidth,_yLimit) 
    _limitLine.anchorX = 0 
    _limitLine.anchorY = 0 

2.将所有项目符号添加为物理对象。

3.施加在子弹的底部中心,而不是变形。 [假设子弹具有矩形的物理形状,否则决定形状的平衡点]。

4.检查与“碰撞”事件侦听器的冲突。碰撞

这就是这么简单

5.Destroy子弹:)

让我知道如果你还有这方面的问题。