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 trialosaydabushoke
392 Pointsit don't work when trying to get the new platform on my game
platform 2 doesnt work
Steven Parker
231,236 PointsPlease show your code and provide a link to the challenge page.
Aaron B.
1,063 PointsI also have this problem. And there is a strange invisible wall on one of the platforms. I reloaded the workspace, started from scratch and made the edits, which are only 4 bits, but it doesn't work.
// 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, 'platform2');
platforms.create(650, 300, 'platform2');
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('platform', 'platform_1.png');
game.load.image('platform', '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() {
}
};
Steven Parker
231,236 PointsAaron B. — You should create a new question instead of posting a comment to an old one. Otherwise, it's likely to only be seen by folks who asked, commented, or answered the old one.
Also, it would be very helpful in diagnosing your issue if you post a link to a workspace snapshot. That also saves you from needing to include the code in-line.
Ali Abbas
2,097 PointsHey Aaron, after looking at your code I realised your problem. The problem is inbetween lines 80-90. Where it says " game.load.image( 'platform', 'platform_2.png' ); ". It should say 'platform2' and not 'platform', otherwise you're declaring 2 variables with the same name. The line above has the same variable name. I hope I have clarified the issue and I hope this fixes your problem. Have a great day!
Haley Ward
Treehouse Project ReviewerHaley Ward
Treehouse Project ReviewerCan you please post a snippet of your workspaces, so we can take a look at your code?