Hi,
In my enemy entity file I have one more entity that does not need to be placed in Weltmeister; hence I included it in the same file following the enemy code, and it can get spawned by the enemy code.
The thing is I would like to get access to the enemy's variable from this entity that got spawned, so I can send it to main. I used this code however it does work:
ig.game.damage = this.damage;
I understand it has to do with the second part. Any idea what to use instead of 'this'?
1 decade ago
by Arantor
I'm having a hard time understanding what you're trying to do with this piece of code, if you're passing it to ig.game.damage, presumably there's only one of these entities in place?
If so, something like this should do the trick:
var object = ig.game.getEntitiesByType( EntityObject )[0];
Change the names, of course, to suit. Also, if your entity has a name property defined, e.g. EntityObject.name = 'myentity', you will also be able to access that from ig.game.namedEntities['myentity'].
Also, as another option, when it's spawned, you can save the resulting entity to ig.game in some fashion, e.g. ig.game.thisentity = ig.game.spawnEntity(...); and then reference it that way.
The enemy entity is the parent one. Then I got some child enteties of this one - each in its own entity file, where each one has a different damage value. So when they shoot and the player gets a hit from one of them, it should take the damage value from the one that hit the player and this is evaluated in the check function of the spawned entity (which is in the same entity file as the enemy entity) and from this check function where the evaluation happened the correct damage value should be passed to main, so it can be displayed on screen.
Until now I had results like displaying on screen 'NaN', or taking the damage value of one child entity for all of them, or like the last try with the code stated above where I defined different object variables for different child enteties and here the result was that if the player got hit the summation of all the damages was given to the player and I cannot figure out so far how to take the damage of just the one shoot that hit the player, to be more specific to give the damage of the child entity that spawned that shot.
Does this make any sense? Any ideas?
1 decade ago
by Arantor
In which case, I'd move the logic to the player entity's check method, and do it simply as:
check: function (other) {
if (other instanceof EntityEnemy)
{
this.receiveDamage(other.damage);
}
}
Plus then you can reference it from other.damage and move it to ig.game somewhere if you want to. Note that EntityEnemy should be whatever the parent class is called since all the children should be inheriting the parent classes too. (E.g. EntityChild that extends EntityParent that extends ig.Entity should all return true for instanceof ig.Entity)
1 decade ago
by gxxaxx
What do you think about using the check() method of the enemy entities.
check: function (other) {
if (other instanceof EntityPlayer) {
other.receiveDamage(this.damage);
}
}
My player.js file is so bloated I've started moving code code into other files whenever appropriate.
You see any reason this would be a bad plan?
1 decade ago
by Arantor
In this particular case I was going for doing it in the player's entity because that way you have only one point where everything that has to do this will converge (since it's always enemy attacks on the player)
I'd generally advocate keeping the player's check() method lean if possible because presumably, a lot more is likely to collide with the player specifically... so you don't want to have the collision code evaluate every possible type of collision, only the ones that actually collide.
In your case, gxxaxx, it sounds like using the enemy's entity for delivering damage (rather than the player's entity for collisions to receive damage, as it were) would be preferable.
Especially if you can draft a more generic class higher up that all enemies can derive from that all have a common enough update() function (and any other generic behaviours)
In the player's check function I started to run into the same problems as I did when I had the code in the entity enemy file. Additionally some new issues came up having this code in the player entity. So I moved it back to the enemy entity. Then I started again to 'play' around with additional variables and finally succeeded.
@Arantor: As always thanks for your support!
@gxxaxx: Having the code where it belongs is in my opinion a very wise decision and should be followed always, only exception if there is no other option available to make it work.
Page 1 of 1
« first
« previous
next ›
last »