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

8 years ago by janix2011

Hey guys. I am working on enemy ai in my game, but I am still fairly new with all of this. I included, what I believe to be, the correct code in the update function to get my enemies to fire at me within a certain distance. I am getting a console error that says "other is undefined" in entity.js line 192. So I am guessing there is a problem with how I am referencing the .distanceTo. Would you guys take a look please, and let me know what I am doing wrong here? Thanks.

ig.module(
   'game.entities.eye'
)
.requires (
   'impact.entity'
)

.defines(function() {

EntityEye = ig.Entity.extend({
   
   size: {x:16, y:16},
   maxVel: {x: 60, y: 0},
   health: 20,
   flip: true,
   friction: {x:300, y:300},
   collides: ig.Entity.COLLIDES.PASSIVE,
   type: ig.Entity.TYPE.B,
   checkAgainst: ig.Entity.TYPE.A,
   check: function( other ) {
      if( other instanceof EntityPlayer ) {
         other.receiveDamage(10, this);
         other.currentAnim = other.anims.flinch;
         if( other.pos.x > this.pos.x ) {
            // Flinch right
            other.vel.y = -150;
            other.vel.x = 150;
                
         }
         else {
            // Flinch left
            other.vel.y = -150;
            other.vel.x = -150;
         }
      }
   },
   
   animSheet: new ig.AnimationSheet('media/eye.png',16,16),
    
    init: function(x,y,settings){
        this.parent(x,y,settings );
        
        this.addAnim('run',.8,[0,1]); 
    },
    
    update: function() {
    // near an edge? return!
        if( !ig.game.collisionMap.getTile(this.pos.x + (this.flip ? +4 : this.size.x -4),
            this.pos.y + this.size.y+1
          ))
        {
        this.flip = !this.flip;
    }
        var xdir = this.flip ? -1 : 1;
        this.accel.x = 60 * xdir;
        
        if(this.distanceTo(ig.game.player) < 200) {
            ig.game.spawnEntity( EntityBeam, this.pos.x + 5, this.pos.y, {flip:this.flip});
        }
        
        this.currentAnim.flip.x = this.flip; 
        this.parent();
    },
    
    handleMovementTrace: function( res ) {
         this.parent( res );
         // collision with a wall? return!
         if( res.collision.x ) {
            this.flip = !this.flip;
         }
    },
});

   EntityBeam = ig.Entity.extend({
      size: {x:8, y:4},
      animSheet: new ig.AnimationSheet('media/beam.png',8 ,4),
      maxVel: {x:150, y:0 },
      type: ig.Entity.TYPE.NONE,
      checkAgainst: ig.Entity.TYPE.A,
      collides: ig.Entity.COLLIDES.PASSIVE,
      check: function(other) {
         other.receiveDamage(10, this );
         this.kill();
      },
      
      init: function( x, y, settings ) {
         this.parent( x + (settings.flip ? -4 : 15) , y+8, settings );
         this.vel.x = this.accel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x);
         this.addAnim( 'idle', 0.2, [0] );
      },
   
      handleMovementTrace: function( res ) {
         this.parent(res);
         if(res.collision.x || res.collision.y) {
         this.kill();
         }
      }  
   });

});

8 years ago by lazer

I think the problem is that "instanceof" in your check function should be "instanceOf" (case sensitive - someone please correct me if I'm wrong).

8 years ago by stahlmanDesign

@lazer instanceof all lowercase is correct

example:
for (i=0;i<ig.game.entities.length;i++){
		if (ig.game.entities[i] instanceof EntityFish){
			imaFish = ig.game.entities[i];
		}

8 years ago by stahlmanDesign

What you need to do is this:

instead of ig.game.player

use ig.game.getEntitiesByType(EntityPlayer)[0];

if(this.distanceTo(ig.game.player) < 200) {
            ig.game.spawnEntity( EntityBeam, this.pos.x + 5, this.pos.y, {flip:this.flip});
        }


becomes
var player = ig.game.getEntitiesByType(EntityPlayer)[0];
if(this.distanceTo(player) < 200) {
            ig.game.spawnEntity( EntityBeam, this.pos.x + 5, this.pos.y, {flip:this.flip});
        }

7 years ago by benkz

Hello Guys,
first of all thanks to Dominic for this great Framework. Great fun so far playing with it.

I'm a Impact & JS newbie you could say and I tried to create an entity with the code above, i added a timer, which i found in the Space Invader source.

But now each time my player is killed, firebug says, (each frame) "player is undefined!
so the game stops and my player doesn't respawn.
I found a thread here in the forum (1 year old, "getEntityByName() issue")
it dealt with the same problem but i couldn't figure it out so far.

Would be great if anyone could give me advice, thanks.

Here is a code snippet , part from the update function from the "enemyEntity"


 if(this.distanceTo(ig.game.getEntitiesByType(EntityPlayer)[0]) < 200 && this.canShoot) {
                    ig.game.spawnEntity( EntityBeam, this.pos.x + 5, this.pos.y, {flip:this.flip});
                    this.canShoot = false;
                    this.fireCooldown.reset();


                }

7 years ago by Datamosh

mmh...
I'm going to contradict stahlmanDesign, maybe in your case it works

On player init add: ig.game.player = this; and replace ig.game.getEntitiesByType(EntityPlayer)[0] with ig.game.player

7 years ago by benkz

Thanks a lot Datamosch , this did the job.

7 years ago by fulvio

Doing the following in your main.js init() will allow you to use ig.game.player globally as well:

player: null,
init: function() {
    this.player = this.getEntitiesByType(EntityPlayer)[0];
}

Then you can access ig.game.player anywhere.

Nevertheless it's probably just easier to use Datamosch's code and set the EntityPlayer entity to ig.game.player.

7 years ago by RationalGaze

How is it possible to access ig.game.player data ? (size, pos.x, pos.y, etc)

for exemple

if(this.distanceTo(ig.game.player) < 100) {
// change ig.game.player position on X;
//change ig.game.player position on Y;
}

7 years ago by RationalGaze

its ok finally.
Page 1 of 1
« first « previous next › last »