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

2020/3/7

Making a random enemy move back and forth while still punching?

yko1 yko1

2020/3/7

#
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!");

            }
        }
    }

}
danpost danpost

2020/3/7

#
There seems to be a few "anomalies" in your given code. The first thing that jumped out at me was line 31 -- following by line 32. Of course it will be true there as you just assigned it the value true. Line 38 is kind of strange as well. Setting its value there does not make any sense as the scope of the variable is one line short of ending and that last line is not dependent on its value. Next thing was line 48 in the moveBack method. I doubt that you want the "move back to its spot" to be a changing location, always 100 pixels below the main kid. Then there are lines 34 thru 36 and lines 53 thru 55. I do not understand why you would attempt to run the timer in two different places. Finally, there is the act method. It appears you want all behaviors to execute at the same time. There is no control over what should happen when. The bully has a choice of 3 possible actions at any one time -- that is: approach, punch or retreat. Also, the order in which these 3 actions occur will always be the same. Therefore an action counter field can be used to indicate which of the 3 is currently being implemented. The approach and retreat actions may vary in the time it takes to complete; however, the punch will always take the same amount of time. You already have a timer field for punch time, so your good to go, there.
yko1 yko1

2020/3/7

#
Yes, I expected that there would be random things in my code...I'm not quite sure what I am doing. The true or false values were so that if the Bully touched the MainKid, it would move back (after performing punch, which I did not add yet), and then it would go to the start point and then the value would change and it would start over. The moveBack method was kind of a test to see if the Bully would move back (I did not set the values yet). I ran the time for the punchMain because I wanted there to be a delay so that the Bully did not get any more extra hits than 1, if that is correct. I understand what you mean the act method, that was a careless mistake on me! I totally forgot that the act method would run everything. Thank you so much!
You need to login to post a reply.