3
\$\begingroup\$

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – House
    Sep 4, 2012 at 18:46
  • \$\begingroup\$ 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. \$\endgroup\$
    – Green_qaue
    Sep 4, 2012 at 19:00

2 Answers 2

5
\$\begingroup\$

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;
  }

} 

}
\$\endgroup\$
2
  • \$\begingroup\$ 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 \$\endgroup\$
    – Green_qaue
    Sep 4, 2012 at 21:33
  • \$\begingroup\$ Haha ok, I think you're better off with update method with main loop time in then :) \$\endgroup\$ Sep 4, 2012 at 21:38
3
\$\begingroup\$

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
                }
            }
        }
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Green_qaue
    Sep 4, 2012 at 21:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .