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
Hondrok Hondrok

2017/1/21

#
I want to access the variable armour from the Mario class and I don't know how to do it
import greenfoot.*;

public class Health_Bar extends Actor
{
    public void act() 
    {
        checkHealth();
    }    
    
    public void checkHealth()
    {
        if(armour == 3)
        {
            setImage("Health_bar_3.png");
        }
        
        if(armour == 2)
        {
            setImage("Health_bar_2.png");
        }
        
        if(armour == 1)
        {
            setImage("Health_bar_1.png");
        }
        
        if(armour == 0)
        {
            setImage("Health_bar_0.png");
        }
    }
}
danpost danpost

2017/1/21

#
Hondrok wrote...
I want to access the variable armour from the Mario class
It is done pretty much the same way as you accessed it within the new world when changing worlds.
Hondrok Hondrok

2017/1/21

#
danpost wrote...
Hondrok wrote...
I want to access the variable armour from the Mario class
It is done pretty much the same way as you accessed it within the new world when changing worlds.
It doesn't work
import greenfoot.*;

public class Health_Bar extends Actor
{  
    public void act() 
    {
        checkHealth();
        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)
    }    
    
    public void checkHealth()
    {
        if(armour == 3)
        {
            setImage("Health_bar_3.png");
        }
        
        if(armour == 2)
        {
            setImage("Health_bar_2.png");
        }
        
        if(armour == 1)
        {
            setImage("Health_bar_1.png");
        }
        
        if(armour == 0)
        {
            setImage("Health_bar_0.png");
        }
    }
}
Super_Hippo Super_Hippo

2017/1/21

#
You create a new world in line 8 and then get a reference to the new Mario in this new world. Instead, you want a reference to the Mario in the current world. Try to use the 'getWorld' method to get the current world and then, get the Mario from that world. Line also sets the Mario armour variable to the armour variable of the Health_Bar object. One thing is, there is no armour variable in the Health_Bar class (you use it in the 'checkHealth' method, but it isn't created anywhere). The other thing is that you probably want to set the armour used in the Health_Bar to the armour field in this Health_bar (so the other way around). However, I would suggest you remove all code from that class (line 5-34) and add the following methods:
public Health_Bar()
{
    updateImage(3); //if you start with 3 armour
}

public void updateImage(int armour)
{
    setImage("Health_bar_"+armour+".png");
}
Whenever the armour variable in the Mario class changes, then you need to get a reference to the Health_Bar object (or you just save a reference in the Mario class), call the 'updateImage' method on the Health_Bar object and pass the new armour value.
Hondrok Hondrok

2017/1/22

#
danpost wrote...
Hondrok wrote...
I want to access the variable armour from the Mario class
It is done pretty much the same way as you accessed it within the new world when changing worlds.
Super_Hippo wrote...
You create a new world in line 8 and then get a reference to the new Mario in this new world. Instead, you want a reference to the Mario in the current world. Try to use the 'getWorld' method to get the current world and then, get the Mario from that world. Line also sets the Mario armour variable to the armour variable of the Health_Bar object. One thing is, there is no armour variable in the Health_Bar class (you use it in the 'checkHealth' method, but it isn't created anywhere). The other thing is that you probably want to set the armour used in the Health_Bar to the armour field in this Health_bar (so the other way around). However, I would suggest you remove all code from that class (line 5-34) and add the following methods:
public Health_Bar()
{
    updateImage(3); //if you start with 3 armour
}

public void updateImage(int armour)
{
    setImage("Health_bar_"+armour+".png");
}
Whenever the armour variable in the Mario class changes, then you need to get a reference to the Health_Bar object (or you just save a reference in the Mario class), call the 'updateImage' method on the Health_Bar object and pass the new armour value.
It still doesn't work
import greenfoot.*;

public class Health_Bar extends Actor
{  
    public void act() 
    {
        getWorld();
        World world = new Level_2();
        Mario mario2 = (Mario)world.getObjects(Mario.class).get(0);
        mario2.armour = this.armour;
        check_armour();
    }    
    
    public void updateImage(int armour)
    {
        setImage("Health_bar_"+armour+".png");
    }
    
    public void check_armour()
    {
        if(armour==3)
        {
            updateImage(3);
        }
        if(armour==2)
        {
            updateImage(2);
        }
        if(armour==1)
        {
            updateImage(1);
        }
        if(armour==0)
        {
            updateImage(0);
        }
    }
}
Super_Hippo Super_Hippo

2017/1/22

#
I think you didn't understand what I tried to tell you. Remove the act method and remove the check_armour method. Control the Health_Bar object from the Mario object.
Hondrok Hondrok

2017/1/22

#
Super_Hippo wrote...
I think you didn't understand what I tried to tell you. Remove the act method and remove the check_armour method. Control the Health_Bar object from the Mario object.
it still doesn't work line 154
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();
       checkArmour_hb();
    }
    
    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;
            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();
        }
    }
    
    public void updateImage(int armour)
    {
        Health_Bar health_bar = new Health_Bar();
        setImage("Health_bar_"+armour+".png");
    }
    
    public void checkArmour_hb()
    {
        if(armour==3)
        {
            updateImage(3);
        }
        if(armour==2)
        {
            updateImage(2);
        }
        if(armour==1)
        {
            updateImage(1);
        }
        if(armour==0)
        {
            updateImage(0);
        }
    }
}
Super_Hippo Super_Hippo

2017/1/22

#
Remove the 'checkArmour_hb' method and move the 'updateImage' method back to the 'Health_Bar' class. Change the 'checkArmour' method to this:
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();
        }
    }
}
Hondrok Hondrok

2017/1/22

#
Super_Hippo wrote...
Remove the 'checkArmour_hb' method and move the 'updateImage' method back to the 'Health_Bar' class. Change the 'checkArmour' method to this:
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();
        }
    }
}
got that but what do I add in the act method of the health bar?
import greenfoot.*;

public class Health_Bar extends Actor
{  
    public void act() 
    {
        
    }    
    
    public void updateImage(int armour)
    {
        setImage("Health_bar_"+armour+".png");
    }
}
Super_Hippo Super_Hippo

2017/1/22

#
Nothing
Hondrok Hondrok

2017/1/22

#
Super_Hippo wrote...
Nothing
if I let it like this
import greenfoot.*;
 
public class Health_Bar extends Actor
{  
    public void act() 
    {
         
    }    
     
    public void updateImage(int armour)
    {
        setImage("Health_bar_"+armour+".png");
    }
}
it won't do anything
danpost danpost

2017/1/22

#
Hondrok wrote...
if I let it like this < Code Omitted > it won't do anything
What does the healthbar object need to continuously do?
Super_Hippo Super_Hippo

2017/1/22

#
((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
This line is controlling the Health_Bar. The Health_Bar doesn't need to do anything. You only have to change the image when Mario's armour is changing and that is happening with this line.
Hondrok Hondrok

2017/1/22

#
Super_Hippo wrote...
((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
This line is controlling the Health_Bar. The Health_Bar doesn't need to do anything. You only have to change the image when Mario's armour is changing and that is happening with this line.
is not working
Hondrok Hondrok

2017/1/22

#
danpost wrote...
Hondrok wrote...
if I let it like this < Code Omitted > it won't do anything
What does the healthbar object need to continuously do?
the health_bar needs to change its image acording to the armour of the Mario actor
There are more replies on the next page.
1
2