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

2012/4/11

Changing World image during the game

dlanni dlanni

2012/4/11

#
I want to create a game that begins in a room that has doors out to other World images. I've been playing with Mik's Pengu (3061), to figure out how to do this. He supplies a Level2 image that can be accessed if the penguin arrives at x=749 with this method: private void checkNextLevel() { if (getX() == getWorld().getWidth()-1) { if (level == 1) { level = 2; getWorld().removeObject(this); Greenfoot.setWorld(new Level2(this)); } else { level = 1; getWorld().removeObject(this); Greenfoot.setWorld(new Level1(this)); } } } I am able to add a Level3 and have it operational ( Pengu falls into a pond when he falls instead of gameOver ) by changing the x to a y and changing getWidth to get Height ( and adding code in my Level3 world ). However, every attempt I make to have Pengu wander off the Left side of the image fails. When I type " if (getX() == getWorld().getWidth()-749) " the width = 750 the code compiles, but Pengu can't exit the world into Level2 via the left side. I have tried multiple variants of getX( )==1, getX ==||< 30, getX()=<25. I assume the same problem will happen if I try to leave Level1 via the north. 1. Is it necessary to getWorld.getWidth or is it just more elegant? Don't I have a getX() within my class? 2. Why does leaving the screen via the bottom to a new level 3 work, but re-using Level2 for the left side not work. ( I am assuming that if I draw a new Level 4 and attach it properly to the left side, Pengu will be able to go that way, but maybe I'm missing something). 3. Is it necessary to do the 'this' stuff when instantiating a new world image if my character is not carrying a cumulative variable (score)?, or is it good form that requires this. 4. Are there any tutorial videos or examples of downloadable code or discussions that I should look at to better understand this?
danpost danpost

2012/4/11

#
Did you try getX() == 0?
dlanni dlanni

2012/5/4

#
Thank you! That works. I tried so many numbers and even if (getX() == getWorld().getWidth() - (getWorld().getWidth()-1)) !
You need to login to post a reply.