Author Topic: Sorting  (Read 888 times)

superbany

  • Member
  • **
  • Posts: 53
  • Karma: +0/-0
  • [Please enter text here]
    • View Profile
    • Superbany's games
Sorting
« on: Tue, Nov 2, 2010 »
For my platformer, I'm trying to get the character's eyes to look in whatever direction the player is aiming. I create a <player> object in PlayState.as. In player.as, it creates an <_eyes> object, which is an instance of the PlayerEyes class, and adds it to the <_player> (different from <player>) group in PlayState. However, this puts the eyes behind the head, and they can't be seen. How would you do about reordering so that the eyes show up in front? Thanks.

DelishusCake

  • délicieux gâteau
  • Member
  • **
  • Posts: 44
  • Karma: +0/-0
    • View Profile
Re: Sorting
« Reply #1 on: Tue, Nov 2, 2010 »
For my platformer, I'm trying to get the character's eyes to look in whatever direction the player is aiming. I create a <player> object in PlayState.as. In player.as, it creates an <_eyes> object, which is an instance of the PlayerEyes class, and adds it to the <_player> (different from <player>) group in PlayState. However, this puts the eyes behind the head, and they can't be seen. How would you do about reordering so that the eyes show up in front? Thanks.
Simply switch the 'add()' statements. Flixel adds them in the order you put them in.

superbany

  • Member
  • **
  • Posts: 53
  • Karma: +0/-0
  • [Please enter text here]
    • View Profile
    • Superbany's games
Re: Sorting
« Reply #2 on: Tue, Nov 2, 2010 »
No, I know how it works, but I don't see how I could use that with my current form. I'll give the exact code:
in PlayState:
Code: [Select]
_player = new Player(32, 2048, 1, 0, _throwKnives.members, _playerGroup);
_playerGroup.add(_player);

//...

add(_playerGroup);

in Player:
Code: [Select]
public function Player(X:Number, Y:Number, Keys_B:Number, Keys_G:Number, KNIVES:Array, PLAYERGROUP:FlxGroup)
{
//Construction stuff for the player

_eyes = new PlayerEyes(x, y, ImgPlayerEyes);
PLAYERGROUP.add(_eyes);
}



The main body and eyes are both added to _playerGroup, which is then added to the state. Would it work better to add  the eyes separately in PlayState?
so that PlayState would have:

Code: [Select]
_eyes = new PlayerEyes(32, 2048, ImgPlayerEyes);
_playerGroup.add(_eyes);
_player = new Player(32, 2048, 1, 0, _throwKnives.members);
_playerGroup.add(_player);

//...

add(_playerGroup);

and Player would have:
Code: [Select]
public function Player(X:Number, Y:Number, Keys_B:Number, Keys_G:Number, KNIVES:Array)
{
//Construction stuff for the player
}

Backward pieS

  • Active Member
  • ***
  • Posts: 110
  • Karma: +0/-0
    • View Profile
    • Backward pieS, LLC
Re: Sorting
« Reply #3 on: Wed, Nov 3, 2010 »
I had a thread with the exact same problem just last week (in a top-down game, I had a helicopter class which called a propeller class but the propeller always appeared under the helicopter). I never got it to work without adding a propeller group in PlaySate.

However, looking at your code, it seems like it might be possible to use the z-sorting functionality in FlxGroup as part of the newer beta version Flixel(if you download the beta you can choose to only use FlxGroup if you're worried about the rest of it being incompatible/unstable). See Rybar's post in my thread for a very clear explanation of how it would work: http://flixel.org/forums/index.php?topic=2607.0

superbany

  • Member
  • **
  • Posts: 53
  • Karma: +0/-0
  • [Please enter text here]
    • View Profile
    • Superbany's games
Re: Sorting
« Reply #4 on: Wed, Nov 3, 2010 »
I was actually just going to post this saying that I solved it when your post showed up. What I changed it to was:
PlayState:
Code: [Select]
//Construction:
_player = new Player(32, 2048, 1, 0, _throwKnives.members);
_playerGroup.add(_player);
_playerEyes = new PlayerEyes(32, 2048);
_playerGroup.add(_playerEyes);

//...

add(_playerGroup);

//Update:
super.update();

(_playerEyes as PlayerEyes).defineDirection(Player._knifeAngle);
_playerEyes.x = _player.x;
_playerEyes.y = _player.y + 1;
_playerEyes.facing = _player.facing;

Player:
Code: [Select]
//Absolutely nothing about the eyes

PlayerEyes:
Code: [Select]
public function defineDirection(angleInt:int):void
{
if (angleInt == 0)
play("straight")
else if (angleInt == -1)
play("up");
else
play("down");
}

It seems to be working perfectly fine. The player will look in whatever direction the character is aiming, and the eyes are on top. Thanks!