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

2016/1/23

make object disappear after 3 hits (take damage)

1
2
3
nico_vo nico_vo

2016/1/23

#
Good night to everyone. I am currentlyy working on a game, where if the spaceship gets hit 3 times, it disappears. The thing, is that I don't know why it doesn't work. Here is the code:
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
private boolean Kaputt = false;
     
    public void takeDamage()
    {
         
        Actor Obstacle;
        Obstacle = getOneObjectAtOffset(0, 0, Obstacle.class);
 
        Actor Rocket;
        Rocket = getOneObjectAtOffset(0, 0, rocket.class);
         
        if(Obstacle!=null)
        {
                        
            removeTouching(Obstacle.class);           
            damage = damage+1;           
             
            if(damage > 3){
                Kaputt = true;                 
            }
            World world;
            if(Kaputt = true){
                world = getWorld();
                world.removeObject(this);
            }
             
        }
    }
I also want an explosion to occur when the obstacle gets hit (I already have the bit where the obstacle/asteroid disappears). I have created a gif image using GIMP 2, which I would also like to come up. any ideas? Thanks in advance :)
danpost danpost

2016/1/23

#
One major issue is at line 22 -- where you are setting 'Kaputt' to true (instead of checking to see if its value equals true). To check for the condition of equality, you need to use the conditional equality symbol '=='. A couple of things about your code: (1) Lines 9 and 10 define a Rocket object reference, which is not later used in the method (those lines can be removed); (2) field and variable names (such as 'Kaputt', 'Obstacle', and 'Rocket'), by convention, should begin with lowercase characters;
nico_vo nico_vo

2016/1/26

#
okay, thank you. now I want the rocket to shoot, but I don't know how to make the object appear in the world. this is what I've got:
1
2
3
4
5
6
7
8
9
10
public void shoot(){
 
        Actor shot;
        shot = getOneObjectAtOffset(0, 0, Shot.class);
         
         
        world.addObject(shot, getX(), getY());
 
 
            }
the error I get is the following: java.lang.NullPointerException at rocket.shoot(rocket.java:22) *line 22 is world.addObject(shot, getX(), getY()); thank you! :D
danpost danpost

2016/1/26

#
You are assigning 'shot' an object that is not yet in the world (it is assigned a 'null' value at line 3), and then trying to add it (nothing) into the world (at line 7). You need to create a 'new Shot' object to add into the world; not pull one out of the hat!
nico_vo nico_vo

2016/1/27

#
okay, thank you again :) In my game there are some "power ups" that give you 10 bullets per power up, so that when you have less tan 1, it does not shoot. I have done this, but it doesn't work. For you it may seem obvious, but I'm just starting with programming ;)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int shotsLeft = 0;
 
public void shoot(){
        shotsLeft = shotsLeft - 1;
        Shot shot = new Shot();
        getWorld().addObject(shot, getX(), getY());
 
    }
 
public void act()
{
      Actor powerUpShoot = getOneIntersectingObject(PowerUp.class);
      if(powerUpShoot != null){
            shotsLeft = shotsLeft +10;
 
            if(shotsLeft > 0){
                if(Greenfoot.isKeyDown("space")){
                    shoot();
                }
            }
        }
}
I think it's right, but obviously it is not, but it doesn't show up any error on the terminal.
danpost danpost

2016/1/27

#
Well, your logic is off. The way it is coded, you will not be able to shoot at all (lines 16 through 19) unless you are touching a powerup (lines 12 and 13).
nico_vo nico_vo

2016/1/27

#
I see what you mean. could you please tell me how could I do it? it can just be an example, just for guidance EDIT: I changed it so that it is like this, but still doesn't work:
1
2
3
4
5
6
7
8
9
10
if(powerUpShoot != null){
            shotsLeft = shotsLeft +10;
 
             
        }
        if(shotsLeft > 0){
                if(Greenfoot.isKeyDown("space")){
                    shoot();
                }
            }
danpost danpost

2016/1/27

#
nico_vo wrote...
I changed it so that it is like this, but still doesn't work < Code Omitted >
The way you changed it seems okay. If it is still not working, you will need to show more of your code and explain in more detail how it is not working properly.
nico_vo nico_vo

2016/1/27

#
That's all I have for the shooting part. I just found out that it's not always detecting that the rocket touches the powerup, by writing System.out.println("you currently have" +shotsLeft ); when the rocket gets the powerup, but it does not always show up. there are some times, where I can shoot, but not always. EDIT: I think I know why it does not always work. the connclusion I've reached has to do with the hitbox of the rocket, as I created it myself in GIMP. I guess that's the problema because when the asteroids are falling down, they sometimes disappear without touching the rocket, but looking at it a bit closer, I can see that the hitbox is a big square around the rocket, which can't be seen because background is transparent. Is it posible to somehow make a hitbox by making another object move and be "under" the rocket, so that it can't be seen, but that it is the trigger for receiving damage and collecting the bullets' powerup?
danpost danpost

2016/1/27

#
There are a couple things that could be causing problems here. First, I do not see any code to remove the powerup when touched. If this is in the class of the powerup, remove it from there and place it in the class of the rocket (this may be a case of the proverbial "left hand" not knowing what the "right hand" is doing). Another thing is that there is no delay between shots. Ten act cycles can pass very quickly and ten shots overlapping each other would not look much larger than a single shot by itself (although I cannot determine the speed of the shots; the faster they move, the longer the blur of shots will appear). You did not specify what value was given in your printed output -- the value of 'shotsLeft' (both when able to shot and when not).
nico_vo nico_vo

