This site requires JavaScript, please enable it in your browser!
Greenfoot back
ImAndrew
ImAndrew wrote ...

2018/1/18

Wrong method behavior

ImAndrew ImAndrew

2018/1/18

#
I have this method which should move the enemy to the hero when is attacked but it moves all the enemies and not the only one attacked.What should I do?
    void checkForEnemyTrigger()
    {
        int dx = getDistanceX(this.getX(), hero.getX());
        int dy = getDistanceY(this.getY(), hero.getY());
        if((hero.getAttacked()) && (dx > attackRange || dy > attackRange || dx < -attackRange || dy < -attackRange))
        {
            turnTowards(hero.getX(), hero.getY());
            move(speed);
        }
    }
xbLank xbLank

2018/1/18

#
This code is in what class?
danpost danpost

2018/1/18

#
The 'getAttacked' method only returrns a boolean value indicating whether an attack is in progress or not. It does not give any information as to which enemy is being attacked. If it did, you could compare the returned reference to 'this' (the enemy asking).
ImAndrew ImAndrew

2018/1/18

#
일리아스 wrote...
This code is in what class?
It is in Enemy class.
danpost wrote...
The 'getAttacked' method only returrns a boolean value indicating whether an attack is in progress or not. It does not give any information as to which enemy is being attacked. If it did, you could compare the returned reference to 'this' (the enemy asking).
getAttacked return true if hero attacks the enemy. I want to let the melee enemy follows the hero when he(hero) attacks with a range weapon.
CxVercility CxVercility

2018/1/19

#
Your code is super confusing Why is it called Hero.getAttacked when he is the one attacking lol Also, shouldn't dx/dy be smaller than your max attack range? Besides that, as Dan already said you need a reference to the enemy being attacked to do something on that object only. One way would be to use a method in the enemy class that checks if the projectile hits them and then do something
You need to login to post a reply.