1 decade ago by Rungo73
Hi there,
Probably a really basic question..
I have an enemy entity that I have placed in a level a couple of times. Its a basic patrolling entity. When it hits a wall or edge it waits before flipping and continuing on.
Problem is that all iterations of the same enemy entity that I put in the level end up all running off the same timer instead of their individual timers. Like I've declared the timer globally.
Can anyone show me where I've gone wrong with my code?
Probably a really basic question..
I have an enemy entity that I have placed in a level a couple of times. Its a basic patrolling entity. When it hits a wall or edge it waits before flipping and continuing on.
Problem is that all iterations of the same enemy entity that I put in the level end up all running off the same timer instead of their individual timers. Like I've declared the timer globally.
Can anyone show me where I've gone wrong with my code?
ig.module( 'game.entities.enemy' ) .requires( 'impact.entity', 'impact.font' ) .defines(function() { EntityEnemy = ig.Entity.extend({ type: ig.Entity.TYPE.B, checkAgainst: ig.Entity.TYPE.A, collides: ig.Entity.COLLIDES.PASSIVE, size: {x: 14, y: 16}, offset: {x: 2, y: -1}, maxVel: {x: 75, y: 175}, friction: {x: 150, y: 0}, _gravity: 700, flip: false, speed: 55, Xstart: null, Ystart: null, xdir: true, chasing: false, rnd: null, id: null, onGuard: false, xCollide: false, animSheet: new ig.AnimationSheet('media/samurai_2.png', 20, 16), init: function(x, y, settings) { this.parent(x, y, settings); this.addAnim( 'idle', .09, [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3] ); this.addAnim('walk', .07, [ 0, 1, 2, 1]); this.addAnim('spike', .07, [7, 8, 9, 8]); this.addAnim('guard', .09, [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 5, 5, 5, 5, 5, 6, 5, 5, 5, 5, 5, 5, 5, 5]); ig.game.enemy = this; this.Xstart = this.pos.x; this.Ystart = this.pos.y; this.accel.y = this._gravity; this.delayTimer = new ig.Timer(1.5); this.delay = 1.5; this.rnd = Math.floor(Math.random()*4); }, update: function(){ var _target = ig.game.player; this.patrol(); // collision with tilemap, egdes etc if(!ig.game.collisionMap.getTile(this.pos.x + (!this.flip?+0: this.size.x ), this.pos.y + this.size.y+1)) { this.vel.x = 0; this.wait(); }; if (this.distanceTo(_target) < 64 && _target.pos.x < this.pos.x && !this.flip || this.distanceTo(_target) < 64 && _target.pos.x > this.pos.x && this.flip) { this.onGuard = true; } else { this.wait(); }; this.currentAnim.flip.x = this.flip; this.animation(); this.parent(); }, patrol: function(){ if(this.onGuard){ this.xdir = this.flip? 1: -1; this.vel.x = (this.speed + 40)*this.xdir; } else { this.xdir = this.flip? 1: -1; this.vel.x = this.speed*this.xdir; } }, animation: function(){ if (this.vel.x !=0 && !this.onGuard){ this.currentAnim = this.anims.walk; } else if ( this.vel.x == 0 && !this.onGuard){ this.currentAnim = this.anims.idle; } else if ( this.vel.x == 0 && this.onGuard){ this.currentAnim = this.anims.guard; } else if ( this.vel.x !=0 && this.onGuard){ this.currentAnim = this.anims.spike; } }, handleMovementTrace: function(res){ if(res.collision.x){ if (this.onGuard){ this.currentAnim = this.anims.guard; } else{ this.currentAnim = this.anims.idle; } } this.parent(res); }, wait: function(){ if(this.delayTimer.delta() > this.delay){ this.onGuard = false; this.flip = !this.flip; this.delayTimer.set(this.delay); this.patrol(); } }, }); });