Author Topic: Random error with Flixel Overlap  (Read 1277 times)

SemtexVa

  • New Member
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Random error with Flixel Overlap
« on: Sun, Sep 14, 2014 »
Hello there,
I am making a "Space Invaders" -like game, i created the Player, enemy, bullet ,score, and enemyCount i have 25 enemy ships created.
Sometimes both the enemy counter and score update as if i had killed two enemies instead of 1, it happens randomly, and my score jumps from 10 to 30, instead of 20, and my enemycount goes down 2 instead of 1, even if the enemies destroyed were only 1.
I have no idea how to fix it.
Code: [Select]
public var _enemiesKilled:int=0; and then
private function killEnemy(bullet:Bullet,enemy:Enemy):Void
{
_playerBullet.remove(bullet);
_playerBullet.destroy;
GV.bShoot = false;
_enemy.remove(enemy);
_enemy.destroy;
                _enemiesKilled++;
_score += GC.enemyPoints;
_highScore+= GC.enemyPoints;
_hud.updateHUD(_health, _score, _highScore);

if (_enemiesKilled == 25) {
FlxG.switchState(new WinGame());
}

sometimes the error occurs so many times, that the _enemiesKilled counter reaches 25 with 10 ships still in screen
Thanks for your Answers
SemtexVa
PD: sometimes when i try to post, in the verification questions i get: flixel is a library for what language?, and the endles runner game question. i don't know the answers, can someone pls write them down here. Thank you

Arkeus

  • Contributor
  • ****
  • Posts: 321
  • Karma: +1/-0
    • View Profile
    • I, Arkeus
Re: Random error with Flixel Overlap
« Reply #1 on: Sun, Sep 14, 2014 »
Try replacing:

Code: [Select]
_playerBullet.remove(bullet);
_playerBullet.destroy;

With

Code: [Select]
bullet.kill();
And

Code: [Select]
_enemy.remove(enemy);
_enemy.destroy;

With

Code: [Select]
enemy.kill();
You can keep the removes if you want, but they shouldn't be necessary in this case (assuming you're rebuilding _enemy and _playerBullet on each new level, if not, just clear them at the end of each level). Also as a heads up "_playerBullet.destroy" doesn't call the destroy function. You need parentheses (so do _playerBullet.destroy()). But you want to call kill() and not destroy(). If you kill an object, it prevents overlap/collide from continuing to overlap/collide them.

As for your last problem, http://flixel.org/about.html will help you with one, and http://adamatomic.com/canabalt/ is a pretty cool game.

Edit: A side note, you should reuse killed bullets when you're shooting new bullets. This gives you the best performance, since you'll only have a limit amount of bullets that are constantly getting reused. If you don't want to do that, another method is to run a function every couple seconds that copies over all the non-killed bullets to a new array, to get rid of the killed bullets, to keep the array small, which is much quicker than removing them one by one (which doesn't actually fully take it out of the array unless you tell it to splice).
« Last Edit: Sun, Sep 14, 2014 by Arkeus »

SemtexVa

  • New Member
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: Random error with Flixel Overlap
« Reply #2 on: Mon, Sep 15, 2014 »
Quote
Try replacing:

Code: [Select]

_playerBullet.remove(bullet);
_playerBullet.destroy;


With

Code: [Select]

bullet.kill();


And

Code: [Select]

_enemy.remove(enemy);
_enemy.destroy;


With

Code: [Select]

enemy.kill();


You can keep the removes if you want, but they shouldn't be necessary in this case (assuming you're rebuilding _enemy and _playerBullet on each new level, if not, just clear them at the end of each level). Also as a heads up "_playerBullet.destroy" doesn't call the destroy function. You need parentheses (so do _playerBullet.destroy()). But you want to call kill() and not destroy(). If you kill an object, it prevents overlap/collide from continuing to overlap/collide them.

As for your last problem, http://flixel.org/about.html will help you with one, and http://adamatomic.com/canabalt/ is a pretty cool game.

Edit: A side note, you should reuse killed bullets when you're shooting new bullets. This gives you the best performance, since you'll only have a limit amount of bullets that are constantly getting reused. If you don't want to do that, another method is to run a function every couple seconds that copies over all the non-killed bullets to a new array, to get rid of the killed bullets, to keep the array small, which is much quicker than removing them one by one (which doesn't actually fully take it out of the array unless you tell it to splice).


Thank you so much!, it worked perfectly. it now swiches to WinState when all enemies are destroyed.
Once again thank you.