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

2012/5/15

How can i detect the edge of the world?

dandefender2 dandefender2

2012/5/15

#
hi, i am doing some work for college and am stuck as to how i detect the edge of the world so i can remove one actor and make another bounce. i would also like some help with that too (the bounce). thanks
danpost danpost

2012/5/15

#
What code have you tried regarding the edge of the world detection, and the bounce?
dandefender2 dandefender2

2012/5/16

#
i havent got any :/ still pretty new to this.
davmac davmac

2012/5/16

#
An actor is at the edge of the world when: - the X co-ordinate is 0 (the left edge of the world) - the Y co-ordinate is 0 (the top edge of the world) - the X co-ordinate is the width of the world minus one (the right edge) - the Y co-ordinate is the height of the world minus one (the bottom edge)
steved steved

2012/5/16

#
I used this code to make it sort of bounce off the edges but I'm still working on it:
        if (getX() <= 15)
        {
            setRotation(getRotation() + 80);
        }
        if (getY() <= 15)
        {
            setRotation(getRotation() + 80);
        }
        if (getX() >= getWorld().getWidth() - 15)
        {
            setRotation(getRotation() + 80);
        }
        if (getY() >= getWorld().getHeight() - 15)
        {
            setRotation(getRotation() + 80);
        }
You could change the distance by changing the number so it fits the size of of your object. Or you could also change the angle by changing the number in the set rotation method.
You need to login to post a reply.