Author Topic: [SOLVED] Player hitbox?  (Read 1695 times)

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
[SOLVED] Player hitbox?
« 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
« Last Edit: Wed, Jul 21, 2010 by NateTheGreatt »
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery

Jim

  • Guest
Re: Player hitbox?
« Reply #1 on: Wed, Jul 21, 2010 »
This thread may be of help 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.

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
Re: Player hitbox?
« Reply #2 on: Wed, Jul 21, 2010 »
This thread may be of help 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
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery

Jim

  • Guest
Re: Player hitbox?
« Reply #3 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

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
Re: Player hitbox?
« Reply #4 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?
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery

Jim

  • Guest
Re: Player hitbox?
« Reply #5 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();
}

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
Re: Player hitbox?
« Reply #6 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).
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery

Jim

  • Guest
Re: Player hitbox?
« Reply #7 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.

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
Re: Player hitbox?
« Reply #8 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.
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery

Jim

  • Guest
Re: Player hitbox?
« Reply #9 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.
« Last Edit: Wed, Jul 21, 2010 by Jim »

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
Re: Player hitbox?
« Reply #10 on: Wed, Jul 21, 2010 »
Thanks a bunch dude  ;D
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery