2013-05-06 18 views
1

所以我試圖做的是隨機有一個敵人的火,但是當子彈熄滅屏幕向敵人發出信號並讓敵人再次發射時。敵人的每個實例在任何時候都只能有一個活動的Bullet實例。到目前爲止,我只是測試火災並重新實施。該功能拍攝被稱爲敵人的類似下面的實例:子彈火災重置事件電暈SDK

function enemy:shoot() 
    --Calls Bullet Obj file 
    local Bullet = require "Bullet"; 

    --Movement time per space 
    local DEFAULTTIME = 5; 

    --checking if property, there currently is an active bullet instance in scene 
    if self.activeBul ==false then 
      --move the bullet 
      self.bullet:trajectBullet({x=self.sprite.x,y=display.contentHeight, time = DEFAULTTIME* (display.contentHeight-self.sprite.y)}); 

      --there is now an active Bullet linked to this enemy 
      self.activeBul = true; 

    else 

    end 
end 

一切事情現在在trajectBullet是運動實現。我試圖找出現在我怎麼能夠讓聯繫的敵人實例知道它的子彈是在屏幕外。林相當新的Lua和科羅娜SDK這樣的IM仍然得到關於如何最好地處理事物的把握,所以請多多包涵的即時尋找下面是什麼

--random enemy fire 
--move Bullet location on top of enemy(appears enemy fires) 
--makes linked bullet visible 
--simulates trajectory 
    CASE:* doesn't hit anything and goes off screen* 
    --hides Bullet 
    --signals to linked enemy it can fire again(changing activeBul to false) 

兩件事情簡單的輪廓要記住,我有子彈和敵人作爲metatables。此外,子彈實例也是在敵人創建的同時創建的。所以敵人永遠不會創建多個子彈實例,只隱藏和重新定位它的射擊。

我真的想找的洞察力,我應該如何去得到這個正常工作,任何意見可以理解

+0

ÿ當敵人射擊時,不應該要求Bullet.lua文件。敵方metatable應該包含對每個實例利用的Bullet類的引用。 – HennyH 2013-05-06 07:47:19

回答

0

如果你已經知道子彈是關閉屏幕,你可以使用兩個技術途徑: 1。爲他的子彈上的敵人提供參考,所以你可以調用它的功能,以「recicle」子彈; 2.創建一個自定義事件,當它處於屏幕外時,將爲該子彈調度。

我會堅持2),因爲靈活性。

你可以找到更多關於它在這裏:


-- 1) 
function enemy:shoot() 
    local Bullet = require "Bullet"; 
    Bullet.owner = self; 
    -- the same as your previous code 
end; 

function enemy:canShootAgain() 
    self.activeBul = false; 
end; 

-- When you want the enemy to shoot again, just call canShootAgain from bullet 
bullet.owner:canShootAgain(); 

-- 2) 
function enemy:shoot() 
    local Bullet = require "Bullet"; 
    Bullet.owner = self; 
    -- the same as your previous code 
end; 

function enemy:canShootAgain() 
    self.activeBul = false; 
end; 

-- When you want the enemy to shoot again, just call canShootAgain from bullet 
bullet.owner:dispatchEvent({name = 'canShootAgain'}); 

-- Add this to the end of your enemy construction 
enemy:addEventListener('canShootAgain', enemy); 
+0

好吧,我正在嘗試做2)就像你提到的,但我有調整事件本身activeBul屬性的問題,以及能夠簡單地調用一個函數來更改適當的實例的activeBul屬性。我想這個問題的主旨是在我嘗試調整的對象與觸發事件之間進行通信時出現問題。 – 2013-05-06 21:05:22

+0

嗨。我添加了一些示例代碼,舉例說明如何實現這兩種方式。 – Gardner 2013-05-07 01:42:46

+0

非常感謝的例子,我會嘗試實現這些現在! – 2013-05-07 06:20:35