Jump to content

Enemy group how to make it move back and forth


ibb671
 Share

Recommended Posts

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

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

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...