Dav*_*bji 1 java android touch libgdx
我正在使用LibGDX进行游戏,我正在试图弄清楚如何限制TouchEvent for Jumping.
我的尝试:
if(player.b2body.getLinearVelocity().y > 3.5f){
float currentVelocityY = player.b2body.getLinearVelocity().y;
player.b2body.applyLinearImpulse(new Vector2(0, (- currentVelocityY)), player.b2body.getWorldCenter(), true);
}
Run Code Online (Sandbox Code Playgroud)
如果超过某个值,我正在考虑降低Y轴上的速度.但是这不起作用,好像我一直触摸屏幕,角色会飞得更高.
我想限制touchEvent在短时间内跳转两次.
你的任何想法?
谢谢.
解决方案1(限制每秒的跳跃次数):
所以,你的角色会跳上touchDown事件.您可以定义一个变量来存储最后一次点击(以毫秒为单位):
long lastTap = System.currentTimeMillis();
Run Code Online (Sandbox Code Playgroud)
并在点击事件:
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
if(System.currentTimeMillis() - lastTap < 1000)
return true;
// your character jump code
lastTap = System.currentTimeMillis();
return true;
Run Code Online (Sandbox Code Playgroud)
这应该每秒只调用一次你的跳转代码(因为if中的1000ms),无论你多快点击.只需测试数字(500我们每秒2次点击,250 - 4等等).
解决方案2(限制跳数):
您可以定义一个变量来存储执行跳转的次数和最大跳转次数:
int jumps = 0;
int maxJumps = 3;
Run Code Online (Sandbox Code Playgroud)
并在点击事件:
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
// the limit is reached
if(jumps == maxJumps)
return true;
// your character jump code
jumps++;
return true;
Run Code Online (Sandbox Code Playgroud)
当你的身体落下时,你重置你的render()方法或你角色的box2d交互监听器中的jumps var:
if(player.b2body.getLinearVelocity().y == 0)
jumps = 0;
Run Code Online (Sandbox Code Playgroud)
现在用户将能够进行3次快速跳跃,然后他将不得不等待角色掉在地上再次跳跃.
解决方案3(检查敲击力)
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
if(player.b2body.getLinearVelocity().y > 3.5f)
return true;
// your character jump code
return true;
}
Run Code Online (Sandbox Code Playgroud)
现在,只要y速度低于3.5f,用户就能跳转.
| 归档时间: |
|
| 查看次数: |
624 次 |
| 最近记录: |