Jump to content

problem "random" sprites spawning in exact same place need help


Go to solution Solved by bryanbibat,

Recommended Posts

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 place
var Ry = Math.random() * 600; // X and Y coordinate for each colour block
var 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 input
this.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 canvas
this.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 at
this.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 at
this.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 at
this.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 post
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.

Guest
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.

Loading...
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...