【发布时间】:2021-08-29 02:44:48
【问题描述】:
我正在制作一个游戏,其中有 2 个精灵(一个花花公子和一个破坏球),并且在按键时它们都会移动。那部分工作正常,但我希望它们在它们中的任何一个与侧面的墙壁碰撞时都停止。 (也有重力,所以精灵不断地与地板碰撞。) 所以:
什么都没有:什么都没有发生
碰撞:停止!
b 碰撞:停止!
a 和 b 碰撞:停止!
我有以下代码sn-ps:
//in create()
this.wall = this.physics.add.staticGroup()
this.dude = this.physics.add.image(...getPos([7, 1]), 'dude')
this.dude.setCollideWorldBounds(true)
this.physics.add.collider(this.dude, this.wall)
this.ball = this.physics.add.image(...getPos([7, 22]), 'ball')
this.ball.setCollideWorldBounds(true)
this.physics.add.collider(this.ball, this.wall)
//in update()
if (cursors.left.isDown || this.a.isDown) {
this.dude.setVelocityX(-300)
this.ball.setVelocityX(-300)
//code to manage it should go here...
} else if (cursors.right.isDown || this.d.isDown) {
this.dude.setVelocityX(300)
this.ball.setVelocityX(300)
//and here...
} else {
this.dude.setVelocityX(0)
this.ball.setVelocityX(0)
}
if (
(cursors.up.isDown || this.w.isDown || this.space.isDown) &&
this.dude.body.blocked.down
) {
//the ball is not supposed to jump when the player jumps, and should fall according to gravity, but otherwise, it should imitate the player
this.dude.setVelocityY(-1000)
setTimeout(() => {
this.dude.setVelocityY(0)
}, 250)
}
这里有一些我试过但没用的方法
• 为碰撞器添加回调函数 - 碰撞器函数无法区分地板和墙壁,我无法将地板设置为另一个单独的类,因为如果这家伙跳到墙上,它就会变成地板:-
.body.blocked和.touching- 就像其他人说的,当感知左右时,属性总是返回false出于某种原因(我都试过了) -
new phaser.point- 我在堆栈上发现的东西,用于检测标记为 √ 的更改,但它不起作用(语法为Phaser.Point.equals(dude.body.velocity, new Phaser.Point(0,0)))
【问题讨论】:
-
回调函数对我来说似乎是正确的方法,但你已经尝试过......你的意思是:“我不能将楼层设置为另一个单独的类,因为如果这家伙跳到墙上,它会变成地板”?
-
@DavidePedron 地板上有柱子,伙计们可以跳上去。然后,原本是墙的东西变成了“地板”。
标签: javascript dom-events collision-detection phaser-framework