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

2017/3/19

remove objects from another class

Telko Telko

2017/3/19

#
Good evening guys, My game crashes when im trying to remove an object from another class that extend Actor.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void act()
    {
        if (getWorld() != null) {
            if (Greenfoot.isKeyDown("enter")&& Welt.i==0 && Welt.gamemode!=0) {
            getWorld().removeObject(this);
            //World W = getWorld();
            //W.removeObjects(W.getObjects(Description.class)); 
            //W.removeObjects(W.getObjects(Button.class));
             
            //Button buttons = (Button)getWorld().getObjects(Button.class);
            //getWorld().removeObject(buttons);
            //Description description = (Description)getWorld().getObjects(Description.class);   
            //getWorld().removeObjects(description);
             
            //getWorld().removeObject(getWorld().getObjects(Button.class));
            //getWorld().removeObjects(getWorld().getObjects(Description.class));
            Welt.i = Welt.i+1 ;
             
           }
        }
    }
I tried different syntaxes for removing the Object but everytime I got a NullPointerExeption. The methods function is to remove the Startscreen, Desciption and Buttons after the user selects a gamode through the buttons and presses enter.
Super_Hippo Super_Hippo

2017/3/19

#
Line 16 is probably working if you move line 5 after it. Btw, you should not use 'public static' methods in your world only to have an easier access to it from another class.
Telko Telko

2017/3/19

#
Thank you very much!
Super_Hippo wrote...
Btw, you should not use 'public static' methods in your world only to have an easier access to it from another class.
What would be an alternative? I know how to access variables from another class and i should update that part, but I dont know how to change it from another class. Thats why I started to use static variables in my programm.
Super_Hippo Super_Hippo

2017/3/20

#
If you can access it, you should be able to change them the same way. The proper way is to use getter and setter methods.
1
2
3
4
5
6
7
8
9
10
11
private varType varName;
 
public void setVarName(varType newValue)
{
    varName = newValue;
}
 
public varType getVarName()
{
    return varName;
}
You need to login to post a reply.