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

2019/5/31

Enemy follows actor

FarbKlexx FarbKlexx

2019/5/31

#
I want an actor( the enemy) to follow the Main Actor. I tried to code a method to do this but when i put the method in act() it says:"cannot find symbol - variable player". Why does it searches for a variable?
1
2
3
4
5
6
7
8
9
10
private void followPlayer(Actor player)
{
    turnTowards(player.getX(), player.getY());
    move(1);
    setRotation(0);
}
public void act()
    {
        followPlayer(player);
    }
Please Help.
danpost danpost

2019/5/31

#
FarbKlexx wrote...
I want an actor( the enemy) to follow the Main Actor. I tried to code a method to do this but when i put the method in act() it says:"cannot find symbol - variable player". Why does it searches for a variable? << Code Omitted >>
Where is player coming from in line 9? It is not a variable declared within the method. Do you have a field with player declared as its name in the class?
FarbKlexx FarbKlexx

2019/5/31

#
Its the name of an actor.
danpost danpost

2019/5/31

#
FarbKlexx wrote...
Its the name of an actor.
What code do you have in the class declaring that field and assigning the actor to it? If it is just the name of an Actor subclass, that is not enough, as a class is not an Actor object.
FarbKlexx FarbKlexx

2019/5/31

#
How do i declare that?
danpost danpost

2019/5/31

#
FarbKlexx wrote...
How do i declare that?
1
public Actor player;
It would still need to be assigned the player object.
You need to login to post a reply.