Jump to content

Problem with different text color in text group


bolatro
 Share

Recommended Posts

function generate_hex(){                                                           return '#'+((0.5 + 0.5*Math.random())*0xFFFFFF<<0).toString(16);           }   var style = { font: "24px Arial", align: "center" }; var texts = game.add.group();var text = 'hello';for(var i = 0; i < 5; i++){    (function(t){         // var t = game.add.text(10, i * 25, text, style);                     t.fill = generate_hex();        texts.add(t);    })(game.add.text(10, i * 25, text, style) )}    

Idea that I want five lines of text with various color. Problem that all 5 text lines has one color - last that generate_hex function generate. I know this kind of problems happens in loop - that's why I using scope. But it is not helping now.

Please help :)

Link to comment
Share on other sites

Wow that's an interesting (and overly complex!) way of approaching it. The following works fine for me:

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { create: create });var textGroup;function generateHexColor() { 	return '#' + ((0.5 + 0.5 * Math.random()) * 0xFFFFFF << 0).toString(16);}function create() {	textGroup = game.add.group();	var style = { font: "32px Arial", fill: "#ff0044", align: "center" };	for (var i = 0; i < 10; i++)	{		style.fill = generateHexColor();		textGroup.add(game.make.text(100, 64 + i * 32, 'here is a colored line of text',  style));	}}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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