Jump to content

assets not loading but no errors in console please help


callidus
 Share

Recommended Posts

I'm trying to spawn a randomly coloured block and to do so I am using math.random and the console says everything is loading but 

the assets aren't loading. There are no errors in it but can anyone find an error that might be causing the assets not to load? Parts of the code that's relevant is coloured 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 x = Math.floor(Math.random () * 800); // ball spawns in a random place
var y = Math.floor(Math.random() * 600);
var text; 
var colour = Math.random() * 3;
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();
 
block = this.game.add.sprite(x,y,"colour"); // x and y are randomy coordinates the ball spawns at
this.game.physics.arcade.enable(block);
block.body.velocity.setTo(200,200);
block.body.collideWorldBounds = true; 
block.body.bounce.set(1);
 
player = this.game.add.sprite(10,10,"block");
this.game.physics.arcade.enable(player);
player.scale.setTo(0.2,0.2);
player.body.collideWorldBounds = true;
}
 
function update() {
 
player.body.velocity.x = 0;
player.body.velocity.y = 0;
 
if(colour < 1){
colour = "redBlock";
}
 
if(colour < 2){
colour = "blueBlock";
}
 
if(colour < 3){
colour = "greenBlock";
}
 
if(cursors.left.isDown){
player.body.velocity.x += -200; 
}
 
if(cursors.right.isDown){
player.body.velocity.x += 200;
}
 
if(cursors.down.isDown){
player.body.velocity.y += 200;
}
 
if(cursors.up.isDown){
player.body.velocity.y += -200;
}
}
 
 
Link to comment
Share on other sites

You are adding a sprite with the key "colour" but loading in no image with that key, you have "block", "redBlock", "greenBlock", and "blueBlock". Change the line to this.

block = this.game.add.sprite(x,y,"redBlock"); // x and y are randomly coordinates the ball spawns at

and you will see the block displayed. An easy fix for what you want would be to generate a random number and add the sprite based on that.
 

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', {  preload: preload,  create: create,  update: update});var cursors;function preload() {  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;  this.game.scale.pageAlignVertically = true;  this.game.scale.refresh();  cursors = game.input.keyboard.createCursorKeys();  var x = Math.floor(Math.random() * 800);  var y = Math.floor(Math.random() * 600);  var colours = ['redBlock', 'greenBlock', 'blueBlock'];    //Access random index from [0..2] in colours array  var colour = colours[Math.round(Math.random() * 2)];  //Add sprite based on colour selected from colours array  var block = this.game.add.sprite(x, y, colour);  this.game.physics.arcade.enable(block);  block.body.velocity.setTo(200, 200);  block.body.collideWorldBounds = true;   block.body.bounce.set(1);   var player = this.game.add.sprite(10, 10, 'block');  this.game.physics.arcade.enable(player);  player.scale.setTo(0.2, 0.2);  player.body.collideWorldBounds = true;} function update() {  player.body.velocity.setTo(0);   if(cursors.left.isDown) {    player.body.velocity.x += -200;   }  if(cursors.right.isDown) {    player.body.velocity.x += 200;  }  if(cursors.down.isDown){    player.body.velocity.y += 200;  }  if(cursors.up.isDown){    player.body.velocity.y += -200;  }}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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