英文:
Phaser Hit boxes are invisibly to the right, what gives?
问题
我正在制作一个游戏,玩家可以与周围的事物互动,如NPC、物品和环境中的可检查物品。他们可以以两种方式与事物互动,按T键进行检查或与之交谈,或按R键使用或拾取它们。
出于某种原因,用于判断玩家角色何时与某物接触,因此是否能够与之互动的碰撞框似乎有问题。更具体地说,它们似乎隐形地向右移动,因此要求玩家角色位于碰撞框的右侧才能与之互动。
这是一张玩家角色试图检查一扇门的图片。由于他们位于碰撞框的左侧,当玩家按下T键时,什么都不会发生。
在这张图片中,玩家角色位于碰撞框的右侧,因此按下T键时显示的对话框会出现。
最后,这是玩家角色,位于门的碰撞框外,仍然可以通过按T键触发对话。这清楚地证明了碰撞框被隐形移动到右侧。
这是创建门的代码。
this.doorSide = this.physics.add.sprite(548, 735, 'sideDoor');
this.doorSide.body.immovable = true;
this.doorSide.body.allowGravity = false;
这是当玩家在门的碰撞框内按下T键时触发的代码。对于游戏中的所有其他可互动对象,都是相同的。
if ((this.checkCollision(this.p1, this.doorSide)) && Phaser.Input.Keyboard.JustDown(this.keyT)) {
this.talking = !this.talking;
}
我真的不知道出了什么问题,或者为什么会出现这种情况。是否有人知道为什么会发生这种情况,以及是否有解决方法?
如果有帮助的话,我正在使用Phaser 3在VSCode中使用arcade物理引擎。
英文:
I'm working on a game where the player can interact with stuff around them, like npcs, items, and examinable things in the environment. They can interact with stuff in 2 ways, pressing T to examine or talk to it or R to use or pick it up.
For some reason, the hitboxes that tell when the player character is in contact with something and thus should or should not be able to interact with it are off. More specifically, they seem to invisibly be moved to the right, thus demanding the player character to be on the right side of the hitboxes in order to interact with them.
Here is a picture of the player character trying to examine a door. As they are on the left side of the hitbox, when the player presses T, nothing happens.
In this picture, the player character is on the right side of the hitbox, and thus the dialogue that is displayed when pressing T is shown.
Finally, here is the player character, outside of the door's hitbox and still able to trigger the dialogue by pressing T. Clear proof that the hitboxes are invisibly moved to the right.
Here is the code that creates the door.
this.doorSide = this.physics.add.sprite(548, 735, 'sideDoor');
this.doorSide.body.immovable = true;
this.doorSide.body.allowGravity = false;
And here is the code that triggers when the player presses T in the door's hitbox. It's same for all other interactables in the game.
if ((this.checkCollision(this.p1, this.doorSide)) && Phaser.Input.Keyboard.JustDown(this.keyT)) {
this.talking = !this.talking;
}
I really can't tell what went wrong or why this happens. Does anyone know how this happened and if there is a solution?
If it helps, I'm using Phaser 3 is VSCode employing arcade physics.
答案1
得分: 0
I assume the problem is in the function call this.checkCollision(...)
, but since you didn't share that code I just can assume.
Nevertheless, if that function is the problem, the solution is, to use the builtin function overlap
of phaser to check for collision/overlaps. (link to the documentation)
if ((this.physics.overlap(this.p1, this.doorSide)) && Phaser.Input.Keyboard.JustDown(this.keyT)) {
this.talking = !this.talking;
}
Tipp: it is always better to use builtin functions, when possible, instead of coding everything yourself.
英文:
I assume the problem is in the function call this.checkCollision(...)
, but since you didn't share that code I just can assume.
Nevertheless, if that function is the problem, the solution is, touse the builtin function overlap
of phaser to check for collision/overlaps. (link to the documenation)
<!-- language: lang-js -->
if ((this.physics.overlap(this.p1, this.doorSide)) && Phaser.Input.Keyboard.JustDown(this.keyT)) {
this.talking = !this.talking;
}
> Tipp: it is always better to use builtin functions, when possible, instead of coding everything yourself.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论