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

2022/1/4

How to remove actor from another actor class ?

greenwood greenwood

2022/1/4

#
I used getWorld().removeObject to remove FullBar actor class in my Wraith actor class which can be seen in line 13 but it doesn't remove the actor at all. How can i remove my FullBar ?
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
public void youLose()
{
    Zombie1 zombie1eat = (Zombie1)getOneIntersectingObject(Zombie1.class);
    Zombie1R zombie1eatR = (Zombie1R)getOneIntersectingObject(Zombie1R.class);
    Zombie2 zombie2eat = (Zombie2)getOneIntersectingObject(Zombie2.class);
    Zombie2R zombie2eatR = (Zombie2R)getOneIntersectingObject(Zombie2R.class);
    HalfBar halfbar = new HalfBar();
    FullBar fullbar = new FullBar();
 
    if((zombie1eat != null && getNeighbours(getImage().getWidth()/2 , false, Zombie1.class).contains(zombie1eat) && zombie1eat.Eat(this)) || (zombie1eatR != null && getNeighbours(getImage().getWidth()/2 , false, Zombie1R.class).contains(zombie1eatR) && zombie1eatR.Eat(this)) || (zombie2eat != null && getNeighbours(getImage().getWidth() , false, Zombie2.class).contains(zombie2eat) && zombie2eat.Eat(this)) || (zombie2eatR != null && getNeighbours(getImage().getWidth() , false, Zombie2R.class).contains(zombie2eatR) && zombie2eatR.Eat(this)))
    {
        health--;
        getWorld().removeObject(fullbar);
        getWorld().addObject(halfbar, getWorld().getWidth()/3, getWorld().getHeight()/3);
    }
        if(health==60)
        {
        if(direction==1)
        {
        setImage("Wraith_01_Dying_000.png");
        getImage().scale(107,160);
        getWorld().addObject(halfbar, getWorld().getWidth()/3, getWorld().getHeight()/3);
        }
        else
        {
        setImage("Wraith_01_Dying_000.png");
        getImage().scale(107,160);
        getImage().mirrorHorizontally();
        getWorld().addObject(halfbar, getWorld().getWidth()/3, getWorld().getHeight()/3);
        }
 
        return;
    }
        if(health==30)
        {
        if(direction==1)
        {
        setImage("Wraith_01_Dying_000.png");
        getImage().scale(107,160);
        }
        else
        {
        setImage("Wraith_01_Dying_000.png");
        getImage().scale(107,160);
        getImage().mirrorHorizontally(); 
        }
        return;
    }
        if(health==0)
        {
        if(direction==1)
        {
        setImage("Wraith_01_Dying_014.png");
        getImage().scale(260,210);               
        }
        else
        {
        setImage("Wraith_01_Dying_014.png");
        getImage().scale(260,210); 
        getImage().mirrorHorizontally();
        }
        getWorld().showText("You Lose! - You lasted " +(gameTime/60) + " seconds", getWorld().getWidth()/2, getWorld().getHeight()/2);
        Greenfoot.stop();
        return;
    }
     
}
danpost danpost

2022/1/4

#
You have line 8 creating a new FullBar object -- one that is NOT in any world. And if you placed it in the world, you would then have 2 FullBar objects to remove. Replace line 8 with the following:
1
2
3
4
5
java.util.List list = getWorld().getObjects(FullBar.class);
if ( ! list.isEmpty())
{
    fullBar  = (Actor) list.get(0);
}
Then, change line 13 to:
1
if (fullBar != null) getWorld().removeObject(fullBar);
greenwood greenwood

2022/1/4

#
danpost wrote...
You have line 8 creating a new FullBar object -- one that is NOT in any world. And if you placed it in the world, you would then have 2 FullBar objects to remove. Replace line 8 with the following:
1
2
3
4
5
java.util.List list = getWorld().getObjects(FullBar.class);
if ( ! list.isEmpty())
{
    fullBar  = (Actor) list.get(0);
}
Then, change line 13 to:
1
if (fullBar != null) getWorld().removeObject(fullBar);
i got an error which the fullBar is an undeclared variable. What should i declare for the variable ?
danpost danpost

2022/1/4

#
greenwood wrote...
i got an error which the fullBar is an undeclared variable. What should i declare for the variable ?
Sorry. Start the code with the following line:
1
Actor fullBar = null;
greenwood greenwood

2022/1/5

#
danpost wrote...
greenwood wrote...
i got an error which the fullBar is an undeclared variable. What should i declare for the variable ?
Sorry. Start the code with the following line:
1
Actor fullBar = null;
Thank you so much dan! its worked :)
greenwood greenwood

2022/1/6

