Can text be added to a group? Or only sprites?

Recommended Posts



Is it possible to add text to a group? Or is it limited to only sprites?


I tried code like this to no avail:

foo = game.add.group();foo.create(0,0,'bar'); // adding a spritefoo.add.text(0, 10, 'some text', { 'font': '22px Helvetica', fill: '#fff' }); // doesn't work

If text can't be added to a group is there some workaround for creating text that can be manipulated along with sprites as part of the same unit?


Thank you

Share this post

Link to post
Share on other sites

Create the text separately, and add it afterwards.

var group = game.add.group();group.create(0,0,"sprite"); //will only ever create and add sprites.var text = new Phaser.Text(game, 0, 0, "Text", {/*style object*/});group.add(text);//Ortext = game.add.text(0, 0, "Text", {/*style object*/});group.add(text);

This works for tile sprites as well, as you can't create them directly on the group.



Edit: fixed the reference to the GameObjectFactory in the second option...

Share this post

Link to post
Share on other sites

In 1.1.4 I added the ability to do this:

text = game.add.text(0, 0, "Text", {/*style*/}, otherGroup);

Where 'otherGroup' is a new parameter allowing you to specify which Group it gets added to. This parameter has been added to most of the methods in GameObjectFactory.

That's handy, it'll cut my creation code down by about a quarter!

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.