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

2016/2/15

Remove an object from a World, Add to Another at the same time?!?!?

1
2
eve19 eve19

2016/2/15

#
Hello everybody! I've been trying for days but I can't manage to do it. I want to remove an object from a world (when the object is clicked) and I want this object to be added to another world at the same time. 1.When I was searching how to do it from other discussions, I found that way to remove an object ((WorldName) getWorld()).removeObject(this); and it worked only temporarily( when I went to another Word and then return to the previous one,the object was there again). 2.Although that problem existed, I thought that this command can be used to add an object too to another world ((OtherWorld)getWorld()).addObject(this,300,200);, but that gave me that error: java.lang.NullPointerException at key.act(key.java:33) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) Actor Class
 public void act() 
    {
        
  
   if (Greenfoot.mouseClicked(this)) {

      ((Fireplace) getWorld()).removeObject(this);
  ((Boy)getWorld()).addObject(this,300,200);
    
}}
I hope somebody can help me,I'm really stuck.Thanks in advance
Super_Hippo Super_Hippo

2016/2/15

#
To remove an object from the world it is currently in, you can simply use this:
getWorld()).removeObject(this);
So line 7 works if the object is in a 'Fireplace' world. Line 8 does not work because 'getWorld()' returns null. The object was just removed from the world, so it isn't in any world right now. Casting it to 'Boy' can not work. You need a reference to the 'Boy' world in which you want to add the object. 'getWorld' only returns the world in which the object is in right now.
danpost danpost

2016/2/15

#
Actually, you do not even have to remove the actor from the world it is currently in -- just add it into the world you want to place it in. It will automatically be removed from the old world. However, like Super_Hippo said, you need a reference to that world you are trying to move it into:
// instance reference field
private World boy; // which somewhere is assigned a world created from the Boy class

// transfer code
boy.addObject(this, 300, 200);
eve19 eve19

2016/2/15

#
If I understood right,I wrote to Actor Class
public void act() 
    {
    Boy Boy=new Boy();
     if (Greenfoot.mouseClicked(this)) {
Boy.addObject(this, 300, 200);}
}
The object was removed from the other class as my previous code temporarily(I think that this is my fault because I didn't mentioned before that I did that to add the object to Fireplace World
public Fireplace()
    {
        key key=new key();
addObject(key, 300, 200);
 
    }
but it also wasn't added to the Boy class. I really cannot understand what to do.
Super_Hippo Super_Hippo

2016/2/15

#
In line 3, you create a new 'Boy' instance. You don't save it anywhere, so this world is never actually active.
eve19 eve19

2016/2/15

#
The Whole Actor Code , as far as I understand
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class key extends Actor
{
   public World Boy;
 public void act() 
    {
    
     if (Greenfoot.mouseClicked(this)) {

Boy.addObject(this, 300, 200);}
}
}
and now it gives me an error: java.lang.NullPointerException at key.act(key.java:45) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
eve19 eve19

2016/2/15

#
How can I activate the world as you said??
Super_Hippo Super_Hippo

2016/2/15

#
Ok... question to you: You talked about going from one world to another and back. How did you do that if you don't know how it works? By the way, you get the nullpointer exception because 'Boy' is never set to anything else.
eve19 eve19

2016/2/15

#
Here is the code for your question
public void act()
    {   Greenfoot.setSpeed(30);     
        if (Greenfoot.isKeyDown("left"))
        {
            Greenfoot.setWorld(new Living());
        }
        else if (Greenfoot.isKeyDown("right"))
        {
            Greenfoot.setWorld(new Boy());
        }
        else if (Greenfoot.isKeyDown("up")){
            Greenfoot.setWorld(new FireplaceZoom());
        }      
}
But again I don't understand finally how to do that :/
Super_Hippo Super_Hippo

2016/2/15

#
Ok, line 9: You create a new (!) Boy world and set it active. (Use line 2 in the constructor, it doesn't have to be executed every time.)
//outside methods in the Fireplace world
public World boy = new Boy();

//change line 9 to this
Greenfoot.setWorld(boy);
In your key class, you can use this then (I think):
if (Greenfoot.mouseClicked(this) && getWorld() instanceof Fireplace)
{
    ((Fireplace)getWorld()).boy.addObject(this, 300, 200);
}
eve19 eve19

2016/2/15

#
Oh my god!! Finally!! Thank you very much! Now another problem, I want the object to be removed permanetly from the Fireplace World and be on the boy World until the user removes it again. I'm really sorry if I'm bothering you :/
Super_Hippo Super_Hippo

2016/2/15

#
Save a reference of the Fireplace world and return to this world instead of creating a new one. (I guess you do that right now.)
//Change
public World boy = new Boy();
//to
public World boy = new Boy(this);
In the Boy class
public World fireplace;

public Boy(World fp)
{
    fireplace = fp;
}
//When returning to the Fireplace world
Greenfoot.setWorld(fireplace);

//or, from an actor being in the Boy world
Greenfoot.setWorld(((Boy)getWorld()).fireplace);
eve19 eve19

2016/2/15

#
Ok I've done everything you said, but:
Super_Hippo Super_Hippo

2016/2/15

#
Does your scenario start in the Boy world? In this case, you have to do it the other way around.
eve19 eve19

2016/2/15

#
No my scenario starts in the Fireplace World!!
There are more replies on the next page.
1
2