HitBox not working


(Granit Bajraktari) #1

Hey guys.

I completed the tutorial on useflashpunk.net and started making a game. However when i tested the hitbox of my bullet (15px tall and 12px wide graphic) i noticed that the collision happens way ahead before the ship touches the bullet. It’s too big on the top left corner (the hitbox) and too small on the lower right corner.

I could upload a picture but it won’t let me.

Thanks.


(Jean) #2

Code please?

20 caracters


(Martí Angelats i Ribera) #3

Could you copy-paste the where you make the collision box? Also use triple ` to make it apear as code

example

Also where you handle the collision.


(Granit Bajraktari) #4

This is the Bullet.as class

package {

import net.flashpunk.Entity;
import net.flashpunk.Graphic;
import net.flashpunk.FP;
import net.flashpunk.graphics.Image;

/**
 * ...
 * @author GranitBa
 */
 
public class Bullet extends Entity {
	
	[Embed(source="media/Bullet.png")]
	public const BULLET:Class;
	
	public function Bullet() {
		
		x = 250;
		y = 100;
		
		setHitbox(12, 15);
		
		type = "bullet";
		graphic = new Image(BULLET);
		
	}
	
	public function destroy():void {
		FP.world.remove(this);
	}

}

}

And this is the class where i handle the collision

package {

import net.flashpunk.Entity;
import net.flashpunk.graphics.Image;
import net.flashpunk.utils.Input;
import net.flashpunk.utils.Key;

/**
 * ...
 * @author GranitBa
 */
public class Player extends Entity {
	
	[Embed(source="media/Ship.png")]
	private const PLAYER:Class;
	
	public function Player() {
		graphic = new Image(PLAYER);
		
		setHitbox(50, 50);
	}
	
	override public function update():void {
		var b:Bullet = collide("bullet", x, y) as Bullet;
		
		if (b) {
			trace("Collided!");
		}
		
		if (Input.check(Key.LEFT)) {
			x -= 4.5;
		}
		
		if (Input.check(Key.RIGHT)) {
			x += 4.5;
		}
		
		if (Input.check(Key.DOWN)) {
			y += 3;
		}
		
		if (Input.check(Key.UP)) {
			y -= 5;
		}
	
	}

}

}

In the gameworld class i just add them both to the world and in the main class i set up that world.

It could also be the player.as fault.


(Martí Angelats i Ribera) #5

This is probably caused in Player.as

The function collide(type, x, y) works using the x and y parameters as a veirtual position. Here you are detecting if you collided in the last frame becouse you haven’t moved yet.

Just move the player before checking the collide (the input check goes first).

PD: I assumed that the graphics are correct, but they could cause issues.


(Granit Bajraktari) #6

Yes I have to do that and I found out that the hitbox of my player was too big. I solved that problem using the very helpful FP console.(Which i just found out that it exists, you should mention it in the tutorial Zach)

Soo thanks.


(Martí Angelats i Ribera) #7

Oh nice.

Edit the title and add [solved] so people in the future will see that this was solved if they have similar problems.


(Granit Bajraktari) #8

Thanks for being so newbie friendly!!