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

2017/1/21

Accessing variable from other class

1
2
danpost danpost

2017/1/22

#
Hondrok wrote...
the health_bar needs to change its image acording to the armour of the Mario actor
It will only change when the value of 'amour' for the Mario object changes. So, after line 140, which changes the value of 'armour' in the Mario class, you can call the 'updateImage' method of the health bar as Super_Hippo suggested. However, there is also line 85, which also changes the value of 'amour'. Again, you will need to add a line after it to update the image of the health bar.
Hondrok Hondrok

2017/1/22

#
danpost wrote...
Hondrok wrote...
the health_bar needs to change its image acording to the armour of the Mario actor
It will only change when the value of 'amour' for the Mario object changes. So, after line 140, which changes the value of 'armour' in the Mario class, you can call the 'updateImage' method of the health bar as Super_Hippo suggested. However, there is also line 85, which also changes the value of 'amour'. Again, you will need to add a line after it to update the image of the health bar.
like this?
import greenfoot.*;
   
public class Mario extends Actor
{
    private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpStrength = 25;
    public int armour = 3;
     
    public void act() 
    {
       if (! getWorld().getObjects(GameOver.class).isEmpty()) 
       {
           return;
       }      
       checkKeys();
       attack();
       checkFall();
       hitGoomba();
       hitCastle_l1();
       hitCastle_l2();
       checkArmour();
    }
    
    private void checkKeys()  
    {
       if(Greenfoot.isKeyDown("left"))
       {
           moveLeft();
       } 
       if(Greenfoot.isKeyDown("right"))
       {
           moveRight();
       }
       if(Greenfoot.isKeyDown("up"))
       {
           jump();
       }
    }    
 
    public void checkFall()
    {
        if(onGround() && vSpeed != -jumpStrength)
        {
           vSpeed=0; 
           if (Greenfoot.isKeyDown("left") == Greenfoot.isKeyDown("right")) 
           {
               setImage("Goku_idle.png");
           }
        }
        else
        {
            setImage("Goku_jump.png");
            fall();
        }
    }
     
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
     
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }   
         
    public void jump()
    {
       if (onGround())
       {
           vSpeed = - jumpStrength;
       }
    }
     
    public void hitGoomba()
    {
        Actor goomba = getOneIntersectingObject(Goomba.class);
        if(goomba != null)
        {
            armour = armour - 1;
            getWorld().removeObject(goomba);
        }
    }
     
    public void hitCastle_l1()
    {
        Actor castle = getOneIntersectingObject(Castle.class);
        if(castle != null)
        {
            World world = new Level_2(); // reference to new world so you can work with it
            Mario mario2 = (Mario)world.getObjects(Mario.class).get(0); // reference to mario in new world
            mario2.armour = this.armour; // set value of armor of new mario to that of the one in current world ( 'this.' is optional)
            Greenfoot.setWorld(world); // set new level active
        }
    }
    
    public void hitCastle_l2()
    {
        Actor castle = getOneIntersectingObject(Castle_l2.class);
        if(castle != null)
        {
            World world = new Final_level(); // reference to new world so you can work with it
            Mario mario3 = (Mario)world.getObjects(Mario.class).get(0); // reference to mario in new world
            mario3.armour = this.armour; // set value of armor of new mario to that of the one in current world ( 'this.' is optional)
            Greenfoot.setWorld(world); // set new level active
        }
    }
     
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
        setImage("Goku_move_right.png");
    }
     
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
        setImage("Goku_move_left.png");
    }
    
    public void attack()
    {
        if("space".equals(Greenfoot.getKey()))
        {
            getWorld().addObject(new Sphere_right(), getX() + 5, getY());
            setImage("Goku_attack.png");
        }
    }

    public void checkArmour()
    {
        Actor sphere = getOneIntersectingObject(Sphere_left.class);
        if(sphere != null)
        {
           armour = armour - 1;
      
           ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
           getWorld().removeObject(sphere);
        }
        
        if(armour == 0)
        {
            getImage().setTransparency(0);
            World myWorld = getWorld();
            GameOver gameover = new GameOver();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
            Greenfoot.stop();
        }
    }
}
danpost danpost

