Jump to content

can't figure out how to make this work.


Le Twitch
 Share

Recommended Posts

Main.Game = function (game) {};var colourA;var colourB;    var textWord;var textColour;    var square;var squareA;var squareB;var gameText;Main.Game.prototype {        create: function() {        square = this.add.group();                squareA = this.add.sprite(55, 370, 'colourSquare', colourA);        squareB = this.add.sprite(675, 370, 'colourSquare', colourB);        square.add(squareA);        square.add(squareB);                square.setAll('inputEnabled', true);                gameText = this.add.bitmapText(null, null, textColour + 'Text', textWord, 127);                gameText.x = gameWidth - gameText.textWidth / 2;        gameText.y = gameHeight - gameText.textHeight / 2;                this.load.onLoadComplete.add(this.randomColour, this);            },            update: function() {                            },            randomColour: function() {                var colourArray = ['red', 'orange', 'yellow', 'green', 'blue', 'purple'];        var randomA = Math.floor(Math.random() * colourArray.length);        var randomB = Math.floor(Math.random() * colourArray.length);            while (randomA === randomB) {                    randomB = Math.floor(Math.random() * colourArray.length);                }        colourA = colourArray[randomA];    colourB = colourArray[randomB];        var randomText = Math.floor(Math.random() * 2);        if (randomText === 0) {                        textWord = colourA;            textColour = colourB;                    } else if (randomText === 1) {                        textWord = colourB;            textColour = colourA;                    }    }};

in the function 'randomColour', a colour is randomly chosen for 'colourA' and 'colourB'. the two colours that were randomed for 'colourA' and 'colourB' are then randomized again to determine which one defines the bitmap font's text and which one defines which asset to use for the font.

 

the reason the code doesn't work is because 'textColour' isn't defined. I tried to make sure it was defined in the beginning by running 'randomColour' onLoadComplete but that didn't exactly work. how can i fix this?

 

edit: just to clarify, i want it to be randomized in the beginning

Link to comment
Share on other sites

 

Surely you can just call randomColour at the start of create to populate the required variables?

create: function() {  this.randomColour();  // ... the rest of the code here}       

 

wow. i was so caught up with trying to learn phaser specific coding that i completely forgot about that

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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