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

2013/5/10

Following problem

1
2
flopa92 flopa92

2013/5/10

#
Hy! I created a world with some peoples and an elevator,I want to make the people to go to the elevator and when this happens the world should change,how is it possible!
schetefan24 schetefan24

2013/5/10

#
much details :D could you maybe upload your current programm? my first try for a code:
public class person
{
public void act()
{
if(getOneIntersectingObject(elevator.class) != null)
{
setWorld(name of your new world)
}
else
{
move(1);
}
}
}
that code checks wheter you have contact to an elevator and if you have contact a new world will be set, otherwise you move one field on... not the best, but it's not possible to integrate it to your project without having the code schetefan24
Gevater_Tod4711 Gevater_Tod4711

2013/5/10

#
To make the people move to the elevator you should use the turnTowards method. If you know the positoin of the elevator you can just use this position to make the people turn to the elevator. If you have exectued this once they just have to move. Then if all people have reached the elevator you set the new world. Probably it would be the easiest way to remove the people that tuched the elevator and set the new world when no people are there anymore. Therefore you need some code like this in the peoples:
private boolean turnedToElevator = false;

public void act() {
    if (!turnedToElevator) {
        turnTowards(elevatorX, elevatorY);//of course the coordinates of the elevator have to be known.
        turnedToElevator = true;
    }
    move();
    if (getOneIntersectingObject(Elevator.class) != null) {
        getWorld().removeObject(this);
    }
}
That will make the people move to the elevator and remove themselves if they reached it. Then you need to check whether there are no people in the world anymore. You should do this in the world class:
public void act() {
    if (getObjects(People.class).isEmpty()) {
        Greenfoot.setWorld(new World2());
    }
}
Instead of the names for the classes I used you have to use the real names of the classes (like World2(), People, ...).
flopa92 flopa92

2013/5/10

#
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 Actor { /** * Act - do whatever the florin2 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setLocation(getX() + 1, getY() + 2) ; // Add your action code here. } }
flopa92 flopa92

2013/5/10

#
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 florin 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. } }
schetefan24 schetefan24

2013/5/10

#
whats the name of your world? than I can give you the finished code and you also should give the positon of the elevator.. there is a possibility to get it, but you probably wouldn't get it ^^
flopa92 flopa92

2013/5/10

#
name of the world is wall
schetefan24 schetefan24

2013/5/10

#
can I get the code of the world too please?
schetefan24 schetefan24

2013/5/10

#
and why are you using setLocation(getX() + 1, getY() + 2) ; I thought, that they always go the elevator... please tell me how you want it
flopa92 flopa92

2013/5/10

#
i want that the peoples go to the elevator,where ever you set the elevator ,when all the people reache the elevator then they should go to another world
schetefan24 schetefan24

2013/5/10

#
ok change the method act() in your world Wall to:
public void act() {  
    if (getObjects(People.class).isEmpty()) {  
        Greenfoot.setWorld(new Wall());  
    }  
}  
the Class Florin2 change to:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List; 

/**
 * Write a description of class florin2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Florin2 extends Actor
{
	private boolean turnedToLift=false;
	private Lift lift;
	
    /**
     * florin2 turns towards an Lift, moves to it and removes itself after touching one 
     */
    public void act() 
    {
		if(lift==null)
		{
			getLift();
		}
		if (!turnedToLift)
		{  
			turnTowards(lift.getX(),lift.getY()); 
			turnedToLift = true;  
		}  
		move(1);  
		if (getOneIntersectingObject(Lift.class) != null)
		{  
			getWorld().removeObject(this);  
		}
	}
	
	private void getLift()
	{
		List<Lift> LiftList = getObjects(Lift.class);
		if(!LiftList.isEmpty())
		{
			lift = LiftList.get(0)
		}
	}
}

have a try, whether it's working :)
flopa92 flopa92

2013/5/10

#
this is my world where should i change? import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class wall here. * * @author (your name) * @version (a version number or a date) */ public class wall extends World { /** * Constructor for objects of class wall. * */ public wall() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); } }
flopa92 flopa92

2013/5/10

#
in florin2 error by copiling at lift = LiftList.get(0)
flopa92 flopa92

2013/5/10

#
error List<lift> LiftList = getObjects(lift.class);
flopa92 flopa92

2013/5/10

#
cannot find symbol-method getObjects (java.lang.Class<lift>)
There are more replies on the next page.
1
2