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

2020/5/10

Variable not updating

drayko543 drayko543

2020/5/10

#
I'm making a space invaders game and I want to put in a lives counter. I did this with a lives_left class which is a child of the player class. the player class has a method that returns the number of lives left. the problem is when I call the method from the lives_left class it always returns the same value, no matter how many times the player dies. this is the code for the lives_left class
 private int count1;
    private int life1;
    public lives_left(int count) {
        player.scale(player.getWidth()*2,player.getHeight()*2); 
        setImage(player);
        count1 = count;
    }
    public void act() 
    {   
        life1 = life();
        if (life1 < count1){
            getWorld().removeObject(this);
        }
        
    }  
this is the code for the player class
GreenfootImage player = new GreenfootImage("Player.png");
    GreenfootImage destroyed = new GreenfootImage("Player_destroyed_1.png");
    private boolean spaceDown;
    private int delay = 0 ;
    public int lives;
    public Player(){
        player.scale(player.getWidth()*2,player.getHeight()*2);
        destroyed.scale(destroyed.getWidth()*2,destroyed.getHeight()*2);
        setImage(player);
        lives = 3;
    }

    public void act() 
    {   
        collisionDetection();
        //moovement controls
        if(Greenfoot.isKeyDown("right")){
            move(3);
        }
        if(Greenfoot.isKeyDown("left")){
            move(-3);
        }
        //checks if there are less than two bullets if there are it lets you shoot bullets
        if(getWorld().getObjects(playerBullet.class).size()<2){
            if (!spaceDown && Greenfoot.isKeyDown("space")){
                spaceDown = true;
                getWorld().addObject(new playerBullet(), getX(), getY()-20);
            }
            if (spaceDown && !Greenfoot.isKeyDown("space")){
                spaceDown = false;
            }
        }
    }
     private void collisionDetection(){
        Actor a = getOneIntersectingObject(alienBullet.class);//sets any intersecting object to the variable a
        if(a != null){//if there is an object in a then kill the player
            getWorld().removeObject(a);
            death();
            
        }
    }
    private void death(){
        setImage(destroyed);
        getWorld().removeObjects(getWorld().getObjects(alienBullet.class));//removes all alien bullets
        if(lives!=0){//if you have lives left it delays things a bit
            Greenfoot.delay(100);
            lives = lives - 1;
            setImage(player);
        }else{//if you have no lives left then you lose
            setImage(destroyed);
            Greenfoot.delay(100);
            Greenfoot.setWorld(new Game_Over());
        }
        
    }
    public int life(){
        return lives;
    }
danpost danpost

2020/5/10

#
drayko543 wrote...
I'm making a space invaders game and I want to put in a lives counter. I did this with a lives_left class which is a child of the player class.
If you mean lives_left extends player, then that is a problem. An extension of a class is to create instances of the class it extends, just with possible added (or modified) stuff. It could (1) add extra state (new fields); (2) add or modify behavior (new method or method override); or (3) any combination of (1) and (2) (including neither just to give special categorizing).
You need to login to post a reply.