alexche8

Problem with different text color in text group

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 :)

Share this post


Link to post
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));	}}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.