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

2016/10/28

Damage

Sepho Sepho

2016/10/28

#
A weapon is shooting a Zombie with a Bullet. The Collision method is in the Bullet Actor. The Zombie has Hitpoints so that every time i hit the zombie gets less and less hitpoints and eventually the Zombie dies can somebody give me method for that? i tried something like this: public FatZombie() { speed = 1; Hitpoints = 0; Damage = 0; } public void act() { // Add your action code here. super.stopZombie(); super.ZombieMove(); die(); } public void damage(int value) { Hitpoints = Hitpoints + value; } public void die() { if(Hitpoints==10) { LV1 world = new LV1(); world.removeObject(this); } } and in the Bullet class public int kill = 0; public boolean killed = false; public int v1= 0; FatZombie z = new FatZombie(); public SniperBullet() { speed = 80; Damage = 5; } public void act() { shootingspeed(); checkSniperBullet(); remove(); } public void checkSniperBullet() { Weapon w = new Weapon(); LV1 main = (LV1) this.getWorld(); Actor bullet = this.getOneIntersectingObject(Zombie.class); if(bullet != null ) { z.damage(5); kill++; speed--; if(kill==2) { setLocation(0,0); killed = true; } } } thx
Nosson1459 Nosson1459

2016/10/28

#
I didn't read this very carefully because you haven't mentioned an issue it might be easier to help if you say the issue. (See here for how to post code it makes reading it a lot easier.)
Sepho Sepho

2016/10/28

#
it is not working the Zombie still survives
danpost danpost

2016/10/28

#
Having the 'FatZombie z' field in the Bullet class does not make any sense. Which FatZombie object is it supposed to hold? -- certainly not a new one that is not in any world! Maybe you can use it to hold one that has been contacted by the bullet, so that you do not double up on a hit on the same zombie. Currently, you have this line in the 'checkSniperBullet' method:
1
Actor bullet = this.getOneIntersectingObject(Zombie.class);
I do not understand why you would name a Zombie object 'bullet'. If it is a Zombie object, name it 'zombie'. If the 'z' field is to hold a Zombie object, its declaration line needs to be changed to this:
1
private Zombie z = null;
Now, after the 'zombie' is assigned its value in my first code line above, you can do the following check before applying damage and increasing kill count. Actually, with the 'z' field, you do not need a kill count (as long as two kills is the limit). The 'z' field will be 'null' on the first kill and on the second one, it will hold a Zombie object. So, we can check the field for each case.
1
2
3
4
5
if (zombie != z) // new zombie kill
{
    ((Zombie)zombie).damage(5);
    if (z == null) z = zombiez; else getWorld().removeObject(this);
}
Nosson1459 Nosson1459

2016/10/28

#
You call methods that you didn't post so I can't see where the problem might be. You should post the Zombie, Bullet, and World code (the way i said in the last post).
danpost danpost

2016/10/28

#
I lost the internet for a moment, so I was not able to edit/correct my last code post. It should be:
1
2
3
4
5
if (zombie != z) // new zombie kill
{
    ((Zombie)zombie).damage(5);
    if (z == null) z = zombie; else getWorld().removeObject(this);
}
Nosson1459 Nosson1459

2016/10/28

#
danpost wrote...
I lost the internet for a moment, so I was not able to edit/correct my last code post. It should be:
1
2
3
4
5
if (zombie != z) // new zombie kill
{
    ((Zombie)zombie).damage(5);
    if (z == null) z = zombie; else getWorld().removeObject(this);
}
you just posted this in your first (and was only) post you didn't edit/correct anything now it's there twice. My last post was talking to Sepho because when I was typing it your post didn't come up on my screen yet so it wasn't to you. I just realized the extra 'z'
Sepho Sepho

2016/10/28

#
thank you i will try to fix it now
Sepho Sepho

2016/10/28

#
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
public class Bullet extends Actor
{
    private int life;
    private boolean dead = false;
    public int speed;
    public int Damage;
    private Zombie z = null;
    /**
     * 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()
    {
        shootingspeed();
        checkBullet();
       
        
        
        
    }   
     public void shootingspeed()
    {
       
       move(speed);
         
    }
    public void checkBullet()
    {
       Weapon w = new Weapon();
       LV1 main = (LV1) this.getWorld();
       Zombie z = new Zombie();
         
         
        Actor zombie = this.getOneIntersectingObject(Zombie.class);
         
        if(zombie != null )
         
        {
             
             
           if(z== null)
            {
                zombie = z;
            }
             
            else
            {
                getWorld().removeObject(this);
                main.removeObject(zombie);
            }
            
             
            
           
            
             
           
            
        }
         else if ( getX() <= 0|| getY()==0||getY() == 774)
        
             main.removeObject(this);
        }
         
       
         
         
    }
    
     
     
    
    
     
}
 
public class FatZombie extends Zombie
{
    /**
     * Act - do whatever the FatZombie wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public FatZombie()
    {
       speed = 1;
       Hitpoints = 0;
       Damage = 0;
         
    }
    public void act()
    {
        // Add your action code here.
        super.stopZombie();
        super.ZombieMove();
        die();
         
    }   
     
    public void damage(int value)
    {
        Hitpoints =  Hitpoints + value;
         
    }
    public void die()
    {
        if(Hitpoints==10)
        {
            LV1 world = new LV1();
            world.removeObject(this);
        }
         
    }
     
}
so i got this so far
Nosson1459 Nosson1459

2016/10/28

#
In Bullet speed is not equal to anything and in FatZombie the method damage is not called to change the hitPoints (I think they're supposed start with a lowercase letter)
danpost danpost

2016/10/28

#
Line 109 is trying to remove an object, 'this', a FatZombie object, from a newly created world, 'world', which 'this' is not in. You can check for death immediately after the 'Hitpoints' value is changed. Remove line 95, 103, 104, 105 and 108; and change line 109 to:
1
getWorld().removeObject(this);
It also appears that the 'Damage' field on line 87 is not being used; you can remove that line. It also appears that you have change your code to have a bullet remove any contacted zombie immediately; so zombies are killed with one hit. This makes the 'Hitpoints' field of no consequence. Maybe you should explain what exactly you are trying to achieve -- first, I though you wanted the bullet to hit two zombies and zombies take 5 damage per bullet and die after 10 hit points; now, I cannot be sure.
Sepho Sepho

2016/10/28

#
This was just a try. I just want that the zombie dies not with one hit but with few hits depending on the damage the Bullet does. for example: Bullet does 5 damage and the zombie has 10 hitpoint so he dies with 2 hits.
You need to login to post a reply.