callidus Posted August 22, 2014 Share Posted August 22, 2014 In my code im trying to have 3 diff colour blocks spawn in various areas on the screen, but one of the three red blocks spawns exactly on top of a blue one every single time. It makes no sense as I am using a math.random and have even used a game.add.text to make sure that the numbers aren't exactly the same yet it keeps happening. The relevant code is in green, thanks for any help. My code: var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update,});var Rx = Math.random () * 800; // ball spawns in a random placevar Ry = Math.random() * 600; // X and Y coordinate for each colour blockvar Bx = Math.random () * 800; var By = Math.random() * 600;var Gx = Math.random () * 800 var Gy = Math.random() * 600;var text; var colours = ["redBlock","greenBlock","blueBlock"];var colour = colours[Math.floor(Math.random()*2)]; function preload() {cursors = game.input.keyboard.createCursorKeys(); //allows keyboard inputthis.game.load.image("block","assets/block.png");this.game.load.image("redBlock","assets/red.png");this.game.load.image("greenBlock","assets/green.png");this.game.load.image("blueBlock","assets/blue.png");} function create() {this.game.physics.startSystem(Phaser.Physics.ARCADE);this.game.scale.pageAlignHorizontally = true; //centers canvasthis.game.scale.pageAlignVertically = true;this.game.scale.refresh(); for (var i = 0; i < 3 ; i++){Rblock = this.game.add.sprite(i * Rx,i * Ry,"redBlock"); // x and y are randomy coordinates the ball spawns atthis.game.physics.arcade.enable(Rblock);Rblock.body.velocity.setTo(200,200);Rblock.body.collideWorldBounds = true; Rblock.body.bounce.set(1);} for (var i = 0; i < 3; i++){Bblock = this.game.add.sprite(i * Bx,i * By,"blueBlock"); // x and y are randomy coordinates the ball spawns atthis.game.physics.arcade.enable(Bblock);Bblock.body.velocity.setTo(200,200);Bblock.body.collideWorldBounds = true; Bblock.body.bounce.set(1);} for (var i = 0; i < 3 ; i++){Gblock = this.game.add.sprite(i * Gx,i * Gy,"greenBlock"); // x and y are randomy coordinates the ball spawns atthis.game.physics.arcade.enable(Gblock);Gblock.body.velocity.setTo(200,200);Gblock.body.collideWorldBounds = true; Gblock.body.bounce.set(1);} player = this.game.add.sprite(10,10,"block");this.game.physics.arcade.enable(player);player.anchor.set(0.5);player.body.drag.set(100);player.body.maxVelocity.set(300);//player.scale.setTo(0.2,0.2);player.body.collideWorldBounds = true; game.add.text(16,16,Rx + Ry);game.add.text(16,40,Bx + By);} function update() { if (cursors.up.isDown){this.game.physics.arcade.accelerationFromRotation(player.rotation,200,player.body.acceleration);}else{player.body.acceleration.set(0);}if (cursors.left.isDown){player.body.angularVelocity = -300} else if(cursors.right.isDown){player.body.angularVelocity = 300;}else{player.body.angularVelocity = 0;}} Link to comment Share on other sites More sharing options...
bryanbibat Posted August 22, 2014 Share Posted August 22, 2014 for (var i = 0; i < 3 ; i++){Rblock = this.game.add.sprite(i * Rx,i * Ry,"redBlock");... for (var i = 0; i < 3; i++){Bblock = this.game.add.sprite(i * Bx,i * By,"blueBlock");Zero multiplied to anything is always zero. callidus 1 Link to comment Share on other sites More sharing options...
callidus Posted August 22, 2014 Author Share Posted August 22, 2014 *face-palm*, thanks, didn't even notice the for loop started at 0. Link to comment Share on other sites More sharing options...
Recommended Posts