I have some monsters writed in my code, and I would like to delete them at the moment they die. Their image, HP, attackPower ect.. So they don’t appear anymore nor deal damage. However, I don’t really know how to.
I’ve tried to nullify them, this isn’t working. Same for Delete (I am struggling hard). So do I have to make an array of monsters ? Here is my code :
Class Person
attack(target){
while (this.sprite.animationFrameProgress === 8 ){
this.sprite.animationFrameProgress -= 0.5;
}
if (this.sprite.animationFrameProgress === 0){
target.health -= this.attackPower;
}
if (target.health <= 0){
delete target.???;
}
console.log(target.health);
}
Class OverworldMap
playerAttackNearestMonster() {
const player = this.gameObjects.hero;
for (const key in this.gameObjects) {
if (key !== 'hero' && this.gameObjects.hasOwnProperty(key)) {
const monster = this.gameObjects[key];
const distanceX = Math.abs(monster.x - player.x);
const distanceY = Math.abs(monster.y - player.y);
if (distanceX <= 16 && distanceY <= 16) {
monster.sprite.setAnimation("fighting");
monster.attack(player);
if (player.attacking){
player.attack(monster);
}
}
}
}
slime: new Monster({
x: utils.withGrid(7),
y: utils.withGrid(8),
pixelSize: 32,
health : 50,
attackPower : 15,
animations : {
"monster" : [[0,0], [1,0],[2,0], [3,0]],
"monster-left" : [[0,5], [1,5],[2,5], [3,5]],
"monsterWalk-down" : [[0,1], [1,1],[2,1], [3,1],[4,1], [5,1]],
"monsterWalk-up" : [[0,1], [1,1],[2,1], [3,1],[4,1], [5,1]],
"monsterWalk-right" : [[0,1], [1,1],[2,1], [3,1],[4,1], [5,1]],
"monsterWalk-left" : [[0,6], [1,6],[2,6], [3,6],[4,6], [5,6]],
},
src: "/images/characters/monsters/slime.png"
}),
2
Answers
I've found a solution thanks to Justinas. Here is it :
Move your object deletion out of
attack
intoplayerAttackNearestMonster
: