我有一个简单的游戏,玩家可以射击一些子弹。然而,我想让玩家每 x 秒只能发射一颗子弹。我该怎么做?
我尝试通过计算子弹之间的平均时间来做到这一点,并说如果平均时间小于一秒,则不要让空格键(射击控制)起作用。然而,我不知道如何让空格键不起作用,这意味着玩家不能先射击一段时间,然后一次射击很多子弹。
拍摄方法是这样的:
public void keyPressed(KeyEvent e) {
if (!keysDown.contains(e.getKeyCode()))
keysDown.add(new Integer(e.getKeyCode()));
这会将键值的整数添加到数组中,然后在此处读取该数组:
if (keysDown.contains(KeyEvent.VK_SPACE)) {
b = new Bullets(x);
bullCount.add(b);
System.out.println(bullCount.get(0).getY());
System.out.println ("There are " + bullCount.size() + "bullets alive.");
//elapsed = System.nanoTime() - start;
//if ((elapsed / bulletCount) < 1000000000) {
//this is where I would say 'no more bullets shot until
//average time in nanoseconds is more than 1 second!' but I don't know how
//}
最佳答案
创建一个全局变量,即:long lastShot
。
当用户射击时,在允许他射击之前检查是否(System.currentTimeMilis()-lastShot>5000)
。
如果可以拍摄,则存储lastShot = System.currentTimeMilis();
并进行实拍。
如果没有,就不要让他开枪。
这是伪代码的示例:
class SomeClass {
private long lastShot;
public void userPressedShot() {
if (System.currentTimeMillis()-lastShot>5000) {
lastShot = System.currentTimeMillis();
doTheRealShot();
}
// Ignored till 5000 miliseconds from last shot
}
}
关于java - 如何限制玩家 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30424229/