Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
1  
You want the damage to be delayed per enemy right? So each enemy is going to have to track the last time it damaged the player. –  Byte56 Sep 4 '12 at 18:46
    
so add a delay-method for every enemy? yeah that makes sence. do you have any good logic for a delay-timer? cus Im having problems with that part. –  iQue Sep 4 '12 at 19:00
add comment

2 Answers

up vote 4 down vote accepted

Just add a cooldown for any action that needs to be delayed. When the action is performed set the cooldown to a proper value and deduct the time in your update loop. Something like:

Class Entity 
{
  const float COOLDOWN_TIME = 10; //in seconds

  private float cooldown = 0;

//...//

public void Update(float time)
{
  cooldown -= time;
}

public bool attack()
{
  if(cooldown <= 0)
  {
    bringThePain();
    cooldown = COOLDOWN_TIME; 
    return true;
  }else{
    return false;
  }

} 

}
share|improve this answer
    
you are a lifesaver, changed it around a bit but I finally got how the logic works. Will still hate timers for the rest of my life tho! :D –  iQue Sep 4 '12 at 21:33
    
Haha ok, I think you're better off with update method with main loop time in then :) –  Mikael Högström Sep 4 '12 at 21:38
add comment

This should be pretty easy to achieve. Just save a System.currentTimeMillis() for every enemy when he damages you, and check the difference to the current System.currentTimeMillis() if it is larger than an threshold you get damage and the enemy timer is newly set.

private void checkCollision(Canvas canvas) {
    synchronized (getHolder()) {
        long time = System.currentTimeMillis();
        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)){
                    if(time - enemies.get(i).getTime() > 2000) {
                        happy.damageHP(5);
                        enemies.get(i).setTime(time);
                    }
                }

                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
                }
            }
        }
    }
}
share|improve this answer
    
thanks for the answer. Im trying this but cant get it to work. I added a time-variable in my enemy-class, and a setter/getter for that variable.. But for some reason it will only damage my character once, then it just stands there without doing any more damage. I must be missing something. Timers are my nemesis, have a really hard time understanding it. –  iQue Sep 4 '12 at 21:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.