Jump to content

Where to put global var in this case?


Mizukage
 Share

Recommended Posts

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.js

var boot = function(game){};  boot.prototype = {	preload: function(){	},  	create: function(){		this.game.state.start("Preload");	}}

Preload.js

var preload = function (game) {}preload.prototype = {    preload: function () {        I cute thise code.....    },    create: function () {        this.game.state.start("TheGame");    }}

TheGame.js

var 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

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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