Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Image file doesn't load properly

In the stage, when I change the image file from platform_1.png to platform_2.png, and preview the program in Workspaces, the file platform_2.png loads properly and is displayed on the screen, but all the platforms that are platform_1.png don't load - they're just shown as black squares with a green outline and diagonal line. What is this, and how do I fix it? Here's the code to it:

// define variables var game; var player; var platforms; var badges; var items; var cursors; var jumpButton; var text; var winningMessage; var won = false; var currentScore = 0; var winningScore = 100;

// add collectable items to the game function addItems() { items = game.add.physicsGroup(); createItem(375, 400, 'coin'); createItem(575, 500, 'coin'); createItem(225, 500, 'coin'); createItem(100, 250, 'coin'); createItem(575, 150, 'coin'); createItem(525, 300, 'coin'); createItem(650, 250, 'coin'); createItem(225, 200, 'coin'); createItem(375, 100, 'coin'); }

// add platforms to the game function addPlatforms() { platforms = game.add.physicsGroup(); platforms.create(450, 550, 'platform'); platforms.create(100, 550, 'platform'); platforms.create(300, 450, 'platform'); platforms.create(250, 150, 'platform'); platforms.create(50, 300, 'platform'); platforms.create(150, 250, 'platform'); platforms.create(650, 300, 'platform'); platforms.create(550, 200, 'platform'); platforms.create(300, 450, 'platform'); platforms.create(400, 350, 'platform'); platforms.setAll('body.immovable', true); }

// create a single animated item and add to screen function createItem(left, top, image) { var item = items.create(left, top, image); item.animations.add('spin'); item.animations.play('spin', 10, true); }

// create the winning badge and add to screen function createBadge() { badges = game.add.physicsGroup(); var badge = badges.create(750, 400, 'badge'); badge.animations.add('spin'); badge.animations.play('spin', 10, true); }

// when the player collects an item on the screen function itemHandler(player, item) { item.kill(); currentScore = currentScore + 10; if (currentScore === winningScore) { createBadge(); } }

// when the player collects the badge at the end of the game function badgeHandler(player, badge) { badge.kill(); won = true; }

// setup game when the web page loads window.onload = function () { game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render });

// before the game begins function preload() { game.stage.backgroundColor = '#5db1ad';

//Load images
game.load.image('platform1', 'platform_1.png');
game.load.image('platform2', 'platform_2.png');

//Load spritesheets
game.load.spritesheet('player', 'chalkers.png', 48, 62);
game.load.spritesheet('coin', 'coin.png', 36, 44);
game.load.spritesheet('badge', 'badge.png', 42, 54);

}

// initial game set up function create() { player = game.add.sprite(50, 600, 'player'); player.animations.add('walk'); player.anchor.setTo(0.5, 1); game.physics.arcade.enable(player); player.body.collideWorldBounds = true; player.body.gravity.y = 500;

addItems();
addPlatforms();

cursors = game.input.keyboard.createCursorKeys();
jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
text = game.add.text(16, 16, "SCORE: " + currentScore, { font: "bold 24px Arial", fill: "white" });
winningMessage = game.add.text(game.world.centerX, 275, "", { font: "bold 48px Arial", fill: "white" });
winningMessage.anchor.setTo(0.5, 1);

}

// while the game is running function update() { text.text = "SCORE: " + currentScore; game.physics.arcade.collide(player, platforms); game.physics.arcade.overlap(player, items, itemHandler); game.physics.arcade.overlap(player, badges, badgeHandler); player.body.velocity.x = 0;

// is the left cursor key presssed?
if (cursors.left.isDown) {
  player.animations.play('walk', 10, true);
  player.body.velocity.x = -300;
  player.scale.x = - 1;
}
// is the right cursor key pressed?
else if (cursors.right.isDown) {
  player.animations.play('walk', 10, true);
  player.body.velocity.x = 300;
  player.scale.x = 1;
}
// player doesn't move
else {
  player.animations.stop();
}

if (jumpButton.isDown && (player.body.onFloor() || player.body.touching.down)) {
  player.body.velocity.y = -400;
}
// when the player winw the game
if (won) {
  winningMessage.text = "YOU WIN!!!";
}

}

function render() {

}

};

Hope someone can fix it OTL