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

2015/12/8

Getting and Setting a variable- help

carrotcarrot carrotcarrot

2015/12/8

#
I'm a beginner in coding, so sorry if I'm missing something obvious. When Jett hits a fuel cell, she gains energy. When energy is at a certain point, I want the Cell Bar to switch image. So I want Jett to send the int energy to GaugeBar. I'm a little lost at this - how do I get this to work? Here's what I have: (Please ignore the comments, they're all Act as of now)
public class Jett extends Actor
{
    public int energy;

    /**
     * Act - do whatever the Jett wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Jett() 
    {
        // Add your action code here.
    }    

    /**
     * Act - do whatever the Jett wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(1);
        energyUp();
    }    

    /**
     * Act - do whatever the Jett wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void energyUp() 
    {
        if (isTouching(Fuellcell.class))
        {
            removeTouching(Fuellcell.class);
            energy = energy + 1;
            
        }
    }    
    
    public int getEnergy()
    {
        return energy;
    }
    
    public void setEnergy(int energy)
    {
        energy = energy;
    }
And here's GaugeBar:
public class GaugeBar extends CG
{
    public int energy;

    /**
     * Act - do whatever the GaugeBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public GaugeBar() 
    {

    }    

    /**
     * Act - do whatever the GaugeBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        energyCollector();

    }    

    Actor Jett;
    Jett jettObj = (Jett) getOneObjectAtOffset(0, 0, Jett.class);
    /**
     * Act - do whatever the GaugeBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void energyCollector() 
    {
        // to get
        int energy = jettObj.getEnergy();
        // to set
        jettObj.setEnergy(energy);

    }  
}
Thank you, in advance
Super_Hippo Super_Hippo

2015/12/8

#
At line 34 in the first code, you could add this:
if (energy == 10) //change 10 to the "certain point"
{
    GaugeBar gb = (GaugeBar) getWorld().getObjects(GaugeBar.class).get(0);
    gb.changeImage();
}
//in GaugeBar
public void changeImage()
{
    setImage("otherImage.png");
}
carrotcarrot carrotcarrot

2015/12/8

#
Thank you!
You need to login to post a reply.