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

2017/5/1

How to create explosion when shooting an enemy?

ali18 ali18

2017/5/1

#
Please help me to create an explosion when i shooting an enemy.
danpost danpost

2017/5/1

#
Create a class for the explosion. Give it the image you desire. Add an int field to it to count act cycles. Have the counter incremented each act cycle. When the counter value reaches some relatively small number (test and adjust it to suit your needs), have the object remove itself. If the explosion is animated, have the object remove itself when the animation is complete.
ali18 ali18

2017/5/2

#
I don't know what you mean? I have got a image already and i have already made a class called 'explosion'. Can you please help me by giving me the code?
1
2
3
4
5
6
7
8
public void shoot()
{   if ("space".equals(Greenfoot.getKey()))
    {
        bullet shot=new bullet();
        getWorld().addObject(shot,getX(),getY());
        shot.setRotation(getRotation());
    }
}
This is what ive got in my class that shoots the bullets
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
public void act()
    {
        move(5);
        kill();
        remove();
    }
    public void kill()
    {
        Actor rocket=getOneIntersectingObject(rocket.class);
        if (rocket != null)
        {
            World myWorld = getWorld();
            getWorld().removeObject(rocket);
        }
    }
    public void remove()
    {
        Actor rocket = getOneObjectAtOffset(0, 0, rocket.class);
        if (rocket != null)
        {
            World world;
            World myWorld = getWorld();
            getWorld().removeObject(this);
        }
        else
        {
            if(getX()==599||getX()==0||getY()==0||getY()==399)
            {
                getWorld().removeObject(this);
            }
        }
    }
This is the code ive got in my bullet class.
danpost danpost

2017/5/2

#
Lines 19 will never be true as you call kill and remove any intersectin rocket before calling the remove method.. I would suggest replacing line 18 with line 9 and taking line 13 and inserting it at line 23. Then, remove line 4 and the kill method. You can also remove line 21 and 22 and insert the following line there:
1
getWorld().addObject(new explosion(), getX(), getY());
ali18 ali18

2017/5/2

#
OK, i done what you have asked and now when i shoot the rocket, it disappears when the bullet hits them, and the explosion appears, however, the explosion just stays there without disappearing. the new code within actor that shoots is:
1
2
3
4
5
6
7
8
public void shoot()
    {   if ("space".equals(Greenfoot.getKey()))
        {
            bullet shot=new bullet();
            getWorld().addObject(shot,getX(),getY());
            shot.setRotation(getRotation());
        }
    }
The new code within my bullet class is:
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
{
    /**
     * Act - do whatever the bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        move(5);
         
        remove();
    }
     
    public void remove()
    {
        Actor rocket=getOneIntersectingObject(rocket.class);
        if (rocket != null)
        {
            getWorld().addObject(new explosion(), getX(), getY());
            getWorld().removeObject(this);
            getWorld().removeObject(rocket);
        }
        else
        {
            if(getX()==599||getX()==0||getY()==0||getY()==399)
            {
                getWorld().removeObject(this);
            }
        }
    }
}
danpost danpost

2017/5/2

#
You need an int field in your explosion class to be used as a timer. Have the act method increment its value every time it executes; and have it check the value of the field for some nominal value for removing itself from the world. If you run into issues doing this, show your attempted code in the explosion class and ask for help on what you are struggling with.
ali18 ali18

2017/5/2

#
Ok forget it, i am not going to do it, because it is very complicated, and also i have not got a code within my explosion. Thank you anyways.
You need to login to post a reply.