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

2017/4/2

Adding health

Childiish Childiish

2017/4/2

#
I want to add health so I can make it so that if i hit my actor (seal) it would take 3 shots to full kill it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void Shot()
{
    Actor bullet = getOneObjectAtOffset(0, 0, Bullet.class);
    if (bullet != null)
    {
        getWorld().removeObject(bullet);
        health--;
        if (health == 0){
            getWorld().removeObject(this);
            World myWorld = getWorld();
            SealWorld sealworld = (SealWorld)myWorld;
            Counter counter = sealworld.getCounter();
            counter.addScore(100);
    }
}
Im not sure why it wont work, it just adds 100 score every time i hit the seal but it wont die. Do I need to put the code into another class?
Super_Hippo Super_Hippo

2017/4/2

#
It seems like there is a } missing for the { in line 8. The code you are showing will never add score because you would receive a runtime error. So probably you are adding the score from somewhere else.
Childiish Childiish

2017/4/3

#
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Bullet here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Bullet extends Mover
{
 
    /**
     * 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(10.0);
 
        /** 
         * If Bullet hits over the Dolphin it will disappear.
         */
        Actor Dolphin;
        Dolphin = getOneObjectAtOffset(0,0, Dolphin.class);  
 
        if (Dolphin != null)
        {
            World myWorld = getWorld();
            myWorld.removeObject(Dolphin);
            Space space = (Space)myWorld;
            Counter counter = space.getCounter();
            counter.addScore(1);
 
        }
        Actor Seal;
        Seal = getOneObjectAtOffset(0,0, Seal.class);
         
        if (Seal != null)
        { World myWorld = getWorld();
             
            SealWorld sealworld = (SealWorld)myWorld;
            Counter counter = sealworld.getCounter();
            counter.addScore(100);
        }
        /**
         * Bullet destroyed When hits Wall.
         */
        Actor Bullet;
        Bullet = getOneObjectAtOffset(0,0, Bullet.class);
        if (Bullet != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Bullet);
        }
        if  (atWorldEdge())
        {
            World world;
            world = getWorld();
            world.removeObject(this);
        
    }
}
Thats my bullet class (sorry I thought I attached it)
Super_Hippo Super_Hippo

2017/4/3

