Jump to content

how to move a group horizontally


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);
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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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