1 decade 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(); } } }); });