I just started with Phaser and so far I really enjoy it, but I could not find a tutorial/example on composing objects from multiple sprites.
Reading the documentation, I see that you can “group” sprites together, but I was curious if there is a better way of doing this. Let’s consider the following example:
I have a complex button that has a background image, some graphical elements that are animated and a text.
We load all the images in the preload()
this.load.image(‘button_bg’, ‘assets/button_bg.png’);
this.load.image(‘shape1’, ‘assets/shape1.png’);
this.load.image(‘shape2’, ‘assets/shape2.png’);
this.load.image(‘shape3’, ‘assets/shape3.png’);
Now, when I create a button, let’s assume I have this (simplified version) function:
function createButton(text) {
var bg = this.add.image(0, 0, ‘button_bg’);
var shape1 = this.add.image(10, 10, ‘shape1’);
var shape2 = this.add.image(30, 30, ‘shape2’);
// add more shapes and the text
this.button1 = this.group.add();
this.button1.add(bg);
this.button2.add(spahe1);
…
}
Now, I was wondering if there is a better way of doing this. Some object type that I can have my button extend and I can just use add.image() to that one directly and the x and y of the underlying images are relative to the main object’s x and y.
For example, in an old tech (Flash - ActionScrip3), we could have a Sprite object that could contain more sprites. So when I moved the button, all underlying elements would move as well without the need to iterate over each of them and move them accordingly.
This would help a lot for Tweens, where I can add a Tween to the ‘button’ object, and the bg, text and shapes withing would Tween accordingly.
Sorry if this is a basic question, I haven’t worked with game development for many years (hence the AS3 reference) and just re-started this.