Odd Collision Behavior


(David Williams) #1

When you keep firing bullets (spam-click), you can shoot through obstacles. Not really sure what’s going on. [Here][1], you can see what’s happening. (Just keep clicking until you shoot through the platform. Pressing ‘H’ renders hitboxes to make this easier to see.) Here’s the weapon class:

package
{
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Image;
	import flash.utils.ByteArray;
	/**
	 * ...
	 * @author David Williams
	 */
	public class WpnBase extends Entity
	{
		public var bullet:Projectile;
		private var gfx:Image;
		private var ma:Number;
		
		public function WpnBase(_gfx:Image, _bullet:Projectile)
		{
			graphic = gfx = _gfx;
			bullet = _bullet;
		}
		
		public function fire():void
		{
			var angAdj:Number = ma * Math.PI / -180;
			var xAdj:Number = Math.round(13.375 + 15.63 * Math.cos(angAdj) - 9.29 * Math.sin(angAdj));
			var yAdj:Number = Math.round(14.25 + 15.63 * Math.sin(angAdj) + 9.29 * Math.cos(angAdj));
			bullet.x = this.x - 18 + xAdj;
			bullet.y = this.y - 18 + yAdj;
			
			var proj:Projectile = (FP.world.add(copy(bullet)) as Projectile);
			proj.shoot(ma);
		}
		
		override public function update():void
		{
			ma = FP.angle(this.x, this.y, world.mouseX, world.mouseY);
			gfx.angle = ma;
		}
		
		public function copy(_bul:Projectile):Projectile
		{
			var p:Projectile = new Projectile(_bul.damage);
			p.graphic = _bul.graphic;
			p.x = _bul.x;
			p.y = _bul.y;
			p.mask = _bul.mask;
			
			return p;
		}
	}

}

And here’s the projectile class:

package  
{
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	
	/**
	 * ...
	 * @author David Williams
	 */
	public class Projectile extends Entity 
	{
		public var damage:Number;
		private var angle:Number;
		private var xConst:Number = 0;
		private var yConst:Number = 0;
		
		public function Projectile(_dam:Number = 1) 
		{
			damage = _dam;
			type = "projectile";
		}
		public function shoot(ang:Number):void
		{
			angle = -ang + 90;
			yConst = 5 * Math.cos(angle * Math.PI / 180);
			xConst = 5 * Math.sin(angle * Math.PI / 180);
		}
		override public function update():void
		{
			y -= yConst;
			x += xConst;
			if (x < 0 || x > 640 || y < 0 || y > 480 || collide("platform", this.x, this.y)) world.remove(this);
		}
	}

}

Never had anything like this happen before. [1]: https://dl.dropboxusercontent.com/u/31366539/FPstuff/TestingEnviro.swf “Here you can see what’s goin on.”


(Jacob Albano) #2

Is it possibly due to all the projectiles sharing a hitbox? When you do this:

p.mask = _bul.mask;

You’re setting a reference, not copying the mask. I’m wondering if the collision fails when you spam bullets because it’s checking against the wrong hitboxes.


(David Williams) #3

Ah. So I should break it down even further, creating a new hitbox with the variables from the original, like I’m doing with the other variables?

[Edit] That did indeed fix the problem. Thanks for pointing that out for me, I looked everywhere and missed it.


(Jacob Albano) #4

You could also look into using the create() and recycle() functions to automate the copying process.


(David Williams) #5

I’ve always been curious on how to implement create() and recycle(), but never taken the time to fully understand how they work. It’s like relocating existing instances from what I’ve gathered?