Jump to content

After game reset, arcade.overlap detects hit from previous game..?


Recommended Posts

Okay, here is a tough question I think. :huh:


I'm working on a endless-runner game with one player and some enemies. When the game is over, the player's function kill() is called, and for any left over enemy kill() is also called. Then I start a new game, I revive() the same player object and in the onRevive its position is reset to the upper half of the screen. Enemies are also revive() and repositioned in the bottom half of the screen and the game restarts.


The problem is that sometimes the player hits an enemies instantly at the very first frame of the new game. To find out what is happening, I added two console.log output inside the overlap hit handler function:

player overlaps enemy ==> player.x,y=402,116 gameobj.x,y=240,620
player overlaps enemy ==> player.body.x,y->w,h=127,383->32,32 gameobj.body.x,y->w,h=64,347->64,64 

The first line outputs the player/enemy position, the second shows the player.body position. It looks like the positions are reset to the correct positions, however their respective .body objects aren't. The .body x,y position still contain values from the previous game, as the enemies are recycled from a pool. And the body elements are infact overlapping so that's why the overlap handler is called.


I tried calling the .update() function of each sprite directly after it is revived, but that didn't work. Can anyone point me in the right direction to fix this?

Link to comment
Share on other sites

Btw when the game is over and then restarted, it stays in the same "state", I mean it doesn't state.start or someting. At game over, a "restart" button is made visible, and pressing that calls some functions that restart the game. The enemy pool is reused, and the player object is reused.


Anyway this is the revive code:

// en is a collection of enemy layout/start positionsfor (var i = 0; i < en.length; i++){    // get position or else random position                var x = en[i].x;    var y = en[i].y + yLevelOffset;        // get inactive gameobj from gameobjs object pool    var newobj = this._objGroup.getFirstDead();    // if there aren't any available, create a new one    if (newobj === null) {        newobj = new GameObj(this.game, x, y);        this._objGroup.add(newobj);    };    // else revive the newobj (set it's alive property to true)    newobj.revive();    // set values    newobj.init(en[i].objname, x, y, en[i].movetype, en[i].radius, en[i].angle);    // TESTING!!    console.log('spawnLevelPart -> initnew name='+en[i].objname+' x='+x+' y='+y);};
Link to comment
Share on other sites

Did you try sprite.reset()? It resets the physics body as well so it may solve your problem. 

Excellent :) thanks, that did the trick.


Btw I now use sprite.reset(x, y) but the documentation says it expects 3 parameters sprite.reset(x, y, health) I don't use the health so I guess that doesn't matter..

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...