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

2014/1/22

Changing actors when coming into new world

swendleys swendleys

2014/1/22

#
I got two world. I have objects with the class "container" in the world "Terrein" if you touch them you'll enter the other world "InsideContainer". In this world you have a object that returns you to Terrein. But when i return i like the object with the class "container" replaced with another actor how to i do that. I have this so far.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Container here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Container extends Actor
{
    /**
     * Act - do whatever the Container wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       touchContainer();
    } 
       public void touchContainer()
    {
        Actor agent = getOneIntersectingObject(Agent.class);
        if(agent != null)
        {
            Greenfoot.setWorld(new InsideContainer());
            GecheckteContainer container = new GecheckteContainer();
            getWorld().addObject(container,getX(),getY());
           
        }
}
}
danpost danpost

2014/1/22

#
Please post the InsideContainer class code.
swendleys swendleys

2014/1/22

#
InsideContainer is a world. Here is the code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class InsideContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class InsideContainer extends World
{
    /**
     * Constructor for objects of class InsideContainer.
     * 
     */
    public InsideContainer()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 375, 1); 
        randomCrates();

        prepare();
    }

    public void randomCrates()  
    {  
        for (int n=0; n<4; n++)  
        {  
            // addObject(krat2,0,0);  
            //krat2.setLocation(1, 1);  
            Krat krat = new Krat();
            Krat2 krat2 = new Krat2();
           
            // create a crate  
            addObject(krat, 0, 0);  // add it into the world  
            int gap = 5; // space between crates  
            int across = krat.getImage().getWidth()+gap; // horiz interval  
            int down = krat.getImage().getHeight()+gap; // vert interval  
            while (krat.getX() == 0 || krat.getY() == 0 || getObjectsAt(krat.getX(), krat.getY(), Krat.class).size() > 1) 
            {  
                int x = Greenfoot.getRandomNumber(getWidth()/across)*across+across/2;  
                int y = Greenfoot.getRandomNumber(getHeight()/down)*down+down/2;  
                krat.setLocation(x, y);  
            }  
        }  
         Krat2 krat2 = new Krat2();
        addObject(krat2, 0, 0); 
    }

    private void prepare()
    {
        Agent agent2 = new Agent();
        Door door = new Door();

        addObject(agent2, 70, 207); 
        addObject(door,4,188);

    }
}
danpost danpost

2014/1/22

#
Please state the location of and post that code that returns to the Container world (and the relevant code around it.
swendleys swendleys

2014/1/22

#
this returns you to the "Terrein" world
public void touch()
    {
        Actor door = getOneIntersectingObject(Door.class);


        if(door != null)
        {
            Greenfoot.setWorld(new Terrein());
            GecheckteContainer container = new GecheckteContainer();
            getWorld().addObject(container,getX(),getY());
        }
    }
danpost danpost

2014/1/22

#
Ok. The problem is that lines 9 and 10 are making changes in your InsideContainer world and not in your Terrein world. Create a local reference to the new Terrein world object and perform the changes on it.
swendleys swendleys

2014/1/22

#
this returns you to the "Terrein" world
public void touch()
    {
        Actor door = getOneIntersectingObject(Door.class);


        if(door != null)
        {
            Greenfoot.setWorld(new Terrein());
            GecheckteContainer container = new GecheckteContainer();
            getWorld().addObject(container,getX(),getY());
        }
    }
if you touch the object with class door in the "InsideContainer" world you'll return to the "Terrein" world but when i return to the "Terrein" world i would like to replace a already existing actor with another actor in the "Terrein" world. I hope this can help. And i'm not sure but what you mean with state the location of?
danpost danpost

2014/1/22

#
'state the location of' refers to 'what is the name of the class that the touch method (this code) is located'. But at this point, I figure it is 'Agent'.
swendleys swendleys

2014/1/22

#
Yes its located in the "Agent". But I'm stuck because I don't know how to create a reference to the new Terrein world object when i touch the door with the "Agent"
danpost danpost

2014/1/22

#
Just like declaring a local field;
// local int field
int n = 50;
// local Terrein field
Terrein terrein = new Terrein();
This assigns a value 'new Terrein()' to the field named 'terrein' that is declared to hold a value of 'Terrein' type (just list the value '50' is assigned to the field named 'n' that is declared to hold a value of 'int' type). Then just refer to 'terrein' for that world.
You need to login to post a reply.