Search the Community
Showing results for tags 'Death counter'.
-
Okay so current project I'm working on is to make a platformer game which uses just a single level design but multiple stages featuring different mechanics in it. I have a death counter which counts the number of times the player jumps into the spikes from which upon dying they will respawn back at the beginning of the level I also have a timer which is counting the amount of time it take to complete the level. Now what I want to do is make it so upon reaching the pipes at the end of the level when they switch over to the next level the values from the death counter & level timer will carry over to the next level. So e.g. Level 1 - Stage 1 = 20 deaths and a finishing time of 50 seconds. Upon starting Level 1 - Stage 2 the death counter will start at 20 and the level timer will resume counting from 50 seconds. instead of at 0. Currently they're simply resetting back to 0 as you can see in the create function below. How would I go about doing this as I noticed there's not anything in the examples that can show me how to do this? My game can be accessible from http://mbrooks.bitbucket.org Code as follows - BasicGame.Game = function (game){ // When a State is added to Phaser it automatically has the following properties set on it, even if they already exist: this.game; // a reference to the currently running game this.add; // used to add sprites, text, groups, etc this.camera; // a reference to the game camera this.cache; // the game cache this.input; // the global input manager (you can access this.input.keyboard, this.input.mouse, as well from it) this.load; // for preloading assets this.math; // lots of useful common math operations this.sound; // the sound manager - add a sound, play one, set-up markers, etc this.stage; // the game stage this.time; // the clock this.tweens; // the tween manager this.state; // the state manager this.world; // the game world this.particles; // the particle manager this.physics; // the physics manager this.rnd; // the repeatable random number generator this.map; this.layer; this.player; this.cursors; this.spikes; this.deathCounter; this.spikesCollision = false; this.stageTip; this.stageNumber; this.deathText; this.levelText; this.tipText; this.timeText; this.levelStartTime = 0; this.levelCurrentTime = 0;// this.deathTemp;// this.total; //this.timer; // You can use any of these from any function within this State. // But do consider them as being 'reserved words', i.e. don't create a property for your own game called "world" or you'll over-write the world reference.};BasicGame.Game.prototype ={ create: function () { console.log('Creating game state!'); //Change the stagebackgroundColour to classic Mario blue. //this.stage.backgroundColor = '#6888ff'; //Populate the local map attribute with the tilemap with they key "level" map = this.add.tilemap('level-' + BasicGame.current_level_id); //map = this.add.tilemap('level' + BasicGame.current_level_id); //Assign the "level_tiles" tileset to the tilemap data map.addTilesetImage('level_design', 'level-' + BasicGame.current_level_id); //map.addTilesetImage('level_design', 'level_tiles' + BasicGame.current_level_id); //Populate the local layer with the combined level data. layer = map.createLayer('level_design'); //Resize the game world to fit the level data. layer.resizeWorld(); this.physics.startSystem(Phaser.Physics.ARCADE); map.setTileIndexCallback([12,13,14,15], this.PlayerDie, this); map.setTileIndexCallback([21, 25,26], this.nextStage, this); map.setTileIndexCallback(29, this.ButtonPressed, this); map.setCollision([0,1,2,3,4,5,25,26,27,28], true); player = this.add.sprite(135, 110, 'mario'); //total = 0; deathCounter = 0; // deathCounter = deathTemp; this.physics.arcade.enable([player]); this.physics.arcade.gravity.y = 500; player.body.collideWorldBounds =true; player.body.setSize(12, 16, 2, 0); cursors = this.input.keyboard.createCursorKeys(); player.animations.add('run',[0,1,2],10,true); player.animations.add('stand',[6]); //Shows the stage number for this stage deathText = this.game.add.text(1050, 725, 'Total Deaths: 0', { font: '20px Arial', fill: '#fff', align: 'center' }); if (BasicGame.current_level_id == 1) { levelText = this.game.add.text(125, 725, 'Level 1 - Stage 1', { font: '25px Arial', fill: '#fff', align: 'center' }); tipText = this.game.add.text(125, 770, 'Normal Movement', { font: '20px Arial', fill: '#fff', align: 'center' }); } else if (BasicGame.current_level_id == 2) { levelText = this.game.add.text(125,725, 'Level 1 - Stage 2', { font: '25px Arial', fill: '#fff', align: 'center' }); tipText = this.game.add.text(125,770, 'Inverted Movement', { font: '20px Arial', fill: '#fff', align: 'center' }); } /* else if (BasicGame.current_level_id == 2) { levelText = this.game.add.text(125,725, 'Level 1 - Stage 2', { font: '25px Arial', fill: '#fff', align: 'center' }); tipText = this.game.add.text(125,770, 'Inverted Movement / Verted Movement Timer', { font: '20px Arial', fill: '#fff', align: 'center' }); this.timer.game.time.create(false); this.timer.loop(2000, update, this); this.timer.start(); }*/ timeText = this.game.add.text(1050, 770, 'Time: ' + 's', { font: '20px Arial', fill: '#fff', align: 'center' }); this.levelStartTime = this.game.time.time; }, update: function () { // Honestly, just about anything could go here. It's YOUR game after all. Eat your heart out! this.physics.arcade.collide(player, layer); if (BasicGame.current_level_id == 1) { if (cursors.left.isDown) { //Give the player's physics body some velocity on the x (horizontal axis). player.body.velocity.x = -175; //Play the run animation player.animations.play('run'); } //Check if the right arrow is down else if (cursors.right.isDown) { //Give the player's physics body some velocity on the x (horizontal axis). player.body.velocity.x = 175; //Play the run animation player.animations.play('run'); } //Neither direction is pressed. else { //Remove the player's physics body's horizontal velocity. player.body.velocity.x = 0; //Play the Stand animation player.animations.play('stand'); } //if the up arrow is pressed and mario is touching another physics body below. if (cursors.up.isDown && player.body.onFloor()) { //Give the player's physics body some vertical velocity. player.body.velocity.y = -375; } } else if (BasicGame.current_level_id == 2) { if (cursors.left.isDown) { //Give the player's physics body some velocity on the x (horizontal axis). player.body.velocity.x = 175; //Play the run animation player.animations.play('run'); } //Check if the right arrow is down else if (cursors.right.isDown) { //Give the player's physics body some velocity on the x (horizontal axis). player.body.velocity.x = -175; //Play the run animation player.animations.play('run'); } //Neither direction is pressed. else { //Remove the player's physics body's horizontal velocity. player.body.velocity.x = 0; //Play the Stand animation player.animations.play('stand'); } //if the up arrow is pressed and mario is touching another physics body below. if (cursors.up.isDown && player.body.onFloor()) { //Give the player's physics body some vertical velocity. player.body.velocity.y = -375; } } /* else if (BasicGame.current_level_id == 2) { total++ // if(total == 1) //{ if (cursors.left.isDown) { //Give the player's physics body some velocity on the x (horizontal axis). player.body.velocity.x = -175; //Play the run animation player.animations.play('run'); } //Check if the right arrow is down else if (cursors.right.isDown) { //Give the player's physics body some velocity on the x (horizontal axis). player.body.velocity.x = 175; //Play the run animation player.animations.play('run'); } //Neither direction is pressed. else { //Remove the player's physics body's horizontal velocity. player.body.velocity.x = 0; //Play the Stand animation player.animations.play('stand'); } //if the up arrow is pressed and mario is touching another physics body below. if (cursors.up.isDown && player.body.onFloor()) { //Give the player's physics body some vertical velocity. player.body.velocity.y = -375; } //} if (total == 2) { if (cursors.left.isDown) { //Give the player's physics body some velocity on the x (horizontal axis). player.body.velocity.x = 175; //Play the run animation player.animations.play('run'); } //Check if the right arrow is down else if (cursors.right.isDown) { //Give the player's physics body some velocity on the x (horizontal axis). player.body.velocity.x = -175; //Play the run animation player.animations.play('run'); } //Neither direction is pressed. else { //Remove the player's physics body's horizontal velocity. player.body.velocity.x = 0; //Play the Stand animation player.animations.play('stand'); } //if the up arrow is pressed and mario is touching another physics body below. if (cursors.up.isDown && player.body.onFloor()) { //Give the player's physics body some vertical velocity. player.body.velocity.y = -375; } total = 0; } }*/ this.levelCurrentTime = this.game.time.time - this.levelStartTime; timeText.setText('Time: ' + this.levelCurrentTime/1000 + 's') }, ButtonPressed: function(sprite, tile) { map.replace(29, 31); map.replace(19, 41); }, nextStage: function(sprite, tile) { console.log("Next Level!"); BasicGame.current_level_id++; // deathTemp = deathCounter; this.game.world.setBounds(0, 0, 0, 0); //This Ensures the Level Bounds are reset this.game.state.start("Game", true, false); }, PlayerDie: function(sprite, tile) { deathCounter++; deathText.setText('Total Deaths: ' + deathCounter); sprite.kill(); player.reset(135, 110) map.replace(41, 19); map.replace(31, 29); }, quitGame: function (pointer) { // Here you should destroy anything you no longer need. // Stop music, delete sprites, purge caches, free resources, all that good stuff. // Then let's go back to the main menu. this.state.start('MainMenu'); }}
- 2 replies
-
- Timer
- Death counter
-
(and 4 more)
Tagged with: