Hello, I am very new to coding with Greenfoot. Currently, I am coding a fighting game with one player and the other is a computer. I need help randomly moving my computer character back and forth, but still going towards my main character (being controlled by a real person). Basically the computer character would move to my main character once the main character is in range of it. It would punch once and move back to its spot. Currently my computer character just moves to the right wall and stays there while constantly punching. Please help!
import greenfoot.*;
import java.util.List;
public class BullyActions extends BullyKid
{
private int time = 3;
GifImage BpunchImage = new GifImage("punchBully.gif");
public void act()
{
moveBully();
turnTowardsG();
stopPlayer();
punchMain();
}
public void turnTowardsG()
{
List <BullyActions> onePlayer = getObjectsInRange(1200, BullyActions.class);
Actor goodkid = (Actor)getWorld().getObjects(MainKid.class).get(0);
for(BullyActions oP:onePlayer)
{
turnTowards(goodkid.getX(), goodkid.getY());
}
}
public void stopPlayer()
{
if(isTouching(MainKid.class))
{
boolean touch = true;
if (touch){
moveBack();
if (time>0)
time--;
if(time == 0)
{
touch = false;
turnTowardsG();
}
}
}
}
public void moveBack()
{
Actor goodkid = (Actor)getWorld().getObjects(MainKid.class).get(0);
turnTowards(goodkid.getX()+100, goodkid.getY());
}
public void punchMain()
{
if (time>0)
time--;
if(time == 0)
{
setImage(BpunchImage.getCurrentImage());
time = 3;
if(isTouching(BullyActions.class))
{
//punch
System.out.print("Punch!");
}
}
}
}