#
Okay, right now, in the Bullet class you check for intersection with a Seal and the Seal checks for intersection with a Bullet... only check the intersecting in one class. You could do it like this:
1
2
3
4
5
6
7
8
9
10
//in Bullet
Seal seal = (Seal) getOneIntersectingObject(Seal.class);
if (seal != null)
{
    if (seal.loseLife(1))
    {
        ((SealWorld) getWorld()).getCounter().addScore(100);
    }
    getWorld().removeObject(this);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
//in Seal
private int health = 3;
 
public boolean loseLife(int amount)
{
    health -= amount;
    if (health < 1)
    {
        getWorld().removeObject(this);
        return true;
    }
    return false;
}
danpost danpost

2017/4/3

#
We will probably need to see the entire Seal class code.
Childiish Childiish

2017/4/4

#
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Snake here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Seal extends Actor{
    public int health = 3;
public void Shot()
{
    Actor Bullet = getOneObjectAtOffset(0, 0, Bullet.class);
    if (Bullet != null)
    {
        getWorld().removeObject(Bullet);
        health--;
         
        if (health == 0){
            getWorld().removeObject(this);
            World myWorld = getWorld();
            SealWorld sealworld = (SealWorld)myWorld;
            Counter counter = sealworld.getCounter();
            counter.addScore(100);
        }
    }
    else{
        health = health - 1;
        }
}
 
   public Seal()
    {
        GreenfootImage image = getImage(); 
        image.scale(300, 300);
        setImage(image);
        }
         
     
    /**
     * Act - do whatever the Seal wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
     
    {
        move(4);
        if (Greenfoot.getRandomNumber(50) < 10)
        {
            turn(Greenfoot.getRandomNumber(3) - 3);
        }
        if (getX() <= 5 || getX() >= getWorld().getWidth() - 5)
        {
            turn(180);
        }
        if (getY() <= 5 || getY() >= getWorld().getHeight() - 5)
        {
            turn(180);
        }
        
        Actor rocket;
        rocket = getOneObjectAtOffset(0,0, Rocket.class);
        if (rocket != null)
         {
            World myWorld = getWorld();
             
            Greenfoot.playSound("not_really_fine.wav");
            //Display game over text
            GameOver gameover = new GameOver(false);
            myWorld.addObject (gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(rocket);
       
    }
}
thats the whole Seal class
Super_Hippo Super_Hippo

2017/4/4

#
Look, the Shot method is never executed. I would still suggest you try what I posted.
Childiish Childiish

2017/4/4

#
Super_Hippo wrote...
Okay, right now, in the Bullet class you check for intersection with a Seal and the Seal checks for intersection with a Bullet... only check the intersecting in one class. You could do it like this:
1
2
3
4
5
6
7
8
9
10
//in Bullet
Seal seal = (Seal) getOneIntersectingObject(Seal.class);
if (seal != null)
{
    if (seal.loseLife(1))
    {
        ((SealWorld) getWorld()).getCounter().addScore(100);
    }
    getWorld().removeObject(this);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
//in Seal
private int health = 3;
 
public boolean loseLife(int amount)
{
    health -= amount;
    if (health < 1)
    {
        getWorld().removeObject(this);
        return true;
    }
    return false;
}
Thanks! This worked, although now I have a new problem. The problem is every time I hit the Seal the game just stops and i have to reload every shot it hits, but on the 3rd shot it dies like its supposed to. (The game pauses everytime I shoot a bullet at the seal)
Super_Hippo Super_Hippo

2017/4/4

#
You should get an error message (NullPointer?). Show to what line it points.
Childiish Childiish

2017/4/5

#
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:711) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:913) at Bullet.act(Bullet.java:52) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) Thats the error I get, Im looking at line 52 and see nothing wrong though.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Bullet here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Bullet extends Mover
{
 
    /**
     * 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(10.0);
 
        /** 
         * If Bullet hits over the Dolphin it will disappear.
         */
        Actor Dolphin;
        Dolphin = getOneObjectAtOffset(0,0, Dolphin.class);  
 
        if (Dolphin != null)
        {
            World myWorld = getWorld();
            myWorld.removeObject(Dolphin);
            Space space = (Space)myWorld;
            Counter counter = space.getCounter();
            counter.addScore(1);
 
        }
        Seal seal = (Seal) getOneIntersectingObject(Seal.class);
        if (seal != null)
        {
    if (seal.loseLife(1))
    {
        ((SealWorld) getWorld()).getCounter().addScore(100);
    }
    getWorld().removeObject(this);
}
        /**
         * Bullet destroyed When hits Wall.
         */
        Actor Bullet;
        Bullet = getOneObjectAtOffset(0,0, Bullet.class);
        if (Bullet != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Bullet);
        }
        if  (atWorldEdge())
        {
            World world;
            world = getWorld();
            world.removeObject(this);
        
    }
}
danpost danpost

2017/4/5

#
Affter line 42, insert the following line:
1
return;
I noticed that your comment at line 45 does not match what the code following it is doing. Instead of bullet-wall collision detection, the code is doing bullet-bullet collision, removing any bullet that this bullet hits.
Childiish Childiish

2017/4/5

#
danpost wrote...
Affter line 42, insert the following line:
1
return;
I noticed that your comment at line 45 does not match what to code following it is doing. Instead of bullet-wall collision detection, the code is checking for bullet-bullet collision, removing any bullet that this bullet hits.
Thanks! Worked like a charm! Last problem is that the seal's hitbox is just as big as a standard seal (pretty small). Is there anyway I can increase this hit box?
Super_Hippo Super_Hippo

2017/4/5

#
Use the 'getOneIntersectingObject' for Dolphin and Bullet instead of the 'getOneObjectAtOffset' method. (Look at how I did it with the intersectiion with the Seal.)
You need to login to post a reply.