Jump to content

how to move a group horizontally


will2m
 Share

Recommended Posts

i started working on my first game and was wondering how to move a group horizontally left and right say a few pixels (20) or so. I've posted a code sample below as the group items fall from the top and land on platforms. What I'd like to figure out is how to keep them looping left and right.

 

<script type="text/javascript">
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
 
var predators;
 
function preload() {
 game.load.image('coyote', 'assets/images/coyote.png');
}
 
function create() {
predators = game.add.group();
predators.enableBody = true;
for (var i = 0; i < 8; i++) {
   var predator = predators.create(i * 190, 200, 'coyote');
   predator.body.gravity.y = 300;
   predator.body.bounce.y = 0.4 + Math.random() * 0.2;
   //how to move horizontal (back and forth)
  }
}
 
function update() {
game.physics.arcade.collide(predators, platforms);
game.physics.arcade.overlap(player, predators, decreaseScore, null, this);
}
 
</script>
 
Link to comment
Share on other sites

are you referring to the gravity.x lines of code??  That's how I understand to move characters back and forth.  But usually it goes with a key press.   This is what I did for my first game following the discover phaser book.

create: function (    // This uses the built in phaser key bindings    this.cursor = game.input.keyboard.createCursorKeys();},movePlayer: function() {    // If the left key is pressed    if (this.cursor.left.isDown) {      // Move the player to the left      this.player.body.velocity.x = -200;    }    // Right key press    else if (this.cursor.right.isDown) {      this.player.body.velocity.x = 200;    }    // If neither is pressed    else {      // Stop player      this.player.body.velocity.x = 0;    }    // If the up arrow key is pressed and the player is touching the ground    if (this.cursor.up.isDown && this.player.body.touching.down) {      // Move the player upward (jump)      this.player.body.velocity.y = -320;    }  },
Link to comment
Share on other sites

i can get my player to move; am trying to get the predators in the above example to move left and right after they drop onto a platform. this way they are moving back and forth without user intervention and the player has to avoid them.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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