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

2021/4/29

How do you find out if your actor is in the top-half and bottom-half?

BlueHand BlueHand

2021/4/29

#
Hi. How can you make a method that tells if an object is in the top-half and bottom-half of the world? The world is 700x400. I think it might be an if statement about the y-value. Also clockwise is turn(90); and counterclockwise turn(-90); ?
Super_Hippo Super_Hippo

2021/4/29

#
BlueHand wrote...
Hi. How can you make a method that tells if an object is in the top-half and bottom-half of the world? The world is 700x400. I think it might be an if statement about the y-value.
Yes, you can get the current Y-coordinate of your actor with the “getY” method. You can then have two conditions which you connect with || (=or). Keep in mind that y=0 is at the top of the screen.
Also clockwise is turn(90); and counterclockwise turn(-90); ?
Yes.
BlueHand BlueHand

2021/4/30

#
Would the code look like:
if (getY() >= 200  )
        {
            turn(90)
        }
else
         turn(-90);
danpost danpost

2021/4/30

#
BlueHand wrote...
Would the code look like: << Code Omitted >>
??? What are you trying to accomplish, here?
BlueHand BlueHand

2021/4/30

#
danpost wrote...
BlueHand wrote...
Would the code look like: << Code Omitted >>
??? What are you trying to accomplish, here?
I'm trying to use the getY method that Super_Hippo suggested.
danpost danpost

2021/4/30

#
BlueHand wrote...
I'm trying to use the getY method that Super_Hippo suggested.
To do what?
danpost danpost

2021/4/30

#
There must be another condition placed on turning, otherwise your actor(s) will spin around in tight circles -- about 15 times every second.
BlueHand BlueHand

2021/4/30

#
I'm trying to make it so that an object (participant) turns 90 degrees every time it encounters an obstacle. The direction of the turn should be different depending if they are in the top-half or bottom-half of the world. Top-half turn clockwise, bottom-half turn counterclockwise.
danpost danpost

2021/4/30

#
BlueHand wrote...
I'm trying to make it so that an object (participant) turns 90 degrees every time it encounters an obstacle. The direction of the turn should be different depending if they are in the top-half or bottom-half of the world. Top-half turn clockwise, bottom-half turn counterclockwise.
Okay, so the code you have shown is inside another if block, like "if (isTouching(Obstacle.class))". Then your code should be okay. It could be more succinct, like this:
turn(getY() < 200 ? -90 : 90);
or
turn(90*Math.signum(getY()*2-399));
You need to login to post a reply.