What's new
Carbonite

Welcome to Carbonite! Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Help out a noob

UnBreakable_D

Audio Addict
Rating - 100%
47   0   0
Joined
Oct 8, 2014
Messages
971
Reaction score
136
Points
1,135
Age
32
Location
Pretoria
Hey guys , so ive got the following code just started learning some javascript and for some reason its crashing at the last minute.

it is giving a console error on the function deathMessage() stating that its undefined. anyone have any ideas where I went wrong. everything else works, I am getting the right mechanics for losing lives etc but as soon as the last life is lost in this case when var lives === 0 its meant to call the function deathMessage() and pop up a "GAME OVER" text onscreen.

// define variables
var game;
var player;
var platforms;
var badges;
var items;
var cursors;
var jumpButton;
var text;
var text;
var winningMessage;
var deathMessage;
var won = false;
var lives = 3;
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, 'poison');
createItem(370,500,'poison');
createItem(100, 375, 'poison');
createItem(125, 50, 'star');
}

// 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, 'platform2');
platforms.create(300, 450, 'platform2');
platforms.create(400, 350, 'platform2');
platforms.create(100, 100, 'platform2');
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();
if (item.key === 'coin') {
currentScore = currentScore + 10;
} else if (item.key === 'poison') {
lives = lives - 1;
} else if (item.key === 'star') {
currentScore = currentScore + 20;
}
if (currentScore === winningScore) {
createBadge();
}
if (lives == 0) {
deathMessage();
}
}


// 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('platform2', 'platform_2.png');

//Load spritesheets
game.load.spritesheet('player', 'mikethefrog.png', 32, 32);
game.load.spritesheet('coin', 'coin.png', 36, 44);
game.load.spritesheet('badge', 'badge.png', 42, 54);
game.load.spritesheet('poison', 'poison.png', 32, 32);
game.load.spritesheet('star', 'star.png', 32, 32);
}

// 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"});
text2 = game.add.text(300, 16, "LIVES: " + lives, { font: "bold 24px Arial", fill: "white"});
winningMessage = game.add.text(game.world.centerX, 275, "", { font: "bold 48px Arial", fill: "white" });
deathMessage = game.add.text(game.world.centerX, 275, "", { font: "bold 48px Arial", fill: "white" });

winningMessage.anchor.setTo(0.5, 1);
deathMessage.anchor.setTo(0.5, 1);
}

// while the game is running
function update() {
text.text = "SCORE: " + currentScore;
text2.text = "LIVES: " + lives;
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 wins the game
if (won) {
winningMessage.text = "YOU WIN!!!";
}
if (lives ==0 ){
deathMessage.text = " GAME OVER!";
}
}

function render() {

}

};
 

spydr97

Junior Member
Rating - 0%
0   0   0
Joined
Dec 6, 2018
Messages
18
Reaction score
3
Points
385
Age
20
Location
Mondeor, Johannesburg
Not too proficient in JS but from what I can see you're trying to reference the variable but you're calling it as a function. Seems like you implemented the same thing correctly in line 169. Why not use the same implementation on line 81?
 

DarrynB

Well-Known member
Rating - 100%
57   0   0
Joined
Nov 5, 2010
Messages
1,327
Reaction score
10
Points
1,585
Location
JHB
spydr97 spydr97 has it right. deathMessage is a var not a function. Just comment out the line deathMessage();
 

Top Donors

$200.00
$145.00
$82.00
Top