Hi! I'm around this problem for fews days, the thing is:
I have two enemies:
->
sentinel.js
->
spike.js
And one Hero:
->
hero.js(our player)
In the main.js I get the number of enemies by a variable:
numenemies = numSpikes+numSentinels;
and I am trying to make a score after killing a enemy, but the problem is that when one of the Entities dies, a sentinel, a spike or a hero, I don't know who dies and I would like to know to assign different scores and some other things. I was trying to check who dies in the entity.js, on the
kill function by making a simple
if condition. In the kill function I only have:
kill: function() {
ig.game.removeEntity( this );
}
I was trying somehow to find who died, I don&
039;t know If im doing it right in this class or should be in other way. Is there a way to compare the #this
that is in the removeEntity line with the different Entities, like the hero, spike or sentinel?
1 decade ago
by dominic
I'm not sure I understood the problem. I'll try anyway :)
Usually you shouldn&
039;t need to modify the "core" impact files (that is anything in #lib/impact/
). It&
039;s better to implement your functionality in subclassed Entities, or, if you need to, write a "plugin" using the Class' #.inject()
method.
Sooo, if you want to have a different "die" behavior for each of your entities, just implement different
kill()
methods for them.
I.e. in your
sentinel.js
:
kill: function() {
ig.game.numSentinels--;
this.parent();
}
And in your
spike.js
:
kill: function() {
ig.game.numSpikes--;
this.parent();
}
There are many other ways to do you want, though. You could also count your entities:
var numEnemies = this.getEntitiesByType( EntitySentinel ).length + this.getEntitiesByType( EntitySpike ).length;
(Note that
getEntitiesByType()
can be a bit performance intensive when you have lots of entities in your game)
First, impact tracks the numbers, so you don't have to:
For example in your main.js:
numEnemies = this.getEntitiesByType(EntitySentinel).length+this.getEntitiesByType(EntitySpike).length;
I didn't understand your question though.
When you overwrite an entities .kill(), and write something in it, it's executed when that type of enemy dies. Then you can do anything you want.
For example, just add this in your sentinel.js and open your browser console
kill : function() {
console.log("Another Sentinel bites the dust!")
}
Thanks to both of you, the
numEnemies = this.getEntitiesByType( EntitySentinel ).length + this.getEntitiesByType( EntitySpike ).length;
just solved one of my problems. I was counting them in a different way, with a
for condition in each of the .js enemy files. I'm still trying to understand some of the things that can be used with impact. And I'm new with javascript.
I'm using the kill function from the entity.js file, but I've changed it a little:
kill: function() {
var entityObj = this;
if (entityObj.type == 1) {
ig.game.removeEntity(entityObj);
entityObj.Death();
} else {
ig.game.removeEntity(entityObj);
}
},
I've created a new var, entityObj because using the
this was causing a problem. but beside that I'm cheking if the entity that dies, has type 1, EntityHero, if not, i just remove the entity, because I dont want it replaced anymore, because its a grenade or a enemy, but if its type one, I remove it and call a new function that spawns my Hero and makes him lose a live and checks if he's not lost 3 lives yet:
Death: function(){
if(ig.game.lives == 0){
ig.game.gameOver();
}else{
ig.game.spawnEntity( EntityHero, this.startPosition.x, this.startPosition.y, ig.game.lives --);
}
},
With all of this I plan to make a score for the game and show it instantly, starting with 0, and increasing it when I kill some enemy or decreasing it when I die. Sorry If I didn't make me explicit about what I wanted, but my first question was if in the kill function there was a way to know which entity died, so I can do the
ig.game.numSentinels--;
and
ig.game.numSpikes--;
when one of them died and also making the scores and show them when I finish the level.
Page 1 of 1
« first
« previous
next ›
last »