简体   繁体   English

`Phaser.Display.Align.In.Center` 适用于我的文本的第一行,但不能使我的第二行居中。 我如何解决它?

[英]`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?

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/phaser@3.55.2/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. Phaser.Display.Align.In.Center适用于我的文本的第一行,但不能使我的第二行居中。 How do I fix it?我如何解决它?

在此处输入图像描述

Phaser.Display.Align.In.Center just aligns single GameObjects. Phaser.Display.Align.In.Center只对齐单个游戏对象。 Both lines of text are in the same GameObject noti_txt .两行文本都在同一个noti_txt中。

If you want to align both textlines, you could use the align property of the text GameObject, on creating it.如果你想对齐两个文本行,你可以在创建它时使用文本 GameObject 的align属性。 { align: 'center' }
(Or after creation with the property setStyle here a line to the documentation ) (或者在使用属性setStyle在这里创建一行文档之后)

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/phaser@3.55.2/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.我只推荐它,如果您正在重用文本块(或戏剧效果) ,您可以将文本拆分为 GameObjects。

But for that to work you would also have use the function Phaser.Display.Align.To.BottomCenter :但要使其正常工作,您还可以使用 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/phaser@3.55.2/dist/phaser.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 `graphics` object 本身工作得很好,但不能与 `Phaser.Display.Align.In.Center` 一起工作,我错过了什么? - A `graphics` object works well by itself but doesn't work with `Phaser.Display.Align.In.Center`, what am I missing? 考虑到我们已经有了 `setOrigin`,“Phaser.Display.Align.In.Center”是用来做什么的? - Considering we already have `setOrigin`, what is "Phaser.Display.Align.In.Center" used for? 如何在javascript中的字符串的第二行添加文本? - How do I add text on the 2nd line of a string in javascript? 我的价值不会使文本居中对齐 - My Value won't text align center 为什么我不能居中对齐 <td> 文本? - Why can't I center align my <td> text? 无法找到如何使广告居中。 我试过align =“ center”和margin:auto但没有用。 - Can't find how to center my ads. I have tried align=“center” and margin:auto but nothing works. 如何在同一行上制作文字和箭头。 文本中心和最左侧/右侧的箭头 - how to make text and arrow on same line. Text center and arrow on far left/right side 如何修正我的文字使其显示在一行上? - How do I fix my text to make it appear on one line? 如何在中心对齐图标和文本? - How do I align icons and text in center? 如何使用空格和换行符将文本居中对齐 - how to text align center for the text with space and line break
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM