Adding distance to my projectiles


(Nate ) #1

Hey guys this is my shoot function in my projectile class! I was just wondering what the best way to go about adding a distance to my fired projectiles would be.

public function shoot(x:Number,y:Number, facing:int, kind:int):Projectile
	{
		var p:Projectile = new Projectile;
		p.x = x;
		p.y = y;
		p.direction = facing;
		p.projectileSprite.frame = kind;
		FP.world.add(p);
		return p;
		
	
	}

Thanks!


(Zachary Lewis) #2

What do you mean by “adding a distance to my fired projectiles?” Do you mean tracking the distance they’ve traveled, or only allowing them to travel so far?


(Nate ) #3

Sorry I should clarify, yes I would like to track the distance traveled, so once the projectile travels too far it disappears :smiley:


(Zachary Lewis) #4

There are a few ways to do this. If you just want a projectile to have a lifespan (i.e., it exists for 3 seconds then disappears), you can make a simple timer in Projectile.update() or use an Alarm.

If you desire a distance, you’ll need to know both where the Projectile started and where it currently is. Since you already know where it is (Projectile.x and Projectile.y), you just need to keep track of where it started.

public class Projectile extends Entity
{
  /** The initial x-location of the Projectile. */
  protected var _initialX:Number;

  /** The initial y-location of the Projectile. */
  protected var _initialY:Number;

  /** The distance the Projectile may travel. */
  protected const MAX_DISTANCE:Number = 100;
  
  public function Projectile(initialX:Number, initialY:Number)
  {
    // Store the initial location.
    _initialX = initialX;
    _initialY = initialY;

    // Perform any other initialization during construction.

    // The x and y location will be set by Entity's constructor via super().
    super(initialX, initialY);
  }

  override public function update():void
  {
    // Perform any movement and collision code required by the Projectile.

    // Check the distance traveled.
    if (FP.distance(_initialX, _initialY, x, y) >= MAX_DISTANCE)
    {
      // The Projectile has traveled its maximum distance.
      destroy();
    }
  }

  public function destroy():void
  {
    // Perform any destruction tasks, like playing a sound or explosion.
  }
}

With a setup like this, you can simplify your shoot function slightly.

public function shoot(x:Number,y:Number, facing:int, kind:int):Projectile
{
  var p:Projectile = new Projectile(x, y);
  p.direction = facing;
  p.projectileSprite.frame = kind;
  FP.world.add(p);
  return p;
}

(Nate ) #5

Worked beautifully! Thank you Zach! :smiley:


(JP Mortiboys) #6

That shoot function looks like it should be static; it’s creating an entity and doesn’t require an instance itself to function.


(Nate ) #7

I did originally have the shoot function a public static function, but then it was giving me errors whenever I would call it. So making it just a public function allowed it to work,

I am sure there is some very fundamental reason behind why I couldn’t get the public static function to call correctly, but I just went with what would work.

:grin:

However, for my own edification, when I do make it a public static function, it highlights my function call and says Call to a possibly undefined method shoot through a reference with static type entities:Projectile.

These are the two lines involved with calling the projectile:

var newProjectile:Projectile = new Projectile(0,0);
						newProjectile.shoot(theZomb.trackX, theZomb.trackY, theZomb.direction, number);

It is highlighting shoot and giving me the above error. Like I said this is working fine having it a public function, but I am still curious as to why I can’t go public static function. Would newProjectile have to be declared as a public static var?


(JP Mortiboys) #8

To use it as a public static function, you call it via the class name, not the instance variable, like this:

var newProjectile:Projectile = Projectile.shoot(theZomb.trackX, theZomb.trackY, theZomb.direction, number);

Notice how in your code you call new twice - once here:

var newProjectile:Projectile = new Projectile(0,0);

and once again here inside shoot:

var p:Projectile = new Projectile(x, y);

Using the static method means there’s only one new call, which is much better.

Once you’ve got this nailed you can look into using create/recycle instead; this will also work much better with the static method.


(Nate ) #9

Thank you very much Raptor! I will change that stuff over!

I am yet to get create working :x I have had recycle working but never create everytime I do I get errors D:


(Abel Toy) #10

Unless you really need to display the distance to the user, for comparing distances and such I usually use the squared distance, makes it way faster.

(x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)

And then, MAX_DISTANCE would be 100 * 100.