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

2015/10/22

How to copy a actor to another world

Skyus Skyus

2015/10/22

#
What I am trying to do is when the moving character is touching the portal, the actor MainCharacter that is storing all data will be copied to another actor, but it can't find variable mainCharacter in homeTown. Portal Code
World homeTown=new HomeTown();
        World w=(Start)getWorld();
        Actor MainCharacter = getOneObjectAtOffset(0,0, MaleWarrior.class);
if (MainCharacter!=null){
            homeTown.mainCharacter=w.mainCharacter;
            Greenfoot.setWorld(homeTown);}
homeTown Code
  MainCharacter mainCharacter;
    public HomeTown()
    {  super(1000, 800, 1); 
        prepare();}

    public void prepare(){
        addObject(mainCharacter,500,60);}
Start Code
public class Start extends World
{
    MainCharacter mainCharacter;
    public Start()
    {  
        super(1000, 800, 1); 
        prepare();
    }

    public void prepare(){
        mainCharacter=new MainCharacter();
        addObject(mainCharacter,50,50);

    }
}
danpost danpost

2015/10/22

#
The first line in your Portal class code creates a new HomeTown object. The 'prepare' method of the HomeTown class is executed at that time -- before you set mainCharacter of the Start world to mainCharacter of the HomeTown world. There are several ways to fix this. Now that you know why it is not being found; see if you can think of a way to correct the order of events (creation of the HomeTown object, the setting of the mainCharacter and the adding of it into the HomeTown world). I will give you this, the mainCharacter of the HomeTown object must be set either in the constructor, before calling the 'prepare' method, or from a separate method called after the creation of the world (without the prepare method).
You need to login to post a reply.