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

2021/2/4

Enemy movement

JustAnArchosaur JustAnArchosaur

2021/2/4

#
So for my code at the moment, my enemy actor spontaneously moves around the screen, what I want it to do is bounce around the screen like the DVD logo here's my code so far. public void moveAndTurn() { move(3); if (Greenfoot.getRandomNumber(100) < 10) { turn(Greenfoot.getRandomNumber(45)); } if (getX() <= 5 || getX() >= getWorld().getWidth() - 5) { turn(180); } if (getY() <= 5 || getY() >= getWorld().getWidth() - 5) { turn(180); } }
danpost danpost

2021/2/4

#
JustAnArchosaur wrote...
So for my code at the moment, my enemy actor spontaneously moves around the screen, what I want it to do is bounce around the screen like the DVD logo
I want you to describe in detail what should happen (on each act step).
JustAnArchosaur JustAnArchosaur

2021/2/5

#
First, the actor is coded to move at a speed of three, I'm not quite sure how this code works (if (Greenfoot.getRandomNumber(100) < 10)) but I believe it's stating that between moving 10 and 100 units on the map, which then the actor will turn 45 degrees, and the rest of the code I believe states that the actor should turn 180 degrees when at the world border.
danpost danpost

2021/2/5

#
JustAnArchosaur wrote...
First, the actor is coded to move at a speed of three,
Alright:
move(3):
I'm not quite sure how this code works (if (Greenfoot.getRandomNumber(100) < 10)) but I believe it's stating that between moving 10 and 100 units on the map, which then the actor will turn 45 degrees
I believe you mean:
if (Greenfoot.getRandomNumber(100) < 10)
{
    turn(45);
}
which is to say, at a one in ten (or 10 out of 100) chance, "turn 45 degrees at random times averaging 6 turns per second" (at normal scenario speed ~ 60fps).
the rest of the code I believe states that the actor should turn 180 degrees when at the world border.
should be something like this:
if (isAtEdge())
{
    turn(180);
}
Okay, the random turning can be removed. To bounce off edges like you want, the horizontal and vertical speed will need to be dealt with individually and to maintain a speed of 3, the location coordinates and the speeds will need to be tracked more precisely (using double instead of, as in the Actor class, int type). So, add the four fields:
private double xSpeed, ySpeed, xLocation, yLocation;
Since the initial location is not known until the actor is added into the world, add the following overriding method, which also initializes the speed values:
protected void addedToWorld(World world)
{
    xLocation = getX();
    yLocation = getY();
    int rand = Greenfoot.getRandomNumber(8);
    turn(rand*8);
    xSpeed = Math.cos(Math.PI*getRotation()/180)*3;
    ySpeed = Math.sin(Math.PI*getRotation()/180)*3;
}
Now, to turn at edge, you probably do not want any part of the actor's image to go beyond the edge. So:
private void moveAndTurn()
{
    xLocation += xSpeed;
    yLocation += ySpeed;
    setLocation((int)xLocation, (int)yLocation);
    int imgWidth = getImage().getWidth();
    int imgHeight = getImage().getHeight();
    if ((getX() < imgWidth/2 && xSpeed < 0)  || (getX() > getWorld().getWidth()-imgWidth()/2 && xSpeed > 0)) xSpeed = -xSpeed;
    if ((ySpeed < 0 && getY() < imgHeight/2) || (ySpeed > 0 && getY() > getWorld().getHeight()-imgHeight()/2) ySpeed = -ySpeed;
}
That should do it.
You need to login to post a reply.