我正在使用 JavaScript 并使用 Phaser 库制作一个简单的游戏。我对此很陌生,所以希望这不是一个愚蠢的问题。
我已经让一切工作完美,但我很想知道如何让岩石从墙壁上弹起,而不是穿过它们并出现在另一边。
与这个函数有关:
有人告诉我
"If it hits Width: 940 then x = 940 and you start going back 939,
i--
, etc. Height will continue as normal. Rather than resetting i.eshot.reset(x, y);
.If you hit the bottom or top then do the same to height, keeping width the same."
但是,我不确定如何将其实现到代码中。我已经尝试过,但失败了:)这非常令人沮丧,所以对此事的任何帮助都将是令人惊奇的。
谢谢。
最佳答案
通常,我创建一个速度向量,它代表对象的“速度”。 在每一帧上,我将该速度向量添加到位置向量中。当我希望我的物体向相反方向移动时,我将向量乘以-1。
创建一个像这样的向量,当你的对象碰撞边缘时,将其乘以 -1。
你可以用这种类型的矢量做很多事情,比如平滑的速度降低、类似空间的运动等等......
例如:
//on init
var velocity = {x: 10; y: 10};
var pos = {x: 10; y:10};
//on frame update
pos.x += velocity.x;
pos.y += velocity.y
//on edge collision
velocity.x = velocity.x * -1;
velocity.y = velocity.y * -1;
关于javascript - 如何使物体边缘反弹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37002643/