How to update Z order of FlxTypedGroup of bunch of Actors that I sub-class them into Monsters and Heros

33 views
Skip to first unread message

Viperace

unread,
Aug 10, 2015, 1:17:27 PM8/10/15
to HaxeFlixel
I have two types of class instance (Monster and Hero), both are subclass from Actors, which is subclass from FlxSprite.
In PlayState, I add the group one by one, hence causing the screen to render Hero first then Monster sprite will stack on them. 

Now, I would like PlayState to treat all these Actor equally, and sort them according to their 'y' value. Not sorting Monster first, then sort Hero.

If there is only one type of class, then I have the solution using FlxSort. This will sort them nicely. I do not know how to combine them into one group and let PlayState sort them.
  • I tried to create a new FlxTypedGroup<Actor> to hold both Monsters and Heros, and it would't allow me to, because this group only hold <Actor> not their child class

Any ideas?

Seb Jones

unread,
Aug 10, 2015, 5:10:37 PM8/10/15
to HaxeFlixel
FlxTypedGroup<Actor> should store any subclasses of the type assigned to it (in this case Hero and Monster). I made a quick test and this worked:

//IN CONSTRUCTOR
_actors = new FlxTypedGroup<Actor>();
var hero = new Hero(20, 100);
var monster1 = new Monster(20, 80);
_actors.add(hero);
_actors.add(enemy1);
add(_actors);
//IN UPDATE
_actors.sort(FlxSort.byY, FlxSort.ASCENDING);

Hope this helps :)

Viperace

unread,
Aug 11, 2015, 9:08:34 AM8/11/15
to HaxeFlixel
Ah, ok, I see that. the problem is more like this 

_actors = new FlxTypedGroup<Actor>();
var herolist: FlxTypeGroupd<Hero> = new FlxTypeGroupd<Hero>();
var monsterlist: FlxTypeGroupd<Monster> = new FlxTypeGroupd<Monster>();

_actors.add(herolist); // <-- error

Seb Jones

unread,
Aug 11, 2015, 11:38:07 AM8/11/15
to HaxeFlixel
You can't add an FlxTypedGroup<Hero> to _actors because it expects an Actor or a subclass of Actor, which FlxTypedGroup<Hero> is not. You'll have to add the items from each group to _actors individually, like so:

for (hero in heroList)
_actors.add(hero);
for (monster in monsterList)
_actors.add(monster);

Viperace

unread,
Aug 11, 2015, 12:36:18 PM8/11/15
to HaxeFlixel
Ok, I guess I will have to settle with that in the update() cycle. Seems like not much performance drop

for ( h in world.heroList)
{
Zobjects.add(h);
}
for ( m in world.monsterList)
{
Zobjects.add(m);
}
Zobjects.sort(FlxSort.byY);
Reply all
Reply to author
Forward
0 new messages