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

#
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:
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:
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.
final LifeCounter lc = ((MyWorld)getWorld()).getLifeCounter();
You need to login to post a reply.