Question

I'm making a fighting game and I want the actions of the player to be timed so you can't spam the attack key and win easily.

Here is where I do the keyboard stuff and delay. It does delay the first time, but then it the delay slowly decreases in time and ends up being 0 and lets you spam the key. As you can see I've done many things to try and stop the key from registering in the delay etc.

public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    if (isAction == false) {
        if (key == KeyEvent.VK_LEFT) {
            dx = -1;
            if (nHeroX < -10) {
                dx = 0;
            }
            isRight = false;
            isMoving = true;
        } else if (key == KeyEvent.VK_RIGHT) {
            dx = 1;
            if (nHeroX > 1200) {
                dx = 0;
            }
            isRight = true;
            isMoving = true;
        } else if (key == KeyEvent.VK_C) {
            dx = 0;
            isAction = true;
            isMoving = false;
            isBlock = true;
            nImage = 1;
        } else if (key == KeyEvent.VK_X) {
            dx = 0;
            isAction = true;
            isMoving = false;
            isWeak = true;
            nImage = 2;
        } else if (key == KeyEvent.VK_Z) {
            dx = 0;
            isAction = true;
            isMoving = false;
            isStrong = true;
            nImage = 3;
        } else if (key == KeyEvent.VK_P) {
            if (!pause) {
                pause = true;
            } else if (pause) {
                pause = false;
            }
        }
    }
}

public void keyReleased(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_LEFT || key == KeyEvent.VK_RIGHT && !isAction) {
        dx = 0;
        isMoving = false;
        nState = nImage = 1;
    } else if (key == KeyEvent.VK_C && !isWeak && !isStrong) {
        delayTask = new DelayTask();
        tmrDelay.schedule(delayTask, 0, 500);
    } else if (key == KeyEvent.VK_X && !isBlock && !isStrong) {
        z = new DelayTask();
        tmrDelay.schedule(z, 0, 450);
    } else if (key == KeyEvent.VK_Z && !isBlock && !isWeak) {
        x = new DelayTask();
        tmrDelay.schedule(x, 0, 1200);
    }
    nImgNum = (int) (Math.random() * 6 + 1);
    nDelay = 0;
}

//http://www.javaprogrammingforums.com/java-se-api-tutorials/883-how-use-tmrDelay-java.html
class DelayTask extends TimerTask {

    public int nTimes = 0;

    @Override
    public void run() {
        nTimes++;
        if (nTimes == 2) {
            isAction = isBlock = isStrong = isWeak = false;
            nState = nImage = 1;
        }
    }
}

Can someone explain why my delay is messed up? Thank you.

Also this code:

private class Keys extends KeyAdapter {

    @Override
    public void keyPressed(KeyEvent e) {
        hero.keyPressed(e);
    }

    @Override
    public void keyReleased(KeyEvent e) {
        hero.keyReleased(e);
        if (hero.getPause()) {
            repaint();
        }
    }
}
Was it helpful?

Solution

The simplest way to do this is just to remember the last time.

So:

 private long lastTime = 0;

 void doAction() {
     long timeNow = System.currentTimeMillis()
     if (lastTime + MIN_DELAY < timeNow) {
        return;
     }
     lastTime = timeNow;

     // Do action
 }

All the stuff with timers etc is just approaching this from a much more complicated architecture than you need to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top