#
danpost wrote...
greenwood wrote...
i got an error which the fullBar is an undeclared variable. What should i declare for the variable ?
Sorry. Start the code with the following line:
1
Actor fullBar = null;
hai dan, i have another question. How to bounce back my projectile to the opposite direction when the projectile hit the world edge?
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
public void act()
{
if(Greenfoot.isKeyDown("j")
{
specialattacks();
}
}
 
    public void specialattacks()
    {
        SpecialAttack specialattack = new SpecialAttack();
         
        if(direction ==1)
        {
            getWorld().addObject(specialattack, getX()+100, getY());       
            if(atWorldEdge())
            {
                specialattack.setRotation(180);
            }
        }
         
        if(direction==2)
        {
            getWorld().addObject(specialattack, getX()-100, getY());     
            specialattack.setRotation(180);
             
            if(atWorldEdge())
            {  
                specialattack.setRotation(0);
            }
        }
 
 
    }
 
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
        {
            return true;
        }
         
        else
        return false;
    }
danpost danpost

2022/1/6

#
How about using the following to change direction:
1
direction = 3-direction:
greenwood greenwood

2022/1/6

#
danpost wrote...
How about using the following to change direction:
1
direction = 3-direction:
is this the right line to put the code ? because it doesn't work
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
public void specialattacks()
{
    SpecialAttack specialattack = new SpecialAttack();
     
    if(direction ==1)
    {
        getWorld().addObject(specialattack, getX()+100, getY());       
        if(atWorldEdge())
        {
            direction = 3-direction;
        }
    }
     
    if(direction==2)
    {
        getWorld().addObject(specialattack, getX()-100, getY());     
        specialattack.setRotation(180);
         
        if(atWorldEdge())
        {  
            direction = 3-direction;
        }
    }
 
 
}
danpost danpost

2022/1/6

