Adding delay between damage

Posted by iQue on Game Development See other posts from Game Development or by iQue
Published on 2012-09-04T18:37:12Z Indexed on 2012/09/04 21:51 UTC
Read the original article Hit count: 246

Filed under:
|
|

I have a bunch of enemies chasing my main-character, and if they intersect I want them to damage him and that's all good. The problem is that right now they damage him as long as they stand around him, every frame! and since it gets called every frame my character's HP reaches 0 almost instantly.

I've tried adding delay and I've tried a timertask, but can't get it to work.

This is the code I use to check for intersection:

private void checkCollision(Canvas canvas) {
    synchronized (getHolder()) {
        Rect h1 = happy.getBounds();
        for (int i = 0; i < enemies.size(); i++) {
            for (int j = 0; j < bullets.size(); j++) {
                Rect b1 = bullets.get(j).getBounds();
                Rect e1 = enemies.get(i).getBounds();
                if (b1.intersect(e1)) {
                    enemies.get(i).damageHP(5);
                    bullets.remove(j);
                }
                if(e1.intersect(h1)){
                    happy.damageHP(5);
                // this is the statement that needs some sort of delay, I want them to damage him every 2 seconds they intersect him.
                }

                if(enemies.get(i).getHP() <= 0){
                    enemies.get(i).death(canvas, enemies);
                    score.incScore(5);
                    break;
                }
                if(happy.getHP() <= 0){
                    score.incScore(-50);
                    //end-screen
                }
            }
        }
    }
}

If anyone knows the logic to do this please do tell.

© Game Development or respective owner

Related posts about 2d

Related posts about android