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

2016/3/11

Life Counter

GreenMan15 GreenMan15

2016/3/11

#
I wanted to make a life counter for my game. Would it be the same as making a score counter you think but instead changing the text into Lives: instead of Score:
danpost danpost

2016/3/11

#
You could look at my Value Display Tutorial to figure that out.
GreenMan15 GreenMan15

2016/3/12

#
Here is my life counter but sometimes when my plane get hits it doesnt take off a life.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    private int time;
    private int life = 2;
    boolean startMusicc = true;
    GreenfootSound backGrounddMusic = new GreenfootSound("Sound2.mp3");
    LifeCounter count = new LifeCounter();
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 537, 1);
        prepare();
        time = 1000;
        Music();
    }
 
    public void Music()
    {
        
    }
 
    public void act()
    {
        showTheTime();
        if (Greenfoot.getRandomNumber(300) <3 )
        {
            addObject(new Enemy(), Greenfoot.getRandomNumber(100), Greenfoot.getRandomNumber(500));
        }
        if (Greenfoot.getRandomNumber(300) <3)
        {
            addObject(new Rocket(), Greenfoot.getRandomNumber(100), Greenfoot.getRandomNumber(500));
        }
       if (startMusicc)
        {
            backGrounddMusic.playLoop();
            startMusicc= false;
        }
    }
 
    public void showTime()
    {
        showText("Time: " + time, 50, 20);
    }
 
    public void showTheTime()
    {
        time--;
        showTime();
        if(time==0)
        {
            Greenfoot.setWorld(new Level2());
            backGrounddMusic.stop();
        }
 
    }
 
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Man man = new Man();
        addObject(man,702,241);
        Minis mini = new Minis();
        addObject(mini, 887, 424);
        Minis minis2 = new Minis();
        addObject(minis2,884,111);
        addObject(count, 798, 20);
    }
 
    public LifeCounter getCounter()
    {
        return count;
    }
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Man here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Man extends Actor
{
    public Man()
    {
       GreenfootImage myImage = getImage();
       int myNewHeight = (int)myImage.getHeight()/3;
       int myNewWidth = (int)myImage.getWidth()/3;
       myImage.scale(myNewWidth, myNewHeight);  
    }
    public void act()
    {
      checkHit();
      checkKeys();
    }   
     
    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-8);
        }
         
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+8);
        }
         
        if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+4, getY());
        }
         
        if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-4, getY());
        }
        if ("space".equals(Greenfoot.getKey()))
        {
           shoot();
        }
         
    }
    private void shoot()
    {
        Ammo ammo = new Ammo();
        getWorld().addObject(ammo, getX(), getY());
         
     }
    public void checkHit()
    {
      Actor a = this.getOneIntersectingObject(Rocket.class);
      if (a != null)
      {
        this.getWorld().removeObject(a); 
        World myWorld = getWorld();
        MyWorld world = (MyWorld)(myWorld);
        LifeCounter count = world.getCounter();
        count.countLives();
      }
      Actor b= this.getOneIntersectingObject(Enemy.class);
      if (b != null)
      {
        this.getWorld().removeObject(b); 
        World myWorld = getWorld();
        MyWorld world = (MyWorld)(myWorld);
        LifeCounter count = world.getCounter();
        count.countLives();
      
         
    }
}
GreenMan15 GreenMan15

2016/3/12

#
The class Man is an object of a plane but this is a practice code so I didnt bother make a new class. Just FYI
GreenMan15 GreenMan15

2016/3/12

#
Any advice would be appreciated
GreenMan15 GreenMan15

2016/3/12

#
I almost forgot here is my LifeCounter 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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class LifeCounter here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class LifeCounter extends Actor
{
    int lives = 2;
    boolean startMusic = true;
    //GreenfootSound backGrounddMusic = new GreenfootSound("Sound2.mp3");
    public void act()
    {
        setImage(new GreenfootImage("Lives: " + lives, 24, Color.GREEN, Color.BLACK));
        gameOver();
        //Music();
    }   
 
    public void countLives()
    {
        lives--;
    }
danpost danpost

2016/3/12

#
From what I can tell (although you did post the entire LifeCounter class code), your lives should go down any time a Rocket or an Enemy object is encountered.
GreenMan15 GreenMan15

2016/3/12

#
I am confused isnt that what I did? I mean it works perfectly then other times it doesn't take a life a away.
danpost danpost

2016/3/12

#
GreenMan15 wrote...
I am confused isnt that what I did? I mean it works perfectly then other times it doesn't take a life a away.
Maybe you are looking at the different fields that hold the life value. You have one in your World subclass and then you have another one in the LifeCounter class. The one in the LifeCounter class is the one that appears to be coded correctly (it is coded to decrease anytime a Rocket or an Enemy encounter the Man object. The one in the world seems to be just there (nothing is done with it after being declared and assigned its initial value at line 12 of your MyWorld class. As far as not taking lives away, is there anything that is consistent when this happens? If you cannot figure it out, maybe uploading the scenario with published source will allow some help to be provided.
GreenMan15 GreenMan15

2016/3/12

#
Wait I think I just figured it out?? In my rocket class I made it where the man class would disappear if it was touched by it. I wanted the rocket to disappear when touched by the man so I took that line out and it started working. Now everytime I run it, I always lose 1 life.
You need to login to post a reply.