Phaser3 如何调整图像大小

huangapple go评论52阅读模式
英文:

How the Phaser3 resizes an image

问题

我无法理解预期结果与实际结果之间的差异是什么。

在第一个示例中,我将gui-window的宽度设置为屏幕的宽度,高度设置为屏幕高度的0.3,将其放在屏幕上时,发现部分图像无法适应屏幕。

Phaser3 如何调整图像大小
结果截图

您可以确保在以下截图中裁剪了gui-window,在将图像添加到场景时,我添加了100像素的填充。

Phaser3 如何调整图像大小
结果截图

有人能解释为什么Phaser的行为是这样的,以及如何修复吗?

英文:

I can not understand what is the discrepancy between the expected result and what is actually.
In the first example, I set the width of the gui-window to the width of the screen, and the height to 0.3 of the height of the screen, placing this on the screen I found that part of the image didn't fit on the screen.

Phaser3 如何调整图像大小
result screenshot

You can make sure that the gui-window is cropped from the following screenshots, where I added a padding of 100 pixels when adding an image to the scene

Phaser3 如何调整图像大小
result screenshot

Can anyone explain why the Phaser behaves this way and how to fix it?

答案1

得分: 0

The function setDisplaySize just changes the images and distorts it that the image is cut off has probably to do with the passed width and height. The game - scene might be smaller than the window size. Instead of innerHeight / innerWidth you could use this.scene.game.config.height / this.scene.game.config.width, these two properties should return the correct size of the current html-canvas-object.

btw.: if you want to keep the border without distortion, I recommend using nine-slice. Check out this feature (available starting with the version 3.60 and above) link to example

btw.: when posting questions (or answers), it is always better to post the code, and not a screenshot of the code.

英文:

I think the main problem is tht you are not using the function as you should.

The function setDisplaySize just changes the images and distorts it (link to the documentation). that the image is cut off has probably to do with the the passed width and height. The game - scene might be smaller that the window size.
Instead of innerHeight / innerWidth you could use this.scene.game.config.height / this.scene.game.config.width, this two properties should return the correct sze of the current html-canvas-object.

> btw.: if you want to keep the border without distortion, i recommend using nine-slice check out this feature (available starting with the version 3.60 and above) https://labs.phaser.io/view.html?src=src/game%20objects/nine%20slice/create%20nine%20slice.js

> btw.: when posting question (or answers), it is always better to post the code, and not a screenshot of the code.

huangapple
  • 本文由 发表于 2023年5月17日 23:42:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273897.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定