Author Topic: How to remove element from the middle of array?  (Read 1283 times)

smck87

  • Member
  • **
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Hi, i try this code
Code: [Select]
var bullet:Bullet;

for (var i:int = 0; i < _bullets.members.length; i++)
{
bullet = _bullets.members[i];

if (bullet.x > 200)
{

bullet.destroy();
_bullets.members[i] = null;
_bullets.members.splice(i--, 1);
}
}
_bullets is FlxGroup. I get error TypeError: Error #1010: A term is undefined and has no properties. So how can i remove some bullets from array in proper way?

smck87

  • Member
  • **
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: How to remove element from the middle of array?
« Reply #1 on: Sun, Jul 17, 2011 »
ok, i'll try to ask in other way: i have a FlxGroup object and a lot of bullets added using function of _bullets:FlxGroup :
Code: [Select]
(_bullets.recycle(Bullet) as Bullet).shoot(x,y); // this is from Mode game
How to walk through all bullets in FlxGroup and remove some of them ?

GrimPanda

  • Member
  • **
  • Posts: 46
  • Karma: +1/-0
    • View Profile
    • Grim Panda Games
Re: How to remove element from the middle of array?
« Reply #2 on: Sun, Jul 17, 2011 »
Why are you trying to remove the middle of it? It's a homogeneous array (all elements are the same), so just pop one off?
"Mortis Ailuropoda"

zadvornykh

  • Active Member
  • ***
  • Posts: 205
  • Karma: +1/-0
    • View Profile
Re: How to remove element from the middle of array?
« Reply #3 on: Sun, Jul 17, 2011 »
To just remove a bullet:
Code: [Select]
bullet = _bullets.splice(i, 1);
bullet.destroy();
bullet = null;

It would also be better to loop over the array in reverse.

John Hutchinson (Johntron247)

  • Commodore 256
  • Contributor
  • ****
  • Posts: 392
  • Karma: +1/-0
  • I can has lazerz?
    • View Profile
    • Level X Games
Re: How to remove element from the middle of array?
« Reply #4 on: Sun, Jul 17, 2011 »
Arrays in AS3 are basically just stacks, which means that whenever you take an idem out, the length shortens.  Make your loop run backward from the end to the beginning and see if that works.

Code: [Select]
for (var i:int = _bullets.members.length - 1; i >= 0; i--)

smck87

  • Member
  • **
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: How to remove element from the middle of array?
« Reply #5 on: Sun, Jul 17, 2011 »
Why are you trying to remove the middle of it? It's a homogeneous array (all elements are the same), so just pop one off?
No, bullets aren't same, each one have unique x, y coordinats and represent 1 bullet in game like this


zadvornykh, JohnTron247 i'll try backward loop, tnx
i also tried another way to walk through bullets and remove some of them (look at screen), but it didn't work.
« Last Edit: Sun, Jul 17, 2011 by smck87 »

zadvornykh

  • Active Member
  • ***
  • Posts: 205
  • Karma: +1/-0
    • View Profile
Re: How to remove element from the middle of array?
« Reply #6 on: Sun, Jul 17, 2011 »
In the example above you can just loop over all your bullets backwards, and check if they are out of the screen. The individual position does not matter, so you can just remove any bullet that is meets the requirement.

That being said, there really is no reason to remove the bullets from the array and destroy them, as you would not want to create new bullets during the game. Just call kill() on the bullet instead of destroying it, and keep it in the array. Then revive() it when all the bullets are killed.

Do a search for object pooling on this forum to read more about it.

smck87

  • Member
  • **
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: How to remove element from the middle of array?
« Reply #7 on: Sun, Jul 17, 2011 »
In the example above you can just loop over all your bullets backwards, and check if they are out of the screen. The individual position does not matter, so you can just remove any bullet that is meets the requirement.

That being said, there really is no reason to remove the bullets from the array and destroy them, as you would not want to create new bullets during the game. Just call kill() on the bullet instead of destroying it, and keep it in the array. Then revive() it when all the bullets are killed.

Do a search for object pooling on this forum to read more about it.
Thank you, now my code looks like this:
Code: [Select]
                // PlayState::update()
override public function update():void
{
super.update();

for each (var i:Bullet in _bullets.members)
{
if (i.x > 200)
i.kill();
}
                }

                //  Player::update()
override public function update():void
{
                         if (FlxG.keys.SPACE)
                                   (_bullets.recycle(Bullet) as Bullet).reset(x-10,y+25);
                }
and works perfect :D