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

2017/1/26

Teleporting (of Sorts)

Greenfoot1234 Greenfoot1234

2017/1/26

#
Hiya guys, I am making a flappy bird game and I figured instead of spawning lots of pipes, I could just use the same ones and when they reached the edge of the world they could 'teleport' to the other side at a random height. so I used the atWorldEdge code and it didn't work because it 'teleported' to the other side but it was then at the world edge so it 'teleported' back to the other side, etc, etc. So I googled around and I found some code to do with " if getX=1700 { setLocation , 0 , 0 } " i don't know how to do it and i have tried for ages any help would be greatly appreciated :)
Super_Hippo Super_Hippo

2017/1/26

#
Usually the pipes go from right to left, so from high X value to low X while x=0 is the left edge of the visible world. So you could add in the act method of the pipes:
if (getX() == 0)
{
    setLocation(getWorld().getWidth(), getY());
}
But usually it looks better if the pipes actually moving from and to outside of the world. You can do that by changing
super(600, 400, 1); //size of the world is your choice obviously
to
super(600, 400, 1, false);
Then the objects can move outside the visible world.
if (getX() < -20)
{
    setLocation(getWorld().getWidth()+20, getY());
}
Usually, there is no reason to teleport them instead of adding new ones though because you want to have the space between the pipes randomly instead of having three periodic space positions.
danpost danpost

2017/1/26

#
When teleporting from one side to the other, place the pipes inside the point where you would teleport it back the other way.
Greenfoot1234 Greenfoot1234

2017/1/26

#
Thanks so much!! it Works!! Using the teleporting method is it possible to set a random height so it wont be the same every time it comes? Sorry to be a pest, just excited :/
danpost danpost

2017/1/26

#
Greenfoot1234 wrote...
Thanks so much!! it Works!! Using the teleporting method is it possible to set a random height so it wont be the same every time it comes? Sorry to be a pest, just excited :/
Just teleport it using the same expressions for the y-coordinate that you used when you spawned it to begin with (probably with an added 'getWorld().' because you are now working from an Actor subclass instead of a World subclass).
Greenfoot1234 Greenfoot1234

2017/1/26

#
Thanks danpost and Super_Hippo for everything !!
You need to login to post a reply.