ibb671 Posted May 8, 2016 Share Posted May 8, 2016 Hello if anybody can help me I would like to know how to make an enemy within a group move back and forth a certain amount of steps. Also the enemy will only move if there is nothing blocking its way. This is what I have so far I have put this code in the update method and so far the enemies just fly off the screen to the right. Thank you guys in advance. this.steps=0; this.enemies.forEachAlive(function(element){ if(element.body.blocked.left==false && this.steps<=20 && element.body.blocked.right==false){ this.steps++; console.log(this.steps); element.body.velocity.x+=this.steps; } if(this.steps>=20){ this.steps--; console.log(this.steps + "im minusing"); element.body.velocity.x-=this.steps; } },this); Link to comment Share on other sites More sharing options...
ibb671 Posted May 9, 2016 Author Share Posted May 9, 2016 ok so i was able to get help and was advised to use a tween i was able to use something like this var enemyTween = this.game.add.tween(this.enemies).to({ x: this.game.width / 2 + 50 }, 4000, "Linear", true, 0, -1); enemyTween.yoyo(true); Are there any other ways to make an enemy move around back and forth this way without a tween? Link to comment Share on other sites More sharing options...
Recommended Posts