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

2014/6/10

Enemies with A.I.

exifern exifern

2014/6/10

#
How do I make an enemy chase my player wherever he goes?
danpost danpost

2014/6/10

#
Locate player, turn toward player, and move towards player. Once you have a reference to the player, henceforce referred to as 'player', you need only
turnTowards(player.getX(), player.getY());
move(/* speed */);
Getting the reference to the player can be handled in different ways. You can maintain a reference to it in a field (preferable if your enemy will always chase the player) or you can locate it by using 'getObjectsInRange' (preferable if your enemy only chases the player when it is near the enemy).
exifern exifern

2014/6/10

#
I need PlayerPink to chase PlayerBlue just like if the computer had logic.That is my code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; public class PlayerPink extends PlayerBlue { private GreenfootImage pink = new GreenfootImage("PinkPlayer.jpg"); int lastMovement = Greenfoot.getRandomNumber(4); int newMovement = lastMovement; public void act() { turnTowards(PlayerBlue.getX(), playerBlue.getY()); moveTron(); die(); leaveTrail(getX(), getY()); } I get the error non-static method get(X) cannot be referenced from a static context.
lordhershey lordhershey

2014/6/10

#
the problem is you have PlayerBlue.getX(), not an instance of PlayerBlue. looks like a typo probably should be playerBlue.getX()
exifern exifern

2014/6/10

#
Nah,I still get the same error..I changed the code to this public void act() { Actor PlayerPink = (Actor)getWorld().getObjects(PlayerBlue.class).get(0); turnTowards(PlayerPink.getX(), PlayerPink.getY()); moveTron(); die(); leaveTrail(getX(), getY()); } But PlayerPink does not follow the Blue one again
danpost danpost

2014/6/10

#
The minor problem is that you are using 'PlayerBlue.getX()' instead of 'playerBlue.getX()'. The major problem is that you have the PlayerPink class extending the PlayerBlue class. They should probably both extends Actor. Think of it this way: a PlayerPink object is NOT a PlayerBlue object, nor is the latter an object of the first type. Therefore, neither should extend the other. Both create Actor object, so they can both extend Actor. Both are player objects and you can add a Player class that extends Actor that both of these classes can extend. In your PlayerPink class, add a field to hold the PlayerBlue object -- private PlayerBlue playerBlue;. Then, add the following method to that class:
public void setPlayerBlue(PlayerBlue pb)
{
    playerBlue = pb;
}
Then, in your world class constructor (or a method it calls), when you are creating the actors and adding them into the world, declare a local fields to hold the players -- PlayerBlue pb = new PlayerBlue(); and PlayerPink pp = new PlayerPink();. Now, you can just call the new method with pp.setPlayerBlue(pb);. Once all this is done, you can then use playerBlue.getX() and playerBlue,getY() in the act method of the PlayerPink class.
exifern exifern

2014/6/11

#
I am trying to add private PlayerBlue but I get the error "modifier private not allowed here".Anyway thanks for the answer
danpost danpost

2014/6/11

#
You were probably trying to declare the field inside a method. Move it (private PlayerBlue playerBlue;) outside the method; but, keep it within the class.
exifern exifern

2014/6/11

#
Alright,thank you
You need to login to post a reply.