for my game I have coded a basic life system and once the collision with the sprite is done I destroy it but it continues to work even after being destroyed how to fix it?
My code :
//condition for wins the health points
if (character.x < life.x + life.width + 33 && character.x + character.width - 33
life.x && healthPoints !==3 )
{
healthPoints++;
//Once time the sprite enter in collision this one is deleted of the memory
life.destroy();
}
//reset character and the bridge after the fall and lose of HP
if (character.y < wall.y + wall.height && character.y + character.height > wall.y
&& healthPoints !==0)
{
LoseHP()
platform2Reached = false
platform3Reached = false
/*healthPoints--;
bridge.rotated = false;*/
character.body.pos.x = 38;
character.body.pos.y = 505;
bridge.syncGameObject()/*.setDisplaySize(1,10)*/.setBodyScale(1);
bridge.body.pos.x = 95;
bridge.body.pos.y = 528;
bodyBlock.body.pos.x = 91;
bodyBlock.body.pos.y = 520;
}
//condition for wins the health points
if (character.x < life.x + life.width + 33 && character.x + character.width - 33
> life.x && healthPoints !==3 )
{
healthPoints++;
//Once time the sprite enter in collision this one is deleted of the memory
life.destroy();
}
//Condition of defeat
if (healthPoints == 0)
{
text.setText('YOU\nLOSE');
text.setPosition(text.width-20,text.height+150 );
tripleFace.anims.play('left');
character.setVelocity(0);
character.anims.play('stop');
}
Hello. It is difficult to say what can be wrong in your code, even with the code you posted in your last message⦠Like ātacā has said, maybe you have a logical error?
Are you sure your collision detection is working? Is your incrementation āhealthPoints++ā working?
Have you tried to debug your collision by sending a message to the javascript console?
If the collision is right⦠I do not know what else to say ā¦
Maybe you can test the āactiveā property of your ālifeā object to check if ālifeā has been deactivated correctly after destroy().
life.destroy();
if(life.active == true){
console.log(āError ā life is allways active after destroy()ā);
}
else{
console.log(āOK ā life has been destroyed!ā);
}
If i donāt have correctly understood your problem and the collision is working even if the life has been destroyed, maybe you need to delete life OR set your life variable to ānullā or āfalseā?
life.destroy();
life = false; // ā Replace your object by FALSE
if(life !== false){//If life IS NOT FALSE
if (character.x < life.x + life.width + 33 && character.x + character.width - 33 > life.x && healthPoints !==3 )
{
console.log(āCollision is working!ā);
life.destroy();
life = false; // ā Life is now FALSE
}
}
Yes itās fine it works now I donāt get my life back once my heart is destroyed and when I go back to the position it is in.
I just had to make a slight modification my life.destroy() must always be before the 1st life = false otherwise I have an error and I must have only one destroy().