1
votes

So what Im trying to do is have an enemy fire randomly, but when the bullet goes off screen to signal to the enemy and allow the enemy to fire again. Every instance of an Enemy can only have One active instance of Bullet at any time. So far I am simply testing the fire and refire implementation. The function shoot is called of the instance of Enemy like the following:

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

all that is happening for now in the trajectBullet is the movement implementation. Im trying to figure out now how I would be able to let the linked enemy instance know its bullet is off screen. Im fairly new to lua and Corona SDK so im still getting a grasp on how best to process things so please bear with me a simple outline of what im looking for is below

--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)

Couple things to keep in mind, I have both Bullet and Enemy as metatables. Also the Bullet instance is also created the same time the enemy is created. So the the enemy never creates multiple Bullet instances, just hides and relocates it firing.

Im really looking for insight On how I should go about getting this to work properly, any advice will be appreciated

1
You shouldn't be require-ing the Bullet.lua file when the enemy shoots. The enemy metatable should contain a reference to the Bullet class, which each instance leverages.HennyH

1 Answers

0
votes

If you already know when the bullet is off screen, you can use two approachs: 1. Have a reference for the enemy on his bullet, so you can call a funcion on it so "recicle" the bullet; 2. Create a custom event dispatched for the bullet when it is off screen.

I would stick with the 2), because of flexibility.

You can find more about it here:


-- 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);