所以我想做的是让敌人随机开火,但当子弹离开屏幕时,向敌人发出信号,让敌人再次开火。敌人的每个实例在任何时候都只能有一个活动的Bullet实例。到目前为止,我只是简单地测试fire和refire实现。Enemy实例的拍摄函数调用如下:
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和Corona SDK的新手,所以我仍然掌握了如何最好地处理东西,所以请耐心听我说,我正在寻找的东西的简单大纲如下
--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)有几件事要记住,我把Bullet和Enemy都作为metatable。此外,在创建敌人的同时也创建了Bullet实例。所以敌人永远不会创建多个子弹实例,只是隐藏和重新定位它的射击。
我真的在寻找关于我应该如何让它正常工作的洞察力,任何建议都将不胜感激
发布于 2013-05-06 22:54:12
如果你已经知道子弹何时在屏幕外,你可以使用两种方法: 1.在敌人的子弹上有一个引用,这样你就可以在它上面调用一个函数,以便“重述”子弹;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);https://stackoverflow.com/questions/16391401
复制相似问题