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

2020/3/16

World Limit

GrucifyMe GrucifyMe

2020/3/16

#
Hello guys, I am fairly new to Greenfoot, so please bear with me. So basically I have a Player in a World, and I want to make sure that the player stays in the bottom 80% of the world. I tried by using this code: int percent = (int) 0.8 * getWorld().getHeight(); int playerLimit = getWorld().getHeight() - percent; with this scenario if(getX() + getImage().getWidth()/2 > getWorld().getWidth() || getX() - getImage().getWidth()/2 < 0 || getY() > getWorld().getHeight() || getY() < playerLimit){ move(0); } I basically dont want the player to move offscreen, and it should ONLY be in the bottom 80% of the world. I tried using this code but it results in a null pointer exception. Can someone please help?
danpost danpost

2020/3/16

#
Three things: - first, (int)0.8 evaluates to 1; better would be to use:
1
int percent = getWorld().getHeight*8/10; // or '"*4/5"
- second, if you are getting a NullPointerException, then we will need a broader look at your class codes (show entire class) - third, move(0) has no effect on how your program runs (it is like a no-op command).
GrucifyMe GrucifyMe

2020/3/17

#
Ok thank you, it worked, all I needed to do was to set the location to the bottom of the 80% of the world. Thanks, Danpost
You need to login to post a reply.