Jump to content

Failing miserably with alignTo()


QLNEO
 Share

Recommended Posts

Currently making a menu, I have a bunch of buttons, that are essentially groups containing the button sprite itself and text. Trying to vertically align each subsequent group with their previously added button sprite inside the group (since we can't directly alignTo() groups). The code is essentially how it reads below:

let rect, buttons;

            let InitState = {
                preload: function() {
                    let square = this.game.add.graphics(0,0);
                    square.beginFill(0xFFFFFF,1);
                    square.drawRect(0,0,200,100);
                    rect = square.generateTexture();
                    square.clear();
                },

                create: function() {
                    buttons = this.game.add.group();
                    buttons.position = {x: 50, y: 50}
                    let btnarray = ["ONE","TWO","THREE","FOUR","FIVE"];
                    btnarray.forEach(function(btnTxt) {
                        let buttonGroup = this.game.add.group(buttons);
                        let image = rect;

                        let buttonImg = buttonGroup.add(this.game.make.button(0,0,rect));
                        let txt = buttonGroup.add(this.game.make.text(buttonImg.width/2,buttonImg.height/2,btnTxt))
                        txt.anchor = {x: 0.5, y: 0.5};

                        if(buttons.children.length>1) { // We won't be needing to align the first one
                            buttonGroup.alignTo(
                                buttons.getAt(buttons.children.length-2).children[0], // buttonImg
                                Phaser.BOTTOM_LEFT, 0, 4
                            );
                        }
                    }.bind(this))
                },
}
let Game = new Phaser.Game( 800, 600, "", Phaser.AUTO, InitState );

 

This is a simplified version of what I'm doing, but even in this simplified version, the same thing occurs. The first and second buttons are correctly placed, but the third one is positioned in top of the second, and the fourth in top of the third. As you can see on the picture below, we can only see the first and last buttons, because the rest is buried under "five".

 

599af40cf24ae.png

alignTo() always refers to the second-to-last button in the group (the last being the one I'm trying to move), so why it seems to always take position (0,0) (relative to buttons) as reference? Thanks in advance.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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