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

2013/6/20

doesn't work from world 2 to world 3 and 4

flopa92 flopa92

2013/6/20

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Florin2 here. * * @author (your name) * @version (a version number or a date) */ public class Florin2 extends Animal { /** * Act - do whatever the Florin2 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int speed = 2; private int level; public Florin2() { level = 1; } public void act() { checkKeys(); tryToSeeLift(); } private void checkKeys() { if (Greenfoot.isKeyDown("left") ) { moveLeft(); } if (Greenfoot.isKeyDown("right") ) { moveRight(); } if (Greenfoot.isKeyDown("up") ) { moveUp(); } if (Greenfoot.isKeyDown("down") ) { moveDown(); } } public void moveRight() { setLocation ( getX() + speed, getY() ); } public void moveLeft() { setLocation ( getX() - speed, getY() ); } public void moveUp() { setLocation(getX(), getY() - speed); } public void moveDown() { setLocation(getX(), getY() + speed); } public void tryToSeeLift() { if (canSee(Lift.class) ) { checkNextLevel(); } } private void checkNextLevel() { Wall wl = new Wall(); if (getX() == getWorld().getWidth()-1) { if (level == 1) { level = 2; getWorld().removeObject(this); Wall2 wl2 = new Wall2(); Greenfoot.setWorld(wl); } else if(getY() >= 398){ if(level == 2){ level = 3; getWorld().removeObject(this); Wall3 wl3 = new Wall3(); Greenfoot.setWorld(wl3); }else if (level == 3){ level = 4; getWorld().removeObject(this); Wall4 wl4 = new Wall4(); Greenfoot.setWorld(wl4); }else if (level == 4){ getWorld().removeObject(this); Greenfoot.setWorld(wl); Greenfoot.stop(); } } } } }
flopa92 flopa92

2013/6/20

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Lift here. * * @author (your name) * @version (a version number or a date) */ public class Lift extends Actor { /** * Act - do whatever the Lift wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. } }
Gevater_Tod4711 Gevater_Tod4711

2013/6/20

#
What is the problem concerning this code?
Zamoht Zamoht

2013/6/20

#
I have a question for the bottom of your Florin2 code. You write the following:
if (level == 1) {
     level = 2; 
     getWorld().removeObject(this);
     Wall2 wl2 = new Wall2();
     Greenfoot.setWorld(wl); 
}
What i don't get is Greenfoot.setWorld(wl); Why do you set the world to wl when I bet you want to change it to wl2.
danpost danpost

2013/6/20

#
The problem is that when you change worlds, you are creating a new Florin2 object for that world. When a Florin2 object is created, it get a new 'level' field which is set in the constructor to an initial value of one (1). Either pass the Florin2 object or the 'level' field value across to each new world in the world constructor call (you will need to add a constructor with an argument to your Wall world class.
You need to login to post a reply.