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

2013/12/16

how do i remove an object

Hiimachicken Hiimachicken

2013/12/16

#
can anyone post different varietals on how i could remove an object after adding it to the world?
Hiimachicken Hiimachicken

2013/12/16

#
thanks for anyone that can help
danpost danpost

2013/12/16

#
The only way is to use the World class method 'removeObject(Actor)' or 'removeObjects(Class)'. The latter will remove all objects of the class given in the parameter; the first will remove that specific object given in the parameter.
Hiimachicken Hiimachicken

2013/12/16

#
i have a code so when i hit an object it adds an object in the world how would i remove that when i go and hit another block somewhere else in the world?
danpost danpost

2013/12/16

#
You would need a field in the class of the actor that is hitting the block to hold the last object added. If the field does not contain an object (is 'null'), set it to the object added to the world. If the field contains an object, remove that object and set the field back to 'null'.
Hiimachicken Hiimachicken

2013/12/16

#
Where it says "]]] " thats where i want the remove code? can you edit the code so that it deletes the wall and re-post it back here? Thank you so much danpost for the help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
public class Yellowplayer2 extends Char
 
{
  
    private int fallSpeed = 0;
    private int acceleration = 1;
    boolean isJumping=false;
 
     
    
    public void act()
    {
 
        move();  //manage the motion horizontally
        drop();  //manage the motion vertically
         
                       start();
}
 
private void start()
{
    
 Actor first = getOneObjectAtOffset(0,0,End.class);
        if(first!=null)
        {
            
          Second h = new Second(); 
Greenfoot.setWorld(h);
 
}
Actor change = getOneObjectAtOffset(0,0,Change.class);
        if(change!=null)
        {
          Wall wall = new Wall();
          getWorld().addObject(wall,29,215);
          getWorld().addObject(new Wallup(),61,240);
          getWorld().addObject(new Wallup(),301,242);
          getWorld().addObject(new Wall(),490,333);
          getWorld().addObject(new Change2(),484,309);
         
}
Actor Firstc = getOneObjectAtOffset(0,0,Firstc.class);
        if(Firstc!=null)
        {
            
          
           
}
Actor change2 = getOneObjectAtOffset(0,0,Change2.class);
        if(change2!=null)
        {
            
                [[[[[[HELPHERE]]]]]
          
}
Actor exit = getOneObjectAtOffset(0,0,Exit1.class);
        if(exit!=null)
        {
            
//           setImage("smallblock.png");
//          Third a = new Third();
//          Greenfoot.setWorld(a);
          
}}
    public void drop(){
        if(isJumping==true)
            jump();
        else if(onGround()==false) {
            fall();
        }
        else
            standOn();
    }
 
    public boolean onGround(){
 
        Object t1=(Object)getOneObjectAtOffset(-getImage().getWidth()/2+5, getImage().getHeight()/2 , Object.class);
        Object t2=(Object)getOneObjectAtOffset(getImage().getWidth()/2-5, getImage().getHeight()/2 , Object.class);
        if(t1!=null||t2!=null) return true;
        else return false;
    }
 
    public void standOn(){
        Object t1=(Object)getOneObjectAtOffset(-getImage().getWidth()/2+5, getImage().getHeight()/2 , Object.class);
        Object t2=(Object)getOneObjectAtOffset(getImage().getWidth()/2-5, getImage().getHeight()/2 , Object.class);
        if(t1!=null)
            setLocation ( getX(), t1.getY()-t1.getImage().getHeight()/2-getImage().getHeight()/2+2);
        else if(t2!=null)
            setLocation ( getX(), t2.getY()-t2.getImage().getHeight()/2-getImage().getHeight()/2+2);
 
        fallSpeed=0;
 
        if (Greenfoot.isKeyDown("up")){
            fallSpeed = -12;
            isJumping=true;
        }
    }
 
    public void fall(){
        setLocation(getX(),getY() + fallSpeed);
        fallSpeed = fallSpeed + acceleration;
    }
 
    public void jump(){
 
        setLocation(getX(),getY() + fallSpeed);
        fallSpeed = fallSpeed + acceleration;
 
        if(fallSpeed>=0)
            isJumping=false;
 
        Object t1=(Object)getOneObjectAtOffset(-getImage().getWidth()/2+5 ,-getImage().getHeight()/2, Object.class);
        Object t2=(Object)getOneObjectAtOffset(getImage().getWidth()/2-5 ,-getImage().getHeight()/2, Object.class);
        if (t1!=null) {
            setLocation ( getX(), t1.getY() + t1.getImage().getHeight()/2+getImage().getHeight()/2+1);
            fallSpeed=0;
        }
        else if(t2!=null){
            setLocation ( getX(), t2.getY() + t2.getImage().getHeight()/2+getImage().getHeight()/2+1);
            fallSpeed=0;
        }
    }
 
    public void move(){
 
        if (Greenfoot.isKeyDown("left")){
            setLocation(getX()-7, getY());
         
        }
        Object t=(Object)getOneObjectAtOffset(-getImage().getWidth()/2 ,0, Object.class);
        if (t!=null)
            setLocation ( t.getX()+t.getImage().getWidth()/2+getImage().getWidth()/2, getY() );
        if (Greenfoot.isKeyDown("right")){
            setLocation(getX()+7, getY());
          
        }
        t=(Object)getOneObjectAtOffset(getImage().getWidth()/2 ,0, Object.class);
        if (t!=null)
            setLocation ( t.getX()-t.getImage().getWidth()/2-getImage().getWidth()/2, getY() );
    }
 
         
}
danpost danpost

2013/12/16

#
Do you want all the objects that are added when the Change object is encountered to be removed when a Change2 object is encountered?
1
2
3
4
5
6
Wall wall = new Wall(); 
getWorld().addObject(wall,29,215); 
getWorld().addObject(new Wallup(),61,240); 
getWorld().addObject(new Wallup(),301,242); 
getWorld().addObject(new Wall(),490,333); 
getWorld().addObject(new Change2(),484,309);
or a specific one or more?
Hiimachicken Hiimachicken

2013/12/16

#
Just the one object please
Hiimachicken Hiimachicken

2013/12/16

#
?
danpost danpost

2013/12/16

#
Which one? One of the Wall objects, one of the Wallup objects, or the Change2 object?
Hiimachicken Hiimachicken

2013/12/16

#
Not the chnage2 because i want the remove code inside the change2 so when i hit chnage2 it deltes and object form "chnage" and the Wallup object
Hiimachicken Hiimachicken

2013/12/16

#
Do you know how to do this dan?
danpost danpost

2013/12/16

#
You should be able to use:
1
2
getWorld().removeObjects(Wallup.class);
getWorld().removeObjects(Change.class);
This will remove ALL Wallup and Change objects.
Hiimachicken Hiimachicken

2013/12/17

#
i only wanted to remove one but i know how to now
You need to login to post a reply.