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

2014/8/22

Actor to opposite side of world

LaffyTaffy65 LaffyTaffy65

2014/8/22

#
How can i make an actor go to the opposite side of the screen when it hits the edge
Super_Hippo Super_Hippo

2014/8/22

#
1
2
3
4
5
6
7
8
int a = 20; //adjust this to a value you like. It is the distance to the side when the actor is placed on the different side.
int w = getWorld().getWidth();
int h = getWorld().getHeight();
int x = getX();
int y = getY();
 
if (x < a || x > w-a-1) setLocation(w-x-1, y);
if (y < a || y > h-a-1) setLocation(x, w-y-1);
Try this. Didn't try it, but actually it should work.
sengst sengst

2014/8/22

#
That might work, but here's an alternative: If you want it to bounce, make a method called bounce, such as: boolean goingLeft = false; public void bounce() { if ( getX() == 0 ) setLocation( (length of world), getY() ) // moves to the other side of the world immediately, and vice versa. OR if ( getX() == 0 ) goingLeft = false; if ( getX() == ( length of world ) ) goingLeft = true; if ( goingLeft == false ) setLocation( getX() + (number to move), getY() ); // moves right __ number of cells at a time. if ( goingLeft == true ) setLocation( getX() - (number to move), getY() ); // moves left __ number of cells at a time. } If you want to the object to bounce top to bottom, put the number to move after getY() instead of getX(). Hope this helps!
danpost danpost

2014/8/22

#
A highly mathematical way for an actor to wrap in a bounded world is with the following after moving:
1
2
3
4
5
6
int x = getX();
int y = getY();
int ww = getWorld().getWidth();
int wh = getWorld().getHeight();
if (x%(ww-1) == 0) setLocation((1-x/(ww-1))*(ww-3)+1, y);
if (y%(wh-1) == 0) setlocation(x, (1-y/(wh-1))*(wh-3)+1);
If your world is not bounded, then use this after moving:
1
2
3
int ww = getWorld().getWidth();
int wh = getWorld().getHeight();
setLocation((getX()+ww)%ww, (getY()+wh)%wh);
sengst sengst

2014/8/22

#
WOW!
danpost danpost

2014/8/22

#
sengst wrote...
WOW!
What? the maths?? or that it works?
sengst sengst

2014/8/22

#
I have no doubt that it works, but the math and your knowledge of Greenfoot.
danpost danpost

2014/8/22

#
My knowledge of Greenfoot had very little to do with producing the code. It does take a bit of practice to produce the needed maths as given. Mostly, it is just basic programming -- telling the system to do what you want it to do, when you want to do it. If I want to adjust the location (x and y coordinates of where the actor is in the world) with respect to the world bounds, then I would (1) get the data required (current x and y of actor in world and the world dimensions) and (2) adjust the location of the actor as necessary. I could just as easily have done it the 'long' way:
1
2
3
4
5
6
7
8
int x = getX();
int y = getY();
int ww = getWorld().getWidth();
int wh = getWorld().getHeight();
if (x == 0) setLocation(ww-2, y);
if (x == ww-1) setLocation(1, y);
if (y == 0) setLocation(x, wh-2);
if (y == wh-1) setLocation(x, 1);
which checks each edge individually and if the actor is on that edge, move it to one off the opposite edge. I just used maths to combine the checks for the two pairs of opposite edges into single statements to halve the number of 'if's. @Super_Hippo, your method is close, but not account for which edge is being encroached when adjusting the location of the actor . It will work when going toward the low valued edge; but, when going toward the high valued edge, the actor will repeatedly teleport from one edge to the opposite one. When in the high zone, '1' needs added instead of subtracted in the 'setLocation' statement. @sengst, your method is for bouncing, not for world-wrapping, and therefore was irrelevant to this discussion.
LaffyTaffy65 LaffyTaffy65

2014/8/24

#
Thanks the long way works great
You need to login to post a reply.