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

2022/4/8

getLifeCounter undeclared even though I have a actor called LifeCounter

200263 200263

2022/4/8

#
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
163
164
165
166
167
168
169
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Player here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int speed;
    private int score;
    double dy=0;
    double g = 1.3;
    double boost = -5;
    public Player(){
        intro = true;
        Gun = new Gun(this); //new gun to shoot
        speed = 5; //speed of character
         
        score = 0; //starting score
        multi = 1; //score multiplyer
    }
    public void act()
    {
        checkKeys();
        checkOutOfBounds();
         
        if (!intro){
        checkDamage(); //check if hit by enemy (burger)
        checkPickup(); //whether picked up banana
        checkDead(); //whether life = 0
        }
    }
    public void checkKeys(){
        if(Greenfoot.isKeyDown("up")){
            dy=boost;
        }
        if(Greenfoot.isKeyDown("space")){
            Gun.shoot();
        }
    }
    public int getSpeed(){
        return speed;
    }
    public int getScore(){
        return score;
    }
    public void displayGameOver(){
        GameOver gameOver = new GameOver(); //Creating new variable called gameover, add it to the world. 
        getWorld().addObject(gameOver, getWorld().getWidth()/2, getWorld().getHeight()/2); //Put it in the center
        Greenfoot.stop();               
 
    }
    public void checkOutOfBounds(){
        int width = getWorld().getWidth();
        int x = getX();
         
         
        if(x > width)
            setLocation(width, y);
        if(x < 0)
            setLocation(0, y);
    }
    //checks whether player is hit by burger
    public void checkDamaged()
    {
        Enemy e  = (Enemy)  getOneIntersectingObject(Enemy.class);
        LifeCounter lc   = getLifeCounter();
         
        // try to find an intersecting Bullet or Enemy object
        if ( (e != null )) //if a burger is intersecting
        {
            if(e != null) getWorld().removeObject(e);
             
            lc.loseLife(); //lose a life...
                        
        }
    }
    public void addSpeed()
    {
        speed++;
    }
         
    public void addAttack()
    {
        attack++;
    }
     
    public void addScore(int s)
    {
        score += (s * multi);
    }
    public void checkPickup()
    
        Banana n =(Health) getOneIntersectingObject(Banana.class);
         
        if(n != null)
        {
            healthup = n.getType();
             
            if(n!=null) getWorld().removeObject(n);
             
            addScore(50);
             
            setHealth();
        }
    }
     
     public void setPower()
    {
  
        if     (healthup == 0){
            //BACK TO NORMAL
            setDefault();
            System.out.println("RESET!");
        }
        //SUPPORT
        else if(healthup == 1){
            //FULL HEALTH
            LifeCounter lc = getLifeCounter();
            lc.setLives(lc.getMaxLives());
        }
        else if(healthup == 2){
            //EXTRA LIFE
            LifeCounter lc = getLifeCounter();
            lc.addLife();
        }
        else if(healthup == 3){
            //FULL HEALTH
            LifeCounter lc = getLifeCounter();
            lc.addLife();
        }
        else if(healthup == 4){
            //EXTRA LIFE
            LifeCounter lc = getLifeCounter();
            lc.addLife();
        }
         
    }
    public void setDefault()
    {
        Gun2 = new Gun(this);
         
        healthUp     = 0;
         
        multi   = 1;
    }
    public void checkDead()
    {
        LifeCounter lc = getLifeCounter();
         
        if(lc.getLives() <= 0)
        {
            //If lives = 0, display game over
             
             
            GameOver ga = new GameOver();
            getWorld().addObject(ga, 250, 250);
             
            getWorld().removeObject(this);
        }
    }
         
}
^^ Is the code where the error is and this is the code for LifeCounter:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class LifeCounter here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class LifeCounter extends Actor
{
    /**
     * Act - do whatever the LifeCounter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int lives, maxLives;
     
    public void act()
    {
        // Add your action code here.
    }
    pubic LifeCounter(int v){
        lives = v;
        maxLives = v;
         
     
         
    }
    public int getLives(){
        return lives;
    }
    public void loseLife(){
        if(lives>0){lives--;}
     
    }
     
    public void addLife(){
        if(lives < maxLives){lives++;}
     
    }
    public void setLives(int x){
        if(x <= maxLives){
            lives = x;
        }
        else{
            lives = maxLives;
        }
     
    }
    public int getMaxLives()
    {
        return maxLives;
    }
}
Super_Hippo Super_Hippo

2022/4/8

#
There is no method called “getLifeCounter”, so you can’t use the method.
200263 200263

2022/4/9

#
oh okay thanks
Super_Hippo wrote...
There is no method called “getLifeCounter”, so you can’t use the method.
200263 200263

2022/4/15

#
Super_Hippo wrote...
There is no method called “getLifeCounter”, so you can’t use the method.
I just realized I put the getLifeCounter as a public variable inside the main world code. here:
1
2
3
4
public LifeCounter getLifeCounter()
    {
        return lf;
    }
Spock47 Spock47

2022/4/16

#
Well, if the getLifeCounter method is in the MyWorld class, you need to call it on a MyWorld object, e.g.
1
final LifeCounter lc = ((MyWorld)getWorld()).getLifeCounter();
You need to login to post a reply.