【问题标题】:How does Phaser 3 calculate the y position on top of the platforms every time?Phaser 3 每次如何计算平台顶部的 y 位置?
【发布时间】:2020-09-23 22:08:58
【问题描述】:

我正在做一个 Phaser 3 教程(用于制作 2d 和 3d 游戏的 JavaScript 框架),我很想知道 Phaser 3 如何通过这个简单的计算处理 y 位置:

    /**
     * @param {Phaser.GameObjects.Sprite} Sprite
     */
    addCarrotAbove(sprite)
     {
        // this one!!!!!
        const y = sprite.y - sprite.displayHeight

        /** @type {Phaser.Physics.Arcade.Sprite} */
        const carrot = this.carrots.get(sprite.x, y, 'carrot')

        this.add.existing(carrot)

        carrot.body.setSize(carrot.width, carrot.height)

        return carrot
    }

当解析一个不同的对象给这个函数时,它而不是精灵是我写/迭代的一个自动平台回收的东西:

```update(t, dt)
    {
        this.platforms.children.iterate(child => {
            /** @type {Phaser.Physics.Arcade.Sprite} */
            const platform = child

            const scrollY = this.cameras.main.scrollY
            if (platform.y >= scrollY + 600)
            {
                platform.y = scrollY - 60
                platform.body.updateFromGameObject()

                // create a carrot above the platform being reused
                this.addCarrotAbove(platform)
            }
        })```

它会自动将胡萝卜放在平台顶部(在执行函数时)

它是如何做到的?我唯一感兴趣的是我用 const 创建的 y 变量。胡萝卜的位置是用那个简单的计算方法计算出来的,但我不明白它是如何每次把胡萝卜放在平台上的。感谢 stackoverflow 社区!

【问题讨论】:

    标签: javascript logic phaser-framework calculation


    【解决方案1】:

    没关系,我想通了。对于有同样问题但不能解决的任何人,我编写的回收平台功能使用相机滚动将平台向上发送,并且 Y 坐标为负值。为了在平台内部(即 sprite.y)和平台上方生成胡萝卜,减去 sprite 显示高度,而不是将两者相加,因为 sprite.y 是负值,减去它就是向上发送胡萝卜的数量是显示高度的像素数量。希望我能帮上忙。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-10
      • 2017-01-22
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 2011-01-18
      • 1970-01-01
      • 2019-04-19
      相关资源
      最近更新 更多