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

2016/6/28

add object to another world-subclass

MelMerio MelMerio

2016/6/28

#
Hey, I have a school project to do, so I want to make a menue where you can choose your character, so I've created some buttons in my world "character-selection". Now when you click on one of these buttons, they should add the object to the world "AllstarsWorld", but that doesn't work and I simply don't know how to write the code. I tried somethin like this in my Character1-button class;
public void act() 
    {
        if(Greenfoot.mouseClicked(this))
        {
            getAllstarsWorld.addObject(new Character1(), 262, 662);
        }
    }  
But now there's the problem; cannot find symbol - variable getAllstarsWorld
danpost danpost

2016/6/28

#
How is 'getAllstarsWorld' defined for your "Character1-button" object? Because it is not followed by parenthesis, it would be presumed to be a field; however, method names usually begin with a verb (such as 'get', which is very common for what is called a 'getter' method). Do you have a 'getAllstarsWorld' method that the "Character1-button" object can utilize? If so, follow the name in the offending code with a pair of parenthesis.
MelMerio MelMerio

2016/6/29

#
No I don't. I thougt it would work when I use the therm getWorld.addObject(new actor(), x, y) and just replace the word "World" with my AllstarsWorld because I have different world-classes and just want to add the object in a specific world. In my case the AllstarsWorld. Now I wrote it like that;
import greenfoot.*;
public class Character1Button extends C2
{
    public void act() 
    {
       if (Greenfoot.mouseClicked(this))
       {
         getWorld().addObject(new Character1(), 1060, 657);
         Greenfoot.setWorld(new AllstarsWorld());
       }    
    }   
} 
There are "no mistakes" but the character doesn't appear.
Super_Hippo Super_Hippo

2016/6/29

#
Well, you add an object to the current world and set a new world after this. Of course you can't see the object in the old world because you don't see the current world at all.
MelMerio MelMerio

2016/6/29

#
I just need something that adds an object in another world, when I click on the button in "this world"
Super_Hippo Super_Hippo

2016/6/29

#
Like this?
World a = new AllstarsWorld();
a.addObject(new Character1(), 1060, 657);
Greenfoot.setWorld(a);
MelMerio MelMerio

2016/6/29

#
Character still doesn't appear q.q
danpost danpost

2016/6/29

#
MelMerio wrote...
Character still doesn't appear q.q
The code given by Super_Hippo will add a new Character1 object into a new AllstarsWorld object and set that new world active:
World a = new AllstarsWorld(); // creates an AllstarsWorld object and names it 'a'
a.addObject(new Character1(), 1060, 657); // add a new Character1 object to world 'a' at the point (1060, 657)
Greenfoot.setWorld(a); // sets the new AllstarsWorld ('a') active
If the Character1 object does not appear in the new AllstarsWorld world, it must be due to some other code or the world not being bound and the actor being placed or immediately moved outside the normal bounds (or immediately removed). The only other possibility is that an AllstarsWorld object is being created elsewhere in your code where the Character1 object is not being created and added to it.
MelMerio MelMerio

2016/6/29

#
Thank God, I've found the problem. I tried to add the character to the character-selection world to see what is happening and why it's not working. As soon as I click on the button, which is supposed to add my character, the character appears and falls down to the edge of the world, because I told him to fall as long as he doesn't touch the ground (kind of gravity). I checked the coordinates (I've forgot that I changed the map and that the ground was much higher now) and saw that he spawns right in the ground and thats why I didn't saw him. I changed my coordinates and now it works perfectly. Thanks haha
You need to login to post a reply.