#
greenwood wrote...
is this the right line to put the code ? because it doesn't work
Of course, I cannot be totally sure (especially since you have not provided any actual movement codes. However, using "else if" on line 14 might do the trick.
greenwood greenwood

2022/1/7

#
danpost wrote...
greenwood wrote...
is this the right line to put the code ? because it doesn't work
Of course, I cannot be totally sure (especially since you have not provided any actual movement codes. However, using "else if" on line 14 might do the trick.
sorry for the late reply dan. My wraith class
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
 
public class Wraith extends Actor
{
    int animateImage = 0;   //for walking animation image (frame)
    int animateSpeed = 2;   //lower the animate speed for faster walking animation //3
    int animateSpeed2 = 6//lower the animate speed for faster idle animation
    int animateSpeed3 = 1; //speed for shooting animation
    int count;
    int direction =1; //direction which actor facing
    SimpleTimer spaceTimer = new SimpleTimer();  //timer for when the next space can be tekan lagi
    private int timer;
    int gameTime =0//
    int health = 90; //wraith health
    Actor fullBar = null;
     
    public Wraith()
    {
       setImage("Wraith_01_Idle Blinking_000(try).png");
       getImage().scale(107,160);
    }
     
    public void act()
    {
        gameTime++;
        count++;
        checkKeys();
         
        //for movement restrict of wraith in the world
        if(getX()>1200) setLocation(1200,getY());
        if(getY()>550) setLocation(getX(), 550);
        if(getY()<250) setLocation(getX(),250);
         
        youLose();
         
    }
    //test
    private void checkKeys()
    {
        if(Greenfoot.isKeyDown("w")) //for moveup
        {
            setLocation(getX(), getY()-5);
        }
         
        if(Greenfoot.isKeyDown("s")) //for movedown
        {
            setLocation(getX(), getY()+5);
        }
         
        if(Greenfoot.isKeyDown("d")) //for move right
        {
            if((health !=60) && (health !=30) && (health!=0))
            {setLocation (getX() +5, getY());
            walkinganimateRight();
            }
            else
            {
                 
            }
            direction =1;
        }
         
        if(Greenfoot.isKeyDown("a")) //for move  left
        {
            setLocation (getX() -5, getY());
            walkinganimateLeft();
            direction =2;
        }
         
        if(Greenfoot.isKeyDown("space") && spaceTimer.millisElapsed() > 600
        {
            if(direction==1)
            {
                setImage("Wraith_01_Casting Spells_006.png");
                getImage().scale(260,210);
                shoot();
            }
             
            else if(direction==2)
            {
                setImage("Wraith_01_Casting Spells_006.png");
                getImage().scale(260,210);
                getImage().mirrorHorizontally();
                shoot();
            }
            spaceTimer.mark();   //reset balik timer kepada 0
        }
         
        if(Greenfoot.isKeyDown("j")&& spaceTimer.millisElapsed() >600) //special attack power
        {
            specialattacks();
            spaceTimer.mark();
             
        }
         
        //declare for no button pressed
        boolean up = Greenfoot.isKeyDown("w");
        boolean down = Greenfoot.isKeyDown("s");
        boolean right = Greenfoot.isKeyDown("d");
        boolean left = Greenfoot.isKeyDown("a");
        boolean space = Greenfoot.isKeyDown("space");
        boolean j = Greenfoot.isKeyDown("j");
         
        if(!up && !down && !left && !right && !space && !j)  //no button is pressed
        {
            if(direction ==1)
            {
                idleanimationRight();
            }
             
            if(direction ==2)
            {
                idleanimationLeft();
            }
        }
    }
     
    public void specialattacks()
    {
        SpecialAttack specialattack = new SpecialAttack();
         
        if(direction ==1)
        {
            getWorld().addObject(specialattack, getX()+100, getY());       
            if(atWorldEdge())
            {
                setRotation(specialattack.getRotation()*-1);
            }
        }
         
        if(direction==2)
        {
            getWorld().addObject(specialattack, getX()-100, getY());     
            specialattack.setRotation(180);
             
            if(atWorldEdge())
            {  
                setRotation(specialattack.getRotation()*-1);
            }
        }
 
 
    }
     
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
        {
            return true;
        }
         
        else
        return false;
    }
     
    public void walkinganimateRight()
    {
        if(count % animateSpeed == 0)
        {
            if(animateImage >11)
            {
                animateImage = 0;
            }
            setImage("Wraith_01_Moving Forward_00" + animateImage + "(try).png");
            animateImage++;
            getImage().scale(107,160);  // 260,210
             
        }
    }
     
    public void walkinganimateLeft()
    {
        if(count % animateSpeed ==0)
        {
            if(animateImage >11)
            {
                animateImage = 0;
            }
            setImage("Wraith_01_Moving Forward_00" + animateImage + "(try).png");
            animateImage++;
            getImage().scale(107,160);   //260,210
            getImage().mirrorHorizontally();
        }
    }
     
    public void shoot()
    {
        Fireball fireball = new Fireball();
        Greenfoot.playSound("Fireball Sound.wav");
        if(direction==1)
        {
            getWorld().addObject(fireball, getX()+100, getY());        //masukkan fireball ke dalam world dan berjarak 100 pixels dari wraith
        }
         
        else if(direction==2)
        {
            getWorld().addObject(fireball, getX()-100, getY());        //masukkan fireball ke dalam world dan berjarak 100 pixels dari wraith
            fireball.setRotation(180);
        }
    }
     
    public void idleanimationRight()
    {
        if(count % animateSpeed2 ==0)
        {
            if(animateImage >11)
            {
                animateImage =0;
            }
            setImage("Wraith_01_Idle Blinking_00" +animateImage + "(try).png");
            animateImage++;
            getImage().scale(107,160);  //260,210
        }
    }
     
        public void idleanimationLeft()
    {
        if(count % animateSpeed2 ==0)
        {
            if(animateImage >11)
            {
                animateImage =0;
            }
            setImage("Wraith_01_Idle Blinking_00" +animateImage + "(try).png");
            animateImage++;
            getImage().scale(107,160); //260,210
            getImage().mirrorHorizontally();
        }
    }
SpecialAttack class
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
public class SpecialAttack extends Actor
{
    int animateImage =1;
    int animateSpeed2 = 3; //animation speed
    int count;
     
    public SpecialAttack()
    {
        //setImage("frame_0_delay-0.1ss.png");
        setImage("blue-projectile1.png");
        getImage().scale(116,119);
    }
     
    public void act()
    {
        count++;
        animate();
        move(5);
    }
     
    public void animate()
    {
        if(count % animateSpeed2 ==0)
        {
            if(animateImage >2)
            {
                animateImage =1;
            }
            setImage("blue-projectile" + animateImage + ".png");
            animateImage++;
            getImage().scale(116,119);
            //getImage().scale(150,150);
        }    
    }
 
}
danpost danpost

2022/1/7

#
The following SpecialAttack act method should suffice:
1
2
3
4
5
6
7
public void act()
{
    count++;
    animate();
    move(5);
    if (isAtEdge()) turn(180);
}
greenwood greenwood

2022/1/8

#
danpost wrote...
The following SpecialAttack act method should suffice:
1
2
3
4
5
6
7
public void act()
{
    count++;
    animate();
    move(5);
    if (isAtEdge()) turn(180);
}
Thank you it finally worked, but i have one more question, how to remove the object after it bounces back at another edge of the world ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void act()
{
    count++;
    animate();
    move(3);
    if(getX()>1190 || getX()<5 && !checkEdge)
    {
        checkEdge = true;
        turn(180);
        if(isAtEdge() && checkEdge)           
        {
            getWorld().removeObject(this);
            checkEdge = false;
        }
 
    }
}
danpost danpost

2022/1/8

#
greenwood wrote...
how to remove the object after it bounces back at another edge of the world ? << Code Omitted >>
It is not a checkEdge boolean that you need -- it is a bounced boolean. The condition to bounce is when the boolean is false. Set the boolean to true when bouncing. Then, remove the actor when the boolean is true (when at edge). It would be like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private boolean bounced;
 
public void act()
{
    count++;
    animate();
    move(3);
    if (isAtEdge())
    {
        if (bounced) getWorld().removeObject(this);
        else
        {
            turn(180);
            bounced = true;
        }
    }
}
You need to login to post a reply.