CaptKiwi Posted January 5, 2017 Share Posted January 5, 2017 Hello everyone, I would like to be able to center a text phrase in the x-axis of the screen within the game body. Well, after a lot of fiddling around and having no success using the "center text" example : https://phaser.io/examples/v2/text/center-text#download // Setting the style var style = { font: "bold 32px Arial", fill: "#fff", boundsAlignH: "center", boundsAlignV: "middle" }; // Adding the text text = game.add.text(0, 0, "this is the text", style); // Setting the bounds from x0, y100 and be 800px wide by 100px high text.setTextBounds(0, 100, 800, 100); I found that the issue was that I was using Phaser 2.6.2 and it only worked when I used 2.4.x I cannot find a working example for centering text in 2.6.x Any help would be really appreciated! Link to comment Share on other sites More sharing options...
PhasedEvolution Posted January 5, 2017 Share Posted January 5, 2017 I think you can do it this way: text.position.x = game.width / 2; or similar Link to comment Share on other sites More sharing options...
Xesenix Posted January 6, 2017 Share Posted January 6, 2017 I am centering my text like this: var label = this.game.add.text( positionOfTextCenterX, positionOfTextTopY, 'Some text to display\nMay be multiline', { font: '64px ' + fontNameUsedInGame, fill: '#ffffff', align: 'center' } ); label.fixedToCamera = true; // so it will move with camera label.anchor.setTo(0.5, 0); //titleLabel.anchor.setTo(x, y); // x: // 0.0 = left // 0.5 = center // 1.0 = right // y: // 0.0 = top // 0.5 = center // 1.0 = bottom Link to comment Share on other sites More sharing options...
Kitanga Nday (NDAY Games) Posted January 6, 2017 Share Posted January 6, 2017 I think using world.centerX would also work in conjuction with text.anchor.setTo(0.5). For example, this will center the text to the center of the game's screen. If you want to function create() { var text = game.add.text(game.world.centerX, game.world.centerY, 'I am centered', { font: "65px Arial", fill: "#ff0044" }); text.anchor.setTo(0.5); } Link to comment Share on other sites More sharing options...
CaptKiwi Posted January 6, 2017 Author Share Posted January 6, 2017 Thanks guys, appreciate the help! I was able to get the desired centering by using the code from Nday. Kitanga Nday (NDAY Games) 1 Link to comment Share on other sites More sharing options...
Kitanga Nday (NDAY Games) Posted January 6, 2017 Share Posted January 6, 2017 Just now, CaptKiwi said: Thanks guys, appreciate the help! I was able to get the desired centering by using the code from Nday. Glad to be of some assistance Link to comment Share on other sites More sharing options...
samme Posted January 8, 2017 Share Posted January 8, 2017 On 1/5/2017 at 1:28 PM, CaptKiwi said: I cannot find a working example for centering text in 2.6.x https://phaser.io/examples/v2/text/center-text still works in 2.6.2. If you use `setTextBounds()` you must make sure that rectangle is centered within the game canvas. Link to comment Share on other sites More sharing options...
Recommended Posts