Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by sanity

I am currently having some trouble with getting my enemy entity to spawn on a timer after death. Currently when the zombie dies the timer will start and when i hits 0 the text displayed from the draw function respawns but the zombie/animation does not, and without that you cant damage the entity. Also i have figured out how to set global variables inside of the main.js and use them within my entities but, how do i do the opposite and use variables like the default health variable associated with entities? I have tried using .getEntityByName and .getEntitiesByType to pass the entity into a variable inside the main.js but i keep getting errors. if anyone can find a problem with my code or have a better way of doing it i would love to see an example.

zombie.js

        draw: function(){
          var x = ig.system.width/2;
          var y = ig.system.height/2 - 20;

          this.statText.draw('Level: '+ig.game.zombielevel, x+45, y-30, ig.Font.ALIGN.CENTER);
          this.statText.draw('Health: '+this.health, x+45, y-20, ig.Font.ALIGN.CENTER);
          this.statText.draw('Attack: '+ig.game.zombieattack, x+45, y-10, ig.Font.ALIGN.CENTER);
          this.statText.draw('Defense: '+ig.game.zombiedefense, x+45, y, ig.Font.ALIGN.CENTER);
          this.statText.draw('Experience: '+ig.game.zombieexperience, x+45, y+10, ig.Font.ALIGN.CENTER);
          this.parent();
        },

        update: function(){
         if(this.health > 2){ //This was just to see if i could get the timer to pause/unpause
          ig.game.deathtimer.pause();
         }else if( this.health < 2 ){
          ig.game.deathtimer.reset();
         }



         this.parent();

        },


        kill: function(){
          //Declare what you get on death
          ig.game.deathtimer.reset();
          ig.game.playerexperience = ig.game.playerexperience + ig.game.zombieexperience;
          this.parent();
 }




Main.js

	update: function() {


          if( this.deathtimer.delta() >= 0 ){
          ig.game.spawnEntity( EntityZombie, this.startPosition.x, this.startPosition.y );
          this.deathtimer.reset();
          this.parent();
          }

                this.parent();
 }

1 decade ago by Joncom

Consider something like this:

// Kills zombie, and spawns a new one at 0,0 after 1 second.
kill: function() {
    setTimeout(
        function() {
            ig.game.spawnEntity(EntityZombie, 0, 0);
        }, 1000
    );
    this.parent();
}

1 decade ago by lazer

For your question regarding getting an entity's variable from outside of the entity:

Let's say you have a player entity. You want to get its health from main.js. A couple of ways of doing this would be:

1) In the player's init function:

ig.game.player = this;

You can then refer to ig.game.player.health wherever you want (or ig.game.player.anyotherattribute)

OR

2) in main.js:
var player = ig.game.getEntitiesByType(EntityPlayer);
if (player) {
    var health = player.health;
}

1 decade ago by sanity

ah, Thank you. Both of you have helped me a lot.
Page 1 of 1
« first « previous next › last »