Mizukage Posted October 27, 2015 Share Posted October 27, 2015 I need to storage player stats in a global var / obj because after chenge the game state normal var will be flushed.Im not sure where to make global var Boot.jsvar boot = function(game){}; boot.prototype = { preload: function(){ }, create: function(){ this.game.state.start("Preload"); }}Preload.jsvar preload = function (game) {}preload.prototype = { preload: function () { I cute thise code..... }, create: function () { this.game.state.start("TheGame"); }}TheGame.jsvar theGame = function (game) { this.player = { name: 'Kirito', lvl: 1, lvl2: false, lvl3: false, lvl4: false, maxHp: 100, hp: 100, str: 10, agi: 10, exp: 10, card: 1, weapon: this.oldSword, skills: this.resLife, gold: 0 } this.playerDmg = (this.player.str / 10) * (this.player.weapon.attack + this.player.lvl); this.monsterLvl1 = { name: 'Week Recrut', hp: 50, attack: 5, exp: 50, gold: 5 }}theGame.prototype = { create: function () { //Sterowanie w grze cursors = this.game.input.keyboard.createCursorKeys(); upKey = this.game.input.keyboard.addKey(Phaser.Keyboard.W); downKey = this.game.input.keyboard.addKey(Phaser.Keyboard.S); leftKey = this.game.input.keyboard.addKey(Phaser.Keyboard.A); rightKey = this.game.input.keyboard.addKey(Phaser.Keyboard.D); characterMenuKey = this.game.input.keyboard.addKey(Phaser.Keyboard.K); }, lvlUpCheck: function (player) { if (player.exp <= this.lvl1Exp) { } else if (player.exp > this.lvl1Exp && player.exp <= 1500 && player.lvl2 == false) { //dzwiek lvl up i napis na srodku ekranu czasowy player.lvl = 2; this.lvl1Exp = 1500; this.lvlUp(player); } else if (player.exp > this.lvl1Exp && player.exp <= 2500 && player.lvl3 == false) { player.lvl = 3; this.lvlUp(player); } }, update: function () { playerSprite.body.velocity.y = 0; playerSprite.body.velocity.x = 0; if (playerCanMove) { if (upKey.isDown && leftKey.isUp && rightKey.isUp) { playerSprite.body.velocity.y = -this.speed; } else if (downKey.isDown && leftKey.isUp && rightKey.isUp) { playerSprite.body.velocity.y = this.speed; } if (leftKey.isDown) { playerSprite.body.velocity.x = -this.speed; } else if (rightKey.isDown) { playerSprite.body.velocity.x = this.speed; } }; }, render: function () { this.game.debug.text('Name: ' + this.player.name, 30, 30); this.game.debug.text('Hp: ' + Math.round(this.player.hp), 30, 70); }}I cut a big part of inside code, for make it easier to read, mean build Link to comment Share on other sites More sharing options...
Mizukage Posted October 27, 2015 Author Share Posted October 27, 2015 Ok I found a way Link to comment Share on other sites More sharing options...
chongdashu Posted October 27, 2015 Share Posted October 27, 2015 You can always just declare it anywhere outside the scope of the state object's definition or prototype definition. e.g.,var global_stats = {};var theGame = function (game) { this.player = { name: 'Kirito', // ...}theGame.protoype = { // ... update: function() { // ... global_stats["level"] = this.player.level; // ... }But that's pretty hacky. Several ways to make it cleaner would be to have a globals.js file that you store all global variables, or make the variable a static variable of your state object. Link to comment Share on other sites More sharing options...
chongdashu Posted October 27, 2015 Share Posted October 27, 2015 Ok I found a way Great! Would you mind sharing what you did? Always nice to avoid this. drhayes 1 Link to comment Share on other sites More sharing options...
Mizukage Posted October 27, 2015 Author Share Posted October 27, 2015 I cut variables and objects who need to be global, from theGame = function (game) { } and left them in outsideThan I just removed all of "This." from game code where I call those global variables. And then I dont need to declare those variables in another js state file. Link to comment Share on other sites More sharing options...
Recommended Posts