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

2014/1/1

If ball goes to a certain area in the world sets it's location

Moynzy Moynzy

2014/1/1

#
Hey guys, I know how to do this on Action Script 3? But not Java //If (the ball goes in this square area 50 pixels wide and 100 pixels long) //{then move the ball back 100 on it's x axis} I hope this is enough information.
bourne bourne

2014/1/1

#
Something like this (where ball is an Actor):
if (ball.getX() + ball.getImage().getWidth() / 2 > areaLeft && ball.getX() - ball.getImage().getWidth() / 2 < areaRight && ball.getY() + ball.getImage().getHeight() / 2 > areaTop && ball.getY() - ball.getImage().getHeight() / 2 < areaBottom)
    ball.setLocation(ball.getX() - 100, ball.getY());
This checks if the ball overlaps any bit of the area. Note Actors' images are centered around their locations, which is why I add or subtract dimensions of the image.
Moynzy Moynzy

2014/1/1

#
My friend, your code gave me errors. This is my scenario... Please take a look, thank you for your help! Image for Scenario
bourne bourne

2014/1/1

#
What errors? And sorry I forgot to mention the variables areaLeft, areaRight, areaTop, and areaBottom should be defined ints according to their described names.
Moynzy Moynzy

2014/1/1

#
here you are http://postimg.org/image/v1420lahf/
bourne bourne

2014/1/1

#
Looks like you are calling this inside the ball class so you don't need the ball reference from my example.
if (getX() + getImage().getWidth() / 2 > areaLeft && getX() - getImage().getWidth() / 2 < areaRight && getY() + getImage().getHeight() / 2 > areaTop && getY() - getImage().getHeight() / 2 < areaBottom)  
    setLocation(getX() - 100, getY());
Moynzy Moynzy

2014/1/1

#
thank you borne, and I define the ints like this ? AreaLeft = AreaLeft?
Moynzy Moynzy

2014/1/1

#
private int AreaLeft;
bourne bourne

2014/1/1

#
Right with values set to what you want: private int AreaLeft = 10; // The x coordinate of the left side of the area of interest
Moynzy Moynzy

2014/1/1

#
You are GREAT do you know that that! Thank you!!!!
You need to login to post a reply.