Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Best way for random movement of group objects


JackBid
 Share

Recommended Posts

I am very new to phaser but what I am trying to achieve here is lots of sprites (in a group) moving at a constant speeds in different directions. This is the code I have so far:

var game = new Phaser.Game(600, 400, Phaser.AUTO, '', { preload: preload, create: create, update: update });var enemies;var numOfEnemies = 10;function preload() {    game.load.image('gold', 'assets/gold.png');}function create() {    game.stage.backgroundColor = 0x8C8C8C;    enemies = game.add.group();    for (var i=0; i<numOfEnemies; i++) {            createEnemy();    }}function update() {    for (var i=0; i<numOfEnemies; i++) {        enemies.getAt(i).x += Math.random()*2;        enemies.getAt(i).y += Math.random()*2;    }}function createEnemy() {    enemies.create(Math.random()*(game.width-32),Math.random()*(game.height-32),"gold");}

The problem here is that the speed of each object in the group changes every update. I think the solution here would be to make it so that every object created has a set speed in the createEnemy function but it is different for each object in the group. How would I do something like this?

Link to comment
Share on other sites

I would recommend you look at the Tanks game, that uses a Group with a custom object type in it and they all have unique directions (and could easily have unique speeds too).

I spent a long time looking at lots of examples and managed to solve my problem! (On a side note I think I am beginning to realize how powerful phaser can be). Here is what i did if anyone needs it:

    Enemy = function(){        this.x = game.world.randomX;        this.y = game.world.randomY;        this.minSpeed = -75;        this.maxSpeed = 75;        this.vx = Math.random()*(this.maxSpeed - this.minSpeed+1)-this.minSpeed;        this.vy = Math.random()*(this.maxSpeed - this.minSpeed+1)-this.minSpeed;        this.enemySprite = game.add.sprite(this.x,this.y,"enemy");        this.enemySprite.anchor.setTo(0.5, 0.5);        this.enemySprite.body.collideWorldBounds = true;        this.enemySprite.body.bounce.setTo(1, 1);        this.enemySprite.body.velocity.x = this.vx;        this.enemySprite.body.velocity.y = this.vy;        this.enemySprite.body.immovable = true;    }    var game = new Phaser.Game(600, 400, Phaser.AUTO, '', { preload: preload, create: create, update: update });    var numOfEnemies = 10;    var player;    var enemies;    function preload() {        game.load.image('enemy', 'assets/ball.png');        game.load.spritesheet("player", "assets/player.png", 20, 20, 2);    }    function create() {        game.stage.backgroundColor = 0x8C8C8C;        player = game.add.sprite(100,100,"player");        player.name = "Player";        player.animations.add("pulse");        player.animations.play("pulse", 20, true);        player.anchor.setTo(0.5, 0.5);        enemies = [];        for (var i=0; i<numOfEnemies; i++) {            enemies.push( new Enemy() );        }    }    function update() {        player.x = game.input.x;        player.y = game.input.y;        for (var i=0; i<numOfEnemies; i++){            game.physics.collide(player, enemies[i].enemySprite, killPlayer, null, this)        }    }    function killPlayer() {        console.log('You Died');    }
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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