2016/1/27

#
after posting the code, I realized that there was no removing of the powerup, so I added it. all what I've showed before was in the rocket's class when I can shot, then, as you say, all ten shots go at the same time, but I will fix that later on. First, I want that the rocket takes the powerup everytime
danpost danpost

2016/1/27

#
nico_vo wrote...
when I can shot, then, as you say, all ten shots go at the same time
Not at the same time, but on consecutive act cycles -- one per cycle.
First, I want that the rocket takes the powerup everytime
This should not be a problem. How is it not doing what you want?
nico_vo nico_vo

2016/1/27

#
danpost wrote...
This should not be a problem. How is it not doing what you want?
It's just that the rocket doesn't always get the 10 bullets specified by the code when it touches the powerup (although it does actually disappear) I think it's just something to do with the hitbox, so I think that it can be fixed by adding another object (maybe a ball or two) that takes the damage of the rocket and collects the powerups. Bullets can also come out from there the actual hitbox of the rocket is just a square, way too big in comparison with the rocket, so that it makes things disappear, even though it's not visually touching them, and just collect the bullets when the powerup(a small red ball) touches the actual rocket, and ot the square round it. I hope you understand what I'm trying to say, but it's quite difficult to explain ;)
danpost danpost

2016/1/27

#
nico_vo wrote...
It's just that the rocket doesn't always get the 10 bullets specified by the code when it touches the powerup (although it does actually disappear)
If the adding of 10 to the 'shotsLeft' is done along with the code to remove the powerup, then it must be doing both. Please show your revised code for further review. (best would be to show the entire class code) btw, I do understand the concept of the hitbox and its issues. There are ways to reduce the issues involved which we can consider after getting the shot count issue fixed.
nico_vo nico_vo

2016/1/27

#
okay, here's: my entire rocket 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
import greenfoot.*;
 
public class rocket extends Actor
{
    GifImage gifImageRight = new GifImage("Rocket(right).gif");
    GifImage gifImageLeft = new GifImage("Rocket(left).gif");
    GifImage explosion = new GifImage("explosion.gif");
    private boolean isShotReady = false;
    int damage = 0;
    int shotsLeft = 0;
    World world;
 
     
 
    public void GameOver(){
        GameOver gameover = new GameOver();
        getWorld().addObject(gameover, getWorld().getWidth()/2, getWorld().getHeight()/2-100);
         
        restart restart = new restart();
        getWorld().addObject(restart, getWorld().getWidth()/2, getWorld().getHeight()/2-50);
    }
 
    public void damage() {
        Actor obstacle = getOneIntersectingObject(Obstacle.class);
        getWorld().removeObject(obstacle);
        damage = damage +1;
    }
     
    public void control(){
        if(Greenfoot.isKeyDown("d")){     
            setImage(gifImageRight.getCurrentImage());
            move(4);
        }
        else if(Greenfoot.isKeyDown("right")){
            setImage(gifImageRight.getCurrentImage());
            move(4);           
        }
 
        if(Greenfoot.isKeyDown("a")){ 
            setImage(gifImageLeft.getCurrentImage());
            move(-4);
        }
        else if(Greenfoot.isKeyDown("left")){
            setImage(gifImageLeft.getCurrentImage());
            move(-4);
        }
 
        if(Greenfoot.isKeyDown("w")){
            setLocation(getX(), getY()-4);
            setImage(gifImageRight.getCurrentImage());
        }
        else if(Greenfoot.isKeyDown("up")){
            setLocation(getX(), getY()-4);
            setImage(gifImageRight.getCurrentImage());
        }
 
        if(Greenfoot.isKeyDown("s")){
            setLocation(getX(), getY()+4);
            setImage(gifImageRight.getCurrentImage());
        }
        else if(Greenfoot.isKeyDown("down")){
            setLocation(getX(), getY()+4);
            setImage(gifImageRight.getCurrentImage());
        }
    }
 
    public void act()
    {
        control();
         
        Actor obstacle = getOneIntersectingObject(Obstacle.class);
        Actor powerUpShoot = getOneIntersectingObject(PowerUp.class);
        if(obstacle != null){
            damage();
        }
 
        if(damage == 3){
            GameOver();  
            setImage(explosion.getCurrentImage());
            getWorld().removeObject(this);
             
        }
 
        if(powerUpShoot != null){
            shotsLeft = shotsLeft +10;
            System.out.println("you currently have" +shotsLeft );
            getWorld().removeObject(powerUpShoot);
                         
        }
        if(shotsLeft > 0){
                if(Greenfoot.isKeyDown("space")){
                    shoot();
                }
            }       
 
    }
    public void shoot(){
        shotsLeft = shotsLeft - 1;
        Shot shot = new Shot();
        getWorld().addObject(shot, getX(), getY());      
 
    }
}
And here's my shot code:
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
import greenfoot.*;
 
 
public class Shot extends Actor
 
{
    public boolean atWorldEdge(){
        if(getX() <= 5 || getX() >= getWorld() . getWidth() -5){
            return true;
        }
        if(getY() <= 5 || getY() >= getWorld() . getHeight() -5){
            return true;
        }
        else{
            return false;
        }
    }
     
    public void act()
    {
        setLocation(getX(), getY()-6);
         
        if(this.atWorldEdge()){
             
            World world;
            world = getWorld();
            world.removeObject(this);
 
        }   
    }   
}
There are more replies on the next page.
1
2
3