so im trying have enemy fire randomly, when bullet goes off screen signal enemy , allow enemy fire again. every instance of enemy can have 1 active instance of bullet @ time. far testing fire , refire implementation. function shoot called of instance of enemy following:
function enemy:shoot() --calls bullet obj file local bullet = require "bullet"; --movement time per space local defaulttime = 5; --checking if property, there active bullet instance in scene if self.activebul ==false --move bullet self.bullet:trajectbullet({x=self.sprite.x,y=display.contentheight, time = defaulttime* (display.contentheight-self.sprite.y)}); --there active bullet linked enemy self.activebul = true; else end end
all happening in trajectbullet movement implementation. im trying figure out how able let linked enemy instance know bullet off screen. im new lua , corona sdk im still getting grasp on how best process things please bear me simple outline of im looking below
--random enemy fire --move bullet location on top of enemy(appears enemy fires) --makes linked bullet visible --simulates trajectory case:* doesn't hit , goes off screen* --hides bullet --signals linked enemy can fire again(changing activebul false)
couple things keep in mind, have both bullet , enemy metatables. bullet instance created same time enemy created. the enemy never creates multiple bullet instances, hides , relocates firing.
im looking insight on how should go getting work properly, advice appreciated
if know when bullet off screen, can use 2 approachs: 1. have reference enemy on bullet, can call funcion on "recicle" bullet; 2. create custom event dispatched bullet when off screen.
i stick 2), because of flexibility.
you can find more here:
- http://docs.coronalabs.com/api/type/eventlistener/dispatchevent.html
- http://docs.coronalabs.com/api/type/eventlistener/addeventlistener.html
-- 1) function enemy:shoot() local bullet = require "bullet"; bullet.owner = self; -- same previous code end; function enemy:canshootagain() self.activebul = false; end; -- when want enemy shoot again, call canshootagain bullet bullet.owner:canshootagain();
-- 2) function enemy:shoot() local bullet = require "bullet"; bullet.owner = self; -- same previous code end; function enemy:canshootagain() self.activebul = false; end; -- when want enemy shoot again, call canshootagain bullet bullet.owner:dispatchevent({name = 'canshootagain'}); -- add end of enemy construction enemy:addeventlistener('canshootagain', enemy);
Comments
Post a Comment