Phaser 3:改变精灵相对于高度的宽度

发布于 2025-01-11 07:29:53 字数 139 浏览 0 评论 0原文

我希望能够更改精灵的高度以适应菜单,但是当我使用 sprite.displayHeight = menu.height 更改高度时,它的宽度不会随之缩放它会被拉伸/压扁。

有没有办法相对于高度缩放宽度,以保持精灵的原始比例?

I'd like to be able to change the height of my sprite to fit into a menu, however when I change the height with sprite.displayHeight = menu.height, it's width won't scale along and it will be stretched/squashed.

Is there a way to scale the width relative to the height, to keep the sprite's original ratio?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

治碍 2025-01-18 07:29:53

我不知道任何特殊函数可以开箱即用,但是您只需额外一行代码即可实现此目的. 无需额外计算。
(这也适用于 displayHeight

  1. 首先设置图像的宽度img.displayWidth = 100;
  2. 然后设置Y轴的比例(因为X轴是通过设置displayWidth来设置的):img.scaleY = img.scaleX;

这里文档,指出了这一点输出:

“...此游戏对象的显示宽度。
该值考虑了比例因子。
设置此值将调整游戏对象的缩放属性...”

这里是一个工作演示:

// Minor formating for stackoverflow
document.body.style = "display: flex;flex-direction: column;";    

function preload ()
{
    this.load.image('img', 'https://labs.phaser.io/assets/pics/contra3.png');
}

function create ()
{
    let imgOrig = this.add.image(100,100, 'img');
    let img = this.add.image(320, 100, 'img');
    img.displayWidth = 100;

    // extra line to scale the image proportional
    img.scaleY = img.scaleX;
}

var config = {
    type: Phaser.AUTO,
    width: 536,
    height: 160,
    scene: {
        preload: preload,
        create: create
    }
};

var game = new Phaser.Game(config);
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.js"></script>

I don't know any special function, that does this out-of-the-box, BUT you can achieve this, with only one extra line of code. No extra calculation needed.
(this works also with the displayHeight)

  1. First set the width of the image: img.displayWidth = 100;
  2. Then set the scale of the Y axis (since the X-axis is set through setting the displayWidth): img.scaleY = img.scaleX;

Here the documentation, that points this out:

"...The displayed width of this Game Object.
This value takes into account the scale factor.
Setting this value will adjust the Game Object's scale property..."

Here a working demo:

// Minor formating for stackoverflow
document.body.style = "display: flex;flex-direction: column;";    

function preload ()
{
    this.load.image('img', 'https://labs.phaser.io/assets/pics/contra3.png');
}

function create ()
{
    let imgOrig = this.add.image(100,100, 'img');
    let img = this.add.image(320, 100, 'img');
    img.displayWidth = 100;

    // extra line to scale the image proportional
    img.scaleY = img.scaleX;
}

var config = {
    type: Phaser.AUTO,
    width: 536,
    height: 160,
    scene: {
        preload: preload,
        create: create
    }
};

var game = new Phaser.Game(config);
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.js"></script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文