Hitbox for SpriteMap not working [SOLVED]


(Darrin) #1

Hi, just getting back into coding after after a few years. I have a question about collision with the spritemap. Basically I can create a bullet and then have the target check to see if it is hit. This works fine.

Working code:

Bullet Class:

    		public function Bullet(x:int, y:int ) 
	{
		this.x = x;
		this.y = y;
		var img:Image = Image.createCircle(8,0x00FF00, 1);
		graphic = img;
		setHitboxTo(img);
		type = "bullet";
	}

Enemy Class checks:;

var bullet:Bullet = collide("bullet", x, y) as Bullet;
	if (bullet)
	{ 
		HUD.score++;
		bullet.destroy();
		destroy();
	}

So now I want to add a sprite. This doesn’t work when I turn the bullet class to a spritemap.

Bullet Class (new one with spritemap)

		public function Bullet1(x:int, y:int ) 
	{
		graphic = new Spritemap(ANIMATION, 64, 64);
				
		this.x = x;
		this.y = y;
				
		Spritemap(graphic).add("fly", [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,17,17,18,19,20,21,22,23,24,25,26,27], 30, true);
		Spritemap(graphic).play("fly");
				
		setHitbox(64, 64);
		type = "bullet";
	}

The collision is never detected but the animation plays fine. I’m doing something silly I’m sure, any ideas?


(Jacob Albano) #2

I think I see your problem. It’s in this line here:

var bullet:Bullet = collide("bullet", x, y) as Bullet;

When casting a variable with the as keyword, the expression returns null if the cast fails. In this case, you’re attempting to cast the entity to a Bullet, but it’s actually an instance of Bullet1. The variable is set to null and the conditional never evaluates to true.

Worth pointing out, you can simplify your animation code by doing this:

var spritemap:Spritemap = new Spritemap(ANIMATION, 64, 64);
spritemap.add("fly", FP.frames(0, 27), 30, true);
spritemap.play("fly");
graphic = spritemap;

(Darrin) #3

Thanks for both suggestions. I feel so rusty :smile: I guess I’ll have to do one class for all bullets or better do an iBullet Interface. I appreciate the help.


(TaylorAnderson) #4

I’m glad I clicked on this topic for no real reason, because I had no idea FP.frames existed and I’ve been using Flashpunk for like three years O_O


(Jacob Albano) #5

The FP class is categorically magical. It contains 90% of all programming you’ll ever need to do.


(TaylorAnderson) #6

I’m surprised it doesn’t have a randomRange function! (unless it does and I havent found it). If it doesn’t we should totally put it in.


(Zachary Lewis) #7

Random range? Just shove some FP functions together.

// 1. Basic Example
// Get a random uint between [100, 150].
var r1:uint = FP.rand(51) + 100;

// 2. Kinda Strange Example
// Get a random Number between [-12.5, 25.0).
var r2:Number = FP.lerp(-12.5, 25, Math.random());

// 3. Wicked Goofy Example
// Get a random uint between [3, 30] that is a multiple of 3.
var r3:uint = uint(FP.choose(FP.frames(3, 30, 2)));

All these examples are for goofs and fun and aren’t the most optimized way of calculating these values.


(TaylorAnderson) #8

Yeah I found it much easier to just get a randomRange func off the internet and pop it into my helper class :stuck_out_tongue: