【发布时间】: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