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

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

  • Recently Browsing   0 members

    No registered users viewing this page.