Jump to content

Changing States resets image in array


ForT3X
 Share

Recommended Posts

Hello,

I'm currently working on a tiny project in Phaser/JS for my first time ever and I'm walking into a little problem. I'm not so sufficient in JS yet so I hope it's a dumb question that can be solved with a simple suggestion. Already googled like 3 hours but can't fix it.

Case:

I'm generating a global array in my bootstate and filling it with 2 strings and 1 sprite (This will be a list of players with an avatar and name) 
 

BasicGame = {
    players: new Array(8),
    //playerImage: new Array(8)
};

BasicGame.Boot = function (game) {
    console.log("%cStarting my awesome game", "color:white; background:red");
};

BasicGame.Boot.prototype = {
    preload: function () {
        this.game.load.image("mark", "assets/mark.png");
    },
    create: function () {

        players = new Array(8);
        playerStockImage = this.game.make.sprite(0,0,"mark")
        for (i = 0; i < players.length; i++)
        {
            players[i] = {
                name: "name" + i,
                name2: "name2" + i,
                sprite: playerStockImage
            };
            console.log(i);
        }

This all works perfectly fine when arriving at the mainmenustate create function the first time and I call 

        this.game.add.existing(players[0].sprite);

then I navigate to the gamestate and then back to my mainmenustate it tells me "Cannot read property 'length' of undefined" on the line above. I guess I'm missing something but i can't find what and I'm turning to become pretty clueless

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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