Collision Callback function Running twice -Problem-

Got a question? Need help? Post here!

Collision Callback function Running twice -Problem-

Postby Marrue » Wed Mar 19, 2014 1:23 am

Hello Everyone!
I’ve been using the Axel library for a while now and I’ve encountered a problem that I just can’t seem to find the solution to. So I came here to the forum seeking for help.

So, I’m basically creating a side scrolling shoot-em-up game. I’m using the tutorial game “Axelite Star” as reference.

The problem happens when the Player’s “Beam” attack hits the enemy. I have it set to subtract the Enemy’s HP by the Beam’s Attack Points then destroy the Beam Attack. If the Beam Attack reduces the Enemy’s HP to Equal-to-or-Lower than 0, remove both the Beam and Enemy.

What happens here is that the Callback function of the Collision between the Beam and the Enemy fires more than once. But, this happens randomly, sometimes it will run once and sometimes twice, which is why it is confusing me. Enemies are set to have 1 HP and Player's Attack is set to 1.

I'm following the same organization of Entities like in Axelite Star.

GameState.as
(I took some lines out for compression)
http://paste.vigil.io/code/m29
Line 62 has the update method. Line 82 has the callback Method for Collision between Bullet and Enemy.

Enemy.as.
This is the Method "reduceHealth"called when enemy is hit by beam attack.
Code: Select all
//Reduce Health
      public function reduceHealth(amountToReduce:uint):void {
         currentHealth -= amountToReduce;
         //Reduce Health by given amount, if current HP less than 0, set it to 0
         if (currentHealth <= 0 )
            currentHealth = 0;
      }


Hope the code I posted helps you find out what's happening. The Definition of all the other Classes are pretty much basic and closely similar to the Axelite Star, since I used it as reference. If you do need me to post more code or all, please do tell. Thanks in Advance!
"I play Video Games to Kill time, but it keeps coming back"
User avatar
Marrue
Private
 
Posts: 3
Joined: Wed Mar 19, 2014 12:23 am

Re: Collision Callback function Running twice -Problem-

Postby Arkeus » Wed Mar 19, 2014 9:43 pm

Is it possible you're adding either bullets or enemies twice? Perhaps to two different locations (such as adding an enemy to the colliders group and to the enemies group). That could possibly explain the issue. Each entity should only be added to one group that is part of the state (you can add it to many groups, but only one of those should be added to the state (or added to something that's added to the state, etc)).

And when you say it's happening multiple times randomly, how are you determining that? Visually seeing the score go up twice even though you kill one enemy?
Image
User avatar
Arkeus
Site Admin
 
Posts: 363
Joined: Mon Mar 26, 2012 12:43 am

Re: Collision Callback function Running twice -Problem-

Postby Marrue » Sat Mar 22, 2014 12:40 am

Hello Arkeus!
Thank you so much for replying back. Sorry for the late response, being busy these last days.
So I’ve checked all the Axgroup’s that I created and the Classes that add the Enemy and the Player Bullets, they all seem to be adding the objects in the correct group and only once. Here are the lines of code where I add the Enemies and Bullet’s to the groups through code.

Adding the Enemies to the enemies group is done through a basic spawner I made. It is an AxEntity that has a Timer that executes the "SpawnEnemy()" Method every 1 second.
Code: Select all
public function spawnEnemy():void {
         spawnerTimer = AxU.rand(1, 5);//Resturns a number between 1 and 5
         //Spawn enemy if number between 1 through 4
         if (spawnerTimer > 0 && spawnerTimer < 4) {
            
            theEnemy = new SkeletonSerpent();
            theEnemy.x = this.x;
            theEnemy.y = this.y;
            
            Registry.game.enemies.add(theEnemy);
         }
      }

The PlayerBullets, called beamShot, are created within the Player but placed on the PlayerBullet's group through the registry reference.
Code: Select all
if(Registry.hud.getPlayerCurrENG() > 0){
         if (Ax.keys.pressed(AxKey.SPACE)) {
            if(powerSelected == 0 && Registry.hud.getPlayerCurrENG() > 0){
            beamShot = new BeamShot();
            beamShot.x = this.x + 85;
            beamShot.y = this.y + 35;
            Registry.game.playerBullets.add(beamShot); /* <<===== ADDING THE BULLET  */
            Registry.hud.reduceEnergy(1);
            animate("shoot", true);//Adding true will reset the animation to the first frame.
            } else {
               if(powerSelected == 1  && Registry.hud.getPlayerCurrENG() >= 2){
               fireBallShot = new FireBallShot();
               fireBallShot.x = this.x + 85;
               fireBallShot.y = this.y + 35;
               Registry.game.playerBullets.add(fireBallShot); /* <<===== ADDING THE BULLET  */
               Registry.hud.reduceEnergy(2);
               animate("shoot", true);//Adding true will reset the animation to the first frame.
               }
            }
            
         } else {
            if (Ax.keys.released(AxKey.SPACE)) {
            animate("stand");
         }
         }
         }

So far, this is how the game objects should be organized. A Visual Reference.
http://paste.vigil.io/img/m2l
I know the code executes multiple times randomly because I had a trace statement on the callback function of the collision between the PlayerBullets and Enemies. That’s the function that’s running twice. It’s not on the code I posted here because I removed some lines to make it shorter to post here.

The Enemy is set to have 2 HP and the PlayerBullet is set to 1 attack power, the Callback function gets executed a maximum of 2. I added more HP to the enemy and the function gets called 2 times, always. It used to be random, but now it’s twice all the time.When the Attack is greater than the HP of the enemy, the function runs once. (Again, I know this because I used the trace statement inside the function "onBulletHitEnemy" which tells me how many times it executed)

I am starting to believe it’s a delay issue, as in, while it is calculating the remaining health of the enemy another collision is detected. :/ But I remove the Bullet as soon as the collision is detected, and it still fires up the call back function again. I'm out of ideas.

Hope this helps find out what's the problem.
Again, Thanks Arkeus for taking the time to help me with this issue. Much Appreciated~!
"I play Video Games to Kill time, but it keeps coming back"
User avatar
Marrue
Private
 
Posts: 3
Joined: Wed Mar 19, 2014 12:23 am

Re: Collision Callback function Running twice -Problem-

Postby Arkeus » Sat Mar 22, 2014 1:10 pm

Hmm, I'm stumped. One change is that all overlap/collide calls should be after super.update(), but the issues that causes would be unrelated and probably not noticeable in a side shooter.

Would it be possible to zip your project and send it to me? (iarkeus ʘ gmail) If there's private code/assets, perhaps strip it down to the smallest set that you can still reproduce the problem with. Probably much easier for me to debug that way.
Image
User avatar
Arkeus
Site Admin
 
Posts: 363
Joined: Mon Mar 26, 2012 12:43 am

Re: Collision Callback function Running twice -Problem-

Postby Marrue » Sat Mar 22, 2014 1:50 pm

I have done tests with the super.update(); changes, but still, nothing.

Sure! It is possible! I don't have much private code, since I've done so many changes to fix the issue I removed a lot. You will find similarity with Axelite Star, that's me trying to see if I coded incorrectly, but, still having the same issue.

So, I'll be preparing the code and send it to you by mail later today. I Thank you for your time and effort to help. :)

Oh, and I used FlashDevelop for programming.
"I play Video Games to Kill time, but it keeps coming back"
User avatar
Marrue
Private
 
Posts: 3
Joined: Wed Mar 19, 2014 12:23 am


Return to Help

Who is online

Users browsing this forum: No registered users and 1 guest

cron