Jump to content

Make Death counter & Timer carry over to next levels


Dragonfly3r
 Share

Recommended Posts

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');  }}
Link to comment
Share on other sites

Do it the same way you're using BasicGame.current_level_id ie by creating a global variable to hold the values.

Just initialise it's starting value in boot.js (assuming you're still using the rest of the phaser template's structure - this will keep you globals together.) it will then be global and accessible everywhere. Then set it wherever you need to and it will persist and can be accessed even after you change state. As create is run every time you restart the state don't set the counter to anything there as that will defeat your objective - only set it where there is a need to.

Link to comment
Share on other sites

  • 5 weeks later...

Okay let me see if I've got this right, so that I know this for future project.

 

My Boot.js should hopefully look something like this :

 

In regards to global variables deathCounter & levelTime

var BasicGame ={  current_level_id: 1,  deathCounter: 0,    levelTime: 0; };BasicGame.Boot = function (game) {};BasicGame.Boot.prototype = {    init: function () {        //  Unless you specifically know your game needs to support multi-touch I would recommend setting this to 1        this.input.maxPointers = 1;        //  Phaser will automatically pause if the browser tab the game is in loses focus. You can disable that here:        this.stage.disableVisibilityChange = true;        //  This tells the game to resize the renderer to match the game dimensions (i.e. 100% browser width / height)        this.scale.scaleMode = Phaser.ScaleManager.RESIZE;        this.scale.pageAlignHorizontally = true;        this.scale.pageAlignVertically = true;        this.scale.refresh();        // Add reset of the game states        this.state.add('Preloader', BasicGame.Preloader);        this.state.add('MainMenu', BasicGame.MainMenu);        this.state.add('Game', BasicGame.Game);      //  this.state.add('stage2', BasicGame.stage2);    },    preload: function () {        //  Here we load the assets required for our preloader (in this case a background and a loading bar)        this.load.image('preloaderBackground', 'assets/preloader/background.png');        this.load.image('preloaderBar', 'assets/preloader/bar.png');    },    create: function () {        console.log("Creating boot state!");        //  By this point the preloader assets have loaded to the cache, we've set the game settings        //  So now let's start the real preloader going        this.state.start('Preloader');    }};

Now currently for my code in game for the death counter and level time it goes like this:

 

 

This is how I've currently got the code set up

BasicGame.Game = function (game){  this.levelStartTime = 0;  this.levelCurrentTime = 0;   this.deathCounter;};BasicGame.Game.prototype ={  create: function ()  {    map.setTileIndexCallback([12,13,14,15], this.PlayerDie, this);    map.setTileIndexCallback([21, 25,26], this.nextStage, this);    this.levelStartTime = this.game.time.time;    timeText = this.game.add.text(1050, 770, 'Time: ' + 's',    {      font: '20px Arial',      fill: '#fff',      align: 'center'    });  },  update: function ()  {    this.levelCurrentTime = this.game.time.time - this.levelStartTime;    timeText.setText('Time: ' + this.levelCurrentTime/1000 + 's');  }  nextStage: function(sprite, tile)  {    console.log("Next Level!");    BasicGame.current_level_id++;        if (BasicGame.current_level_id > 4)    {      BasicGame.current_level_id = 1;      this.game.world.setBounds(0, 0 , 0 ,0);      this.game.state.start("Game", true, false);    }    else if (BasicGame.current_level_id <= 4)    {      //  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)  {    BasicGame.deathCounter++;    deathText.setText('Total Deaths: ' + BasicGame.deathCounter);    sprite.kill();    player.reset(135, 110)    map.replace(41, 19);    map.replace(31, 29);  }, 

Where do I go from here? As I'm still fairly new to phaser.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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