我可能是愚蠢的东西。但是,我对此很难过。我经常找到代码的例子,但它让我感到困惑。不幸的是,没有任何好的教程。我一直在使用Lua近一年,所以我有经验。帮助将非常感激!
基本上我想学习如何让矩形跳起然后再往下走。
答案 0 :(得分:2)
对于您控制的单个块,您基本上需要存储它的重力并为它找出一个很好的加速度。
currentGravity = 0;
gravity = 1;
然后在循环中,你必须使用一些碰撞检测来检查它是否在地面上。您想将重力加速度添加到currentGravity:
currentGravity = currentGravity + gravity
然后,将其添加到块的当前y轴:
YAxis = YAxis + currentGravity
一旦降落,请确保将重力设置为0.还要确保将其设置为0以确保不会掉落地面(因为无论如何都会不断增加重力。)
if not inAir() then
currentGravity = 0
end
当然,要跳跃,将currentGravity设置为负数(如20)(如果这就是你如何使重力发挥作用。)
这是我为Love2D制作的碰撞检测功能:
function checkCollision(Pos1,Size1,Pos2,Size2)
local W1,H1,W2,H2 = Size1[1]/2,Size1[2]/2,Size2[1]/2,Size2[2]/2
local Center1,Center2 = {Pos1[1]+W1,Pos1[2]+H1},{Pos2[1]+W2,Pos2[2]+H2}
local c1 = Center1[1]+(W1) > Center2[1]-W2
local c2 = Center1[1]-(W1) < Center2[1]+W2
local c3 = Center1[2]+(H1) > Center2[2]-H2
local c4 = Center1[2]-(H1) < Center2[2]+H2
if (c1 and c2) and (c3 and c4) then
return true
end
return false
end
它假定你给它的位置是块的中心。如果盒子旋转它就不会起作用。你必须弄清楚如何使它与墙壁等一起工作。是的,它很难看,因为它已经很老了。 :P