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

2013/5/21

HELP PLEASE! move to setLocation

1
2
3
danpost danpost

2013/5/21

#
The reason for the way out-of-range number in the first two lines is sort of a cheat in determining whether those values have been set to a location to move to. Line 6 uses that condition in the decision to set new coordinates. As far as using abs: I guess it was a matter of choice (between using it and Math.signum). Either way, both the absolute value and the unary sign value were needed to complete the method.
forest forest

2013/5/21

#
I tried replacing the -1000s by just a number 1, and the code works just as well. That explains it, thank you.
forest forest

2013/5/21

#
And one last thing.
randY = Greenfoot.getRandomNumber(getWorld().getHeight())-getImage().getHeight()/2+20;   
for this line, I get that you divided by 2 to get the center, but what is the number 20 for?
danpost danpost

2013/5/21

#
I used it to compensate for half the height of the spider itself. You can drop it if you want the spider to average slightly higher in the screen.
forest forest

2013/5/22

#
Thanks guys!
You need to login to post a reply.
1
2
3