2017/1/22

#
Hondrok wrote...
like this? < Code Omittedd >
You have line 141 in the correct place. But:
danpost wrote...
there is also line 85, which also changes the value of 'amour'. Again, you will need to add a line after it to update the image of the health bar.
Hondrok Hondrok

2017/1/23

#
danpost wrote...
Hondrok wrote...
like this? < Code Omittedd >
You have line 141 in the correct place. But:
danpost wrote...
there is also line 85, which also changes the value of 'amour'. Again, you will need to add a line after it to update the image of the health bar.
I did what you suggested me but it still doesn't work. Even if the value of armour changes the health_bar image doesn't change
import greenfoot.*;
   
public class Mario extends Actor
{
    private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpStrength = 25;
    public int armour = 3;
     
    public void act() 
    {
       if (! getWorld().getObjects(GameOver.class).isEmpty()) 
       {
           return;
       }      
       checkKeys();
       attack();
       checkFall();
       hitGoomba();
       hitCastle_l1();
       hitCastle_l2();
       checkArmour();
    }
    
    private void checkKeys()  
    {
       if(Greenfoot.isKeyDown("left"))
       {
           moveLeft();
       } 
       if(Greenfoot.isKeyDown("right"))
       {
           moveRight();
       }
       if(Greenfoot.isKeyDown("up"))
       {
           jump();
       }
    }    
 
    public void checkFall()
    {
        if(onGround() && vSpeed != -jumpStrength)
        {
           vSpeed=0; 
           if (Greenfoot.isKeyDown("left") == Greenfoot.isKeyDown("right")) 
           {
               setImage("Goku_idle.png");
           }
        }
        else
        {
            setImage("Goku_jump.png");
            fall();
        }
    }
     
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
     
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }   
         
    public void jump()
    {
       if (onGround())
       {
           vSpeed = - jumpStrength;
       }
    }
     
    public void hitGoomba()
    {
        Actor goomba = getOneIntersectingObject(Goomba.class);
        if(goomba != null)
        {
            armour = armour - 1;
            getWorld().removeObject(goomba);
            ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
        }
    }
     
    public void hitCastle_l1()
    {
        Actor castle = getOneIntersectingObject(Castle.class);
        if(castle != null)
        {
            World world = new Level_2(); // reference to new world so you can work with it
            Mario mario2 = (Mario)world.getObjects(Mario.class).get(0); // reference to mario in new world
            mario2.armour = this.armour; // set value of armor of new mario to that of the one in current world ( 'this.' is optional)
            Greenfoot.setWorld(world); // set new level active
        }
    }
    
    public void hitCastle_l2()
    {
        Actor castle = getOneIntersectingObject(Castle_l2.class);
        if(castle != null)
        {
            World world = new Final_level(); // reference to new world so you can work with it
            Mario mario3 = (Mario)world.getObjects(Mario.class).get(0); // reference to mario in new world
            mario3.armour = this.armour; // set value of armor of new mario to that of the one in current world ( 'this.' is optional)
            Greenfoot.setWorld(world); // set new level active
        }
    }
     
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
        setImage("Goku_move_right.png");
    }
     
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
        setImage("Goku_move_left.png");
    }
    
    public void attack()
    {
        if("space".equals(Greenfoot.getKey()))
        {
            getWorld().addObject(new Sphere_right(), getX() + 5, getY());
            setImage("Goku_attack.png");
        }
    }

    public void checkArmour()
    {
        Actor sphere = getOneIntersectingObject(Sphere_left.class);
        if(sphere != null)
        {
           armour = armour - 1;
      
           ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
           getWorld().removeObject(sphere);
        }
        
        if(armour == 0)
        {
            getImage().setTransparency(0);
            World myWorld = getWorld();
            GameOver gameover = new GameOver();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
            Greenfoot.stop();
        }
    }
}
Super_Hippo Super_Hippo

