首页 > 解决方案 > Phaser 3 每次如何计算平台顶部的 y 位置?

问题描述

我正在做一个 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社区!

标签: javascriptlogicphaser-frameworkcalculation

解决方案


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


推荐阅读