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

2017/2/23

movement and world edge

jaycee1021 jaycee1021

2017/2/23

#
hey guys, im new at greenfoot gaming, does anyone know how to create a text box and make it move side to side when it gets to the end of the world using getImage? i tried using if statements and got stuck.
Super_Hippo Super_Hippo

2017/2/23

#
jaycee1021 wrote...
how to create a text box and make it move side to side when it gets to the end of the world using getImage?
Show what you have tried. Do you already got the text box? Is the second "it" still the text box? I mean how should the text box move from side to side when reaching the end of world? What did it do before? Oh and I don't see a way how 'getImage' should be important there. You will just set the image of the text box to it once.
jaycee1021 jaycee1021

2017/2/27

#
yes i already have the text box, i just want it to move side to side when it reaches the end of the world, but i dont know what i would use to get started
danpost danpost

2017/2/27

#
Use a combination 'if' statement; use both the direction and position in determining whether to change directions or not (for both sides). Something like this:
if ((dir == 1 && getX() > getWorld().getWidth()-getImage().getWidth()/2)
    || (dir == -1 && getX() < getImage().getWidth()/2))  dir = -dir;
move(speed*dir);
You need to login to post a reply.