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

2017/2/6

Teleporting

Greenfoot1234 Greenfoot1234

2017/2/6

#
Hiya everyone, I'm making a 'pacman' based game and In it there is 4 walls (obviously) and in the middle of each wall there is a teleporter so when it gets there I want 'pac' to teleport to the other side but I don't know how to do it. I presume you would have to enter a variation of this " if (getX() < -40) { setLocation(getWorld().getWidth()+40, getY()); } " but I don't know how to do it. that is the x code, so I thought you would do this for the y code " if (getY() < -40) { setLocation(getWorld().getHeight()+40, getX()); }" any help would be greatly appreciated please ;D
Super_Hippo Super_Hippo

2017/2/6

#
You might want to swap + und - in the code you showed. Also, in the Y part, you have to swap both parameters in the call to the setLocation method.
danpost danpost

2017/2/6

#
You have the right idea. Just make sure that the new location is inside of the area where teleporting the other way is executed (use '39' and '-39' in the 'setLocation' statements). I will presume that you are using unbounded world (using 'super(int, int, int, false)' world constructor call).
danpost danpost

2017/2/6

#
Greenfoot1234 wrote...
I thought you would do this for the y code " if (getY() < -40) { setLocation(getWorld().getHeight()+40, getX()); }"
The 'x' part always comes first for the parameters of the 'setLocation' method call. So, this should be:
1
setLocation(getX(), getWorld().getHeight()+40);
You will need counterparts to these two checks to teleport the other way. I said this:
danpost wrote...
use '39' and '-39' in the 'setLocation' statements
however, using '40' is okay since you are checking beyond that (using '<' and '>' and not '<=', '>=' or '==').
Greenfoot1234 Greenfoot1234

2017/2/7

#
thanks it worked ;D
Greenfoot1234 Greenfoot1234

2017/2/7

#
if (getY() < -40) { setLocation(getX(), getWorld().getHeight()+40); } if (getY() < +40) { setLocation(getX(), getWorld().getHeight()-40); } if (getX() < -40) { setLocation(getY(), getWorld().getWidth()+40); } if (getX() < +40) { setLocation(getY(), getWorld().getWidth()+40); } turns out it doesn't work :( one wall teleports but the others don't
Super_Hippo Super_Hippo

2017/2/7

#
You should notice some other errors. You may not notice them if the world is a square and the teleporting happens in the middle of the edges. However, it should look like this:
1
2
3
4
5
int x=getX(), y=getY(), w=getWorld().getWidth(), h=getWorld().getHeight();
if (x<40) setLocation(w-41, y);
else if (x>w-41) setLocation(40, y);
if (y<40) setLocation(x, h-41);
else if (y>h-41) setLocation(x, 40);
Creating the variables just make it a bit easier and the 40/41 part so it is symmetric. The most important difference is that you don't check for +40/-40 but for +40/other edge - 40 (or 41) and that the first parameter in setLocation is always the X coordinate and the second one always Y. Same goes with getWidth and getHeight.
Greenfoot1234 Greenfoot1234

2017/2/7

#
thanks that works, but you can see pac teleporting is there a way to teleport whilst off screen, so it looks smoother, I have done this in the Stage by the way "super int, int , int, false" ? thanks
danpost danpost

2017/2/7

#
Greenfoot1234 wrote...
thanks that works, but you can see pac teleporting is there a way to teleport whilst off screen, so it looks smoother, I have done this in the Stage by the way "super int, int , int, false" ? thanks
Just change the signs of all instances of '40' and '41' (they can be all '+40' and '-40' btw).
You need to login to post a reply.