Author Topic: Enemy FlxGroup splicing creates enemy jitter?  (Read 1251 times)

im_making_games

  • Member
  • **
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Enemy FlxGroup splicing creates enemy jitter?
« on: Thu, Jan 19, 2012 »
Hi.

I thought at one point of my game to use kill() to get rid of my enemies, figuring the large group wouldn't be that detrimental to my performance.

Then I realized I would at some point have to iterate through this group to find all living enemies y values for a particular function.

After realizing this, I tried destroy(), but that caused many internal problems.

I decided to go with theGroup.remove(theEnemy, true), this way I could remove the enemy without destroying it and keeping my FlxGroup relatively small.

Problem is, whenever I call remove via the enemy flying off the screen, I get a jitter from every enemy within the group, they go slightly upward and then continue moving downward.

Why is that happening? Why does this not happen if I remove them via a bullet collision (using exact function to remove them)? If I comment out the call to remove and just kill them this does not happen, so I know this is the problem. Has anybody had the same problem? Can I find a solution to this?

Foxtacy

  • Aspiring Indie Game Developer, Prototype Musician, Lame Pixel Artist
  • Member
  • **
  • Posts: 29
  • Karma: +0/-0
    • View Profile
    • Need songs? :3
Re: Enemy FlxGroup splicing creates enemy jitter?
« Reply #1 on: Fri, Jan 20, 2012 »
Hello my friend!

I think you need to

Well i see some problems there:

"Then I realized I would at some point have to iterate through this group to find all living enemies y values for a particular function."

Maybe you should try to create a function to filter the living enemies using stuff like: yourGroup.countLiving() and yourGroup.getFirstAlive();

After realizing this, I tried destroy(), but that caused many internal problems.

You should NEVER use destroy(). Because destroy might nullify your enemies, and it will cause you many problems. You should call destroy when you close the game, to clean up the memory.

Now, about this:

"theGroup.remove(theEnemy, true)"

I don't think this is a good idea. because when you kill() an enemy, you don't actually need to remove it from the group.

Check out the tutorials on photonstorm.org about object pools, this site might give you a glance of what you're doing wrong (and it has very good tutorials too :D)

To keep your enemies onscreen, you might want to do so by tilemap collision (that's what i'm doing on my game here)

something like:

FlxG.collide(tilemap, enemy);

and within your tileset, there's a way to set what tiles will be unable to pass through.

And if you want to allow them to fly off screen just comment this collision check and Bazinga! your enemies will be able to go off screen :D

I hope i have helped you with your problem, or maybe have ideas on how to solve it :D





Waiting is wasting

im_making_games

  • Member
  • **
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: Enemy FlxGroup splicing creates enemy jitter?
« Reply #2 on: Fri, Jan 20, 2012 »
thanks, I guess I'll just iterate through living enemies and just use a pool of 100 or so enemies because I'll never have more than 100 on screen.

Foxtacy

  • Aspiring Indie Game Developer, Prototype Musician, Lame Pixel Artist
  • Member
  • **
  • Posts: 29
  • Karma: +0/-0
    • View Profile
    • Need songs? :3
Re: Enemy FlxGroup splicing creates enemy jitter?
« Reply #3 on: Mon, Jan 23, 2012 »
Hehe you're welcome ^u^

you could also use getFirstAvail()!

This method can get you the first >existing and alive< (not sure if it gets by alive, or by both exists and alive) without the need to iterate to find an available member :)
Waiting is wasting

Berzee

  • Member
  • **
  • Posts: 69
  • Karma: +0/-0
    • View Profile
Re: Enemy FlxGroup splicing creates enemy jitter?
« Reply #4 on: Mon, Jan 23, 2012 »
This is what I use =) I call it every update(), but I am not sure how it would work with a large number of enemies. Seems to work fine with a dozen or so, though.

Code: [Select]
public static function clearDeadItemsFromFlxGroup(theGroup:FlxGroup):void
{
var toDelete:FlxGroup = new FlxGroup();
for each( var obj:FlxObject in theGroup.members )
{
if ( obj.dead )
{
toDelete.add(obj);
}
}
for each( obj in toDelete.members )
{
theGroup.remove(obj, true);
}
}