2017/1/23

#
Okay, what do you have in the Health_Bar class now and how many Health_Bar objects are in the world?
Hondrok Hondrok

2017/1/23

#
Super_Hippo wrote...
Okay, what do you have in the Health_Bar class now and how many Health_Bar objects are in the world?
oh dummy me now it works thank you a looot and thank you too dan post
Hondrok Hondrok

2017/1/23

#
Super_Hippo wrote...
Okay, what do you have in the Health_Bar class now and how many Health_Bar objects are in the world?
danpost wrote...
Hondrok wrote...
like this? < Code Omittedd >
You have line 141 in the correct place. But:
danpost wrote...
there is also line 85, which also changes the value of 'amour'. Again, you will need to add a line after it to update the image of the health bar.
another probleem whenever I succeed to another world the image of the Health_Bar remains the same as the default one (Health_bar_3) until the armour changes then the image changes to the specific armour health bar code
import greenfoot.*;

public class Health_Bar extends Actor
{  
    
    public void act() 
    {
        
    }    
    
    public void updateImage(int armour)
    {
        setImage("Health_bar_"+armour+".png");
    }
}
mario code
import greenfoot.*;
   
public class Mario extends Actor
{
    private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpStrength = 25;
    public int armour = 3;
     
    public void act() 
    {
       if (! getWorld().getObjects(GameOver.class).isEmpty()) 
       {
           return;
       }      
       checkKeys();
       attack();
       checkFall();
       hitGoomba();
       hitCastle_l1();
       hitCastle_l2();
       checkArmour();
    }
    
    private void checkKeys()  
    {
       if(Greenfoot.isKeyDown("left"))
       {
           moveLeft();
       } 
       if(Greenfoot.isKeyDown("right"))
       {
           moveRight();
       }
       if(Greenfoot.isKeyDown("up"))
       {
           jump();
       }
    }    
 
    public void checkFall()
    {
        if(onGround() && vSpeed != -jumpStrength)
        {
           vSpeed=0; 
           if (Greenfoot.isKeyDown("left") == Greenfoot.isKeyDown("right")) 
           {
               setImage("Goku_idle.png");
           }
        }
        else
        {
            setImage("Goku_jump.png");
            fall();
        }
    }
     
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
     
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }   
         
    public void jump()
    {
       if (onGround())
       {
           vSpeed = - jumpStrength;
       }
    }
     
    public void hitGoomba()
    {
        Actor goomba = getOneIntersectingObject(Goomba.class);
        if(goomba != null)
        {
            armour = armour - 1;
            getWorld().removeObject(goomba);
            ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
        }
    }
     
    public void hitCastle_l1()
    {
        Actor castle = getOneIntersectingObject(Castle.class);
        if(castle != null)
        {
            World world = new Level_2(); // reference to new world so you can work with it
            Mario mario2 = (Mario)world.getObjects(Mario.class).get(0); // reference to mario in new world
            mario2.armour = this.armour; // set value of armor of new mario to that of the one in current world ( 'this.' is optional)
            Greenfoot.setWorld(world); // set new level active
        }
    }
    
    public void hitCastle_l2()
    {
        Actor castle = getOneIntersectingObject(Castle_l2.class);
        if(castle != null)
        {
            World world = new Final_level(); // reference to new world so you can work with it
            Mario mario3 = (Mario)world.getObjects(Mario.class).get(0); // reference to mario in new world
            mario3.armour = this.armour; // set value of armor of new mario to that of the one in current world ( 'this.' is optional)
            Greenfoot.setWorld(world); // set new level active
        }
    }
     
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
        setImage("Goku_move_right.png");
    }
     
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
        setImage("Goku_move_left.png");
    }
    
    public void attack()
    {
        if("space".equals(Greenfoot.getKey()))
        {
            getWorld().addObject(new Sphere_right(), getX() + 5, getY());
            setImage("Goku_attack.png");
        }
    }

    public void checkArmour()
    {
        Actor sphere = getOneIntersectingObject(Sphere_left.class);
        if(sphere != null)
        {
           armour = armour - 1;
      
           ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
           getWorld().removeObject(sphere);
        }
        
        if(armour == 0)
        {
            getImage().setTransparency(0);
            World myWorld = getWorld();
            GameOver gameover = new GameOver();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
            Greenfoot.stop();
        }
    }
}
danpost danpost

2017/1/23

#
You will need to insert the following line at line 98:
((Health_Bar)world.getObjects(Health_Bar.class).get(0)).updateImage(armour);
and something similar at line 110.
Hondrok Hondrok

2017/1/24

#
danpost wrote...
You will need to insert the following line at line 98:
((Health_Bar)world.getObjects(Health_Bar.class).get(0)).updateImage(armour);
and something similar at line 110.
it still doesn't work
import greenfoot.*;
   
public class Mario extends Actor
{
    private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpStrength = 25;
    public int armour = 3;
     
    public void act() 
    {
       if (! getWorld().getObjects(GameOver.class).isEmpty()) 
       {
           return;
       }      
       checkKeys();
       attack();
       checkFall();
       hitGoomba();
       hitCastle_l1();
       hitCastle_l2();
       checkArmour();
    }
    
    private void checkKeys()  
    {
       if(Greenfoot.isKeyDown("left"))
       {
           moveLeft();
       } 
       if(Greenfoot.isKeyDown("right"))
       {
           moveRight();
       }
       if(Greenfoot.isKeyDown("up"))
       {
           jump();
       }
    }    
 
    public void checkFall()
    {
        if(onGround() && vSpeed != -jumpStrength)
        {
           vSpeed=0; 
           if (Greenfoot.isKeyDown("left") == Greenfoot.isKeyDown("right")) 
           {
               setImage("Goku_idle.png");
           }
        }
        else
        {
            setImage("Goku_jump.png");
            fall();
        }
    }
     
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
     
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }   
         
    public void jump()
    {
       if (onGround())
       {
           vSpeed = - jumpStrength;
       }
    }
     
    public void hitGoomba()
    {
        Actor goomba = getOneIntersectingObject(Goomba.class);
        if(goomba != null)
        {
            armour = armour - 1;
            getWorld().removeObject(goomba);
            ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
        }
    }
     
    public void hitCastle_l1()
    {
        Actor castle = getOneIntersectingObject(Castle.class);
        if(castle != null)
        {
            World world = new Level_2();
            Mario mario2 = (Mario)world.getObjects(Mario.class).get(0);
            mario2.armour = this.armour;
            ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
            Greenfoot.setWorld(world);
        }
    }
    
    public void hitCastle_l2()
    {
        Actor castle = getOneIntersectingObject(Castle_l2.class);
        if(castle != null)
        {
            World world = new Final_level();
            Mario mario3 = (Mario)world.getObjects(Mario.class).get(0);
            mario3.armour = this.armour; 
            ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
            Greenfoot.setWorld(world);
        }
    }
     
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
        setImage("Goku_move_right.png");
    }
     
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
        setImage("Goku_move_left.png");
    }
    
    public void attack()
    {
        if("space".equals(Greenfoot.getKey()))
        {
            getWorld().addObject(new Sphere_right(), getX() + 5, getY());
        }
    }

    public void checkArmour()
    {
        Actor sphere = getOneIntersectingObject(Sphere_left.class);
        if(sphere != null)
        {
           armour = armour - 1;
           ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
           getWorld().removeObject(sphere);
        }
        
        if(armour == 0)
        {
            getImage().setTransparency(0);
            World world = new Game_Over_Lose();
            Greenfoot.setWorld(world);
        }
    }
}
danpost danpost

2017/1/24

#
You did not copy lines 98 and 111 correctly.
Hondrok Hondrok

2017/1/25

#
danpost wrote...
You did not copy lines 98 and 111 correctly.
it works thank youu
You need to login to post a reply.
1
2