2011-10-09 114 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子彈:)

讓我知道如果你還有這方面的問題。