Flixel Forums

development => help => Topic started by: NateTheGreatt on Wed, Jul 21, 2010

Title: [SOLVED] Player hitbox?
Post by: NateTheGreatt on Wed, Jul 21, 2010
What would be the best method to use when trying to implement hitboxes? For example on my player: Should I make my player class extend FlxGroup and add a player and  hitbox to the class?

I'm trying to make it as modular as possible
Title: Re: Player hitbox?
Post by: Jim on Wed, Jul 21, 2010
This thread may be of help http://flixel.org/forums/index.php?topic=2063.0 (http://flixel.org/forums/index.php?topic=2063.0)

We're discussing how to have a player that consists of multiple FlxSprites which move relative to a main body sprite at all times.
Title: Re: Player hitbox?
Post by: NateTheGreatt on Wed, Jul 21, 2010
This thread may be of help http://flixel.org/forums/index.php?topic=2063.0 (http://flixel.org/forums/index.php?topic=2063.0)

We're discussing how to have a player that consists of multiple FlxSprites which move relative to a main body sprite at all times.

Yeah, I was just reading through that. I'm just having a bit of trouble figuring out how to make the hitBox sprite collide with the map and have the player stick to it without reworking my entire Player class
Title: Re: Player hitbox?
Post by: Jim on Wed, Jul 21, 2010
They way I'm using at the moment is to call a function in the player class that updates the position of the sprites relative to the collidebox directly after calling collide in the playstate.

ie: In the playstate

collide
player.update_sprite_locations
overlap
Title: Re: Player hitbox?
Post by: NateTheGreatt on Wed, Jul 21, 2010
They way I'm using at the moment is to call a function in the player class that updates the position of the sprites relative to the collidebox directly after calling collide in the playstate.

ie: In the playstate

collide
player.update_sprite_locations
overlap

Do I have to move all of my player's update code and put it into the new FlxGroup player class?
Title: Re: Player hitbox?
Post by: Jim on Wed, Jul 21, 2010
No, you can manually add FlxSprites to a FlxSprite without using add()

Basically instead of adding the child sprite to a FlxGroup which renders and updates the sprite for you, you call the child sprites render and update function from the parent sprite like so:
Code: [Select]
public var hitbox:FlxSprite;

public function Player(X:Number, Y:Number)
{
hitbox = new FlxSprite;
hitbox.createGraphic(16,16,0xff0000ff);
hitbox.x = 134;
hitbox.y = 134;
}

override public function update():void
{
super.update();
hitbox.update();
}

override public function render():void
{
super.render();
hitbox.render();
}
Title: Re: Player hitbox?
Post by: NateTheGreatt on Wed, Jul 21, 2010
No, you can manually add FlxSprites to a FlxSprite without using add()

Basically instead of adding the child sprite to a FlxGroup which renders and updates the sprite for you, you call the child sprites render and update function from the parent sprite like so:
-snip-

Aahh okay that's awesome. I'm still having trouble figuring this out, though. I'll just lay out exactly what I want done and see if you can help me with it:

My original player class has all of the movement and attack movements and all that fun stuff in it (obviously). Is there any way I can retain that code and have my hitbox collide with the tilemap as opposed to my player? So basically having the hitbox handle all collisions and the player handle all movement (that the hitbox corresponds with).
Title: Re: Player hitbox?
Post by: Jim on Wed, Jul 21, 2010
Unfortunately that's not doable since the collision function relies on the FlxSprite using the built in velocity vector stuff. This means that whichever sprite you use for movement you must use for collisions.

May I ask why you want to separate your player from your collision hitbox? Usually any issues can be solved by resizing the collision box.
Title: Re: Player hitbox?
Post by: NateTheGreatt on Wed, Jul 21, 2010
Unfortunately that's not doable since the collision function relies on the FlxSprite using the built in velocity vector stuff. This means that whichever sprite you use for movement you must use for collisions.

May I ask why you want to separate your player from your collision hitbox? Usually any issues can be solved by resizing the collision box.

Resizing... the... collision.. box..?

*face palm*

How do I do that? That's exactly what I'm trying to do -_-; I had no idea that that feature was built in.
Title: Re: Player hitbox?
Post by: Jim on Wed, Jul 21, 2010
Hahaha, yeah you never know until you need it

Here's the relevant code from the mode player class, as I don't have any of my own on hand ^_^

Code: [Select]
public function Player(X:int,Y:int,Bullets:Array,Gibs:FlxEmitter)
{
//bounding box tweaks
width = 6;
height = 7;
offset.x = 1;
offset.y = 1;
}

Offset is inherited from FlxSprite, and width and height are inherited from FlxObject, just so you know where they are in the docs.
Title: Re: Player hitbox?
Post by: NateTheGreatt on Wed, Jul 21, 2010
Thanks a bunch dude  ;D