javascript - 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社区!
解决方案
没关系,我想通了。对于有同样问题但不能解决的任何人,我编写的回收平台功能使用相机滚动将平台向上发送,并且 Y 坐标为负值。为了在平台内部(即 sprite.y)和平台上方生成胡萝卜,减去 sprite 显示高度,而不是将两者相加,因为 sprite.y 是负值,减去它就是向上发送胡萝卜的数量是显示高度的像素数量。希望我有所帮助。
推荐阅读
- r - 如何从 R 中具有傅立叶季节性的 TSLM 模型返回预测?
- github - 更改 Github 存储库的后果
- ruby-on-rails - 由于抽象类,规范文件中的统一常量错误?
- c# - Moq It.IsSubtype 没有隐式引用转换
- swiftui - Xcode 13.1 中的 Navigationlink 问题——空白而不是链接?
- oracle-sql-data-modeler - 如何在 SQL Developer Data Modeler 18.2 中增加 UI 字体大小?
- wordpress - 在 Chrome、Edge 和 Firefox 中视频有声音但没有图像
- r - 创建从 R 中带有参数的函数创建的 dataframes/data.tables 列表?
- reactjs - 无法将 React Native 应用程序连接到 Firestore 模拟器
- python - 从字符串重新创建 XML