Phaser 3每次如何计算平台顶部的y位置



我正在做Phaser3教程(制作2d和3d游戏的javascript框架(,我很想知道Phaser3是如何通过这个简单的计算来处理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
}

当解析这个函数的不同对象时,它不是sprite,而是我写的/迭代的自动平台回收物:

```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社区!

无论如何,我都想明白了。对于任何有同样问题但不能的人,我编写的回收平台功能使用相机滚动来向上发送平台,这会给Y坐标一个负值。为了在平台内(即sprite.y(和平台上生成胡萝卜,您可以减去sprite显示高度,而不将两者相加,因为sprite.y是一个负值,通过减去它,您将胡萝卜向上发送显示高度像素量。希望我能帮上忙。

最新更新