`Phaser.Display.Align.In.Center` works well with the first line of my text but doesn't center my 2nd line. How do I fix it?

yaojp

I'm trying to make some kind of a notification/message box giving players hints to advance the gameplay, here is the code

var game = new Phaser.Game({
  width: 320, height: 200,
  scene: {
    create: create
  }
});
function create() {
  let noti_bg = this.add.rectangle(160, 100, 200, 120, 0x000000, .5);
  let noti_txt = this.add.text(0, 0, 'Magic is not ready yet\n\nwait a sec');
  Phaser.Display.Align.In.Center(noti_txt, noti_bg);
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.js"></script>

Phaser.Display.Align.In.Center works well with the first line of my text but doesn't center my 2nd line. How do I fix it?

enter image description here

winner_joiner

Phaser.Display.Align.In.Center just aligns single GameObjects. Both lines of text are in the same GameObject noti_txt.

If you want to align both textlines, you could use the align property of the text GameObject, on creating it. { align: 'center' }
(Or after creation with the property setStyle here a line to the documentation)

Here the adapted Code:

var game = new Phaser.Game({
  width: 320, height: 200,
  scene: {
    create: create
  }
});
function create() {
  let noti_bg = this.add.rectangle(160, 100, 200, 120, 0x000000, .5);
  let noti_txt = this.add.text(0, 0, 'Magic is not ready yet\n\nwait a sec', { align: 'center' });
  Phaser.Display.Align.In.Center(noti_txt, noti_bg);
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.js"></script>

Alternatively / Extra:
I would only recommended it, if you are reusing text blocks (or dramatic effect), you could split the text, into to GameObjects.

But for that to work you would also have use the function Phaser.Display.Align.To.BottomCenter:

var game = new Phaser.Game({
  width: 320, height: 200,
  scene: {
    create: create
  }
});
function create() {
  let noti_bg = this.add.rectangle(160, 100, 200, 120, 0x000000, .5);
  let noti_txt1 = this.add.text(0, 0, 'Magic is not ready yet');
  let noti_txt2 = this.add.text(0, 0, 'wait a sec');

  // extra visual effect
  this.tweens.add({
    targets: noti_txt2 ,
    alpha: 0,
    ease: 'Power1',
    duration: 1000,
    yoyo: true,
    repeat: -1,
  });
  Phaser.Display.Align.In.Center(noti_txt1, noti_bg);
  // Just adding a minor horizontal offset
  Phaser.Display.Align.To.BottomCenter(noti_txt2, noti_txt1, 0, 10);
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.js"></script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

A `graphics` object works well by itself but doesn't work with `Phaser.Display.Align.In.Center`, what am I missing?

How to left align the 2nd line of a text if initial text-align is center

How do I align my border animation in the center with the text

How do I get my divs to align at the center if margin:auto doesn't work?

How do I center my nav bar on its own line?

Align icon vertically to the center of the first line of text

Why doesn't text-align: center work in my table?

how do i align my th text in a same line

I'm trying to put a line of text in the center, but it doesn't work

Center align line below text

How could I align my output to the center?

How i center align my icon to the buttom?

How can I center the logo in my navbar and keep it in line?

How can I center my buttons, <center> </center> doesn't work

How do I display the circles in the center in my program

how to text align center for the text with space and line break

How can I do to center my text using matplotlib?

How do I Vertically Center Text in My Navigation Bar?

How to align my JTextField to center

How can I align text with my logo in the center of my nav bar?

How can I align my text/border to the center of my page with the <div> element?

The box of GUILayout.TextField disappears when I try to center my text, why does it do that and how do I fix it?

How can I align the text that didn't fit the first line to right in CSS, like they do in poetry?

How do I horizontally center 2 or more divs on the same line

I can't align the div to the center of my webpage

How do I center my JFrame?

How do I center my Navbar?

How do I center all my elements?

How do I center my heading?