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

2021/4/3

Calling a Bar method from another actor class

hernry2812 hernry2812

2021/4/3

#
I am using danposts Bar class, but for some reason it says
non-static method setValue(int) cannot be referenced from a static context
This is my actor class:
public void lifes()
    {
        checkIfTouched();   
        if (touchedMole == 1)
        {
            lifes = 2;     
            Bar.setValue(2); //Thats the striking line
        }
        if (touchedMole == 2)
        {
            lifes = 1;     
        }
        if (touchedMole == 3)
        {
            setLocation(600 - ((SWorld)getWorld()).getScrolledX() - 560, 285 -((SWorld)getWorld()).getScrolledY()-360);
            setRotation(0);
            speed = 0;
            boost = 0;
            touchedMole = 0;
        }
    }
No single variable of the method or the method itself is static, why am I getting this "error" message?
danpost danpost

2021/4/3

#
Bar is a class name, not an Actor (or Bar) object. You cannot set a value on the class. You need to reference an instance of the Bar class (a Bar object) to set a value to. If only one Bar object is in your world, then you could use:
Bar bar = (Bar)getWorld().getObjects(Bar.class).get(0);
bar.setValue(lifes);
hernry2812 hernry2812

2021/4/4

#
Okay thanks, works. By the way: I want my Health Bar to be Green at 3 lifes, yellow or orange at 2 lifes and red at 1 life. I already have this on true:
private boolean breakLow = true;
This is my bar call:
public Bar bar = new Bar("", "", 0, 3);
What do I have to do to achieve what I want?
danpost danpost

2021/4/4

#
hernry2812 wrote...
I want my Health Bar to be Green at 3 lifes, yellow or orange at 2 lifes and red at 1 life.
Use the setSafeColor(Color) instance method of the Bar class.
TechNick TechNick

2021/4/7

#
danpost wrote...
Bar is a class name, not an Actor (or Bar) object. You cannot set a value on the class. You need to reference an instance of the Bar class (a Bar object) to set a value to. If only one Bar object is in your world, then you could use:
Bar bar = (Bar)getWorld().getObjects(Bar.class).get(0);
bar.setValue(lifes);
And what to do if there are multiple bars in my world? I have a bar called healthBar and a bar called boostBar. I would like to use the setSafeColor(Color) method to change my bars colors individually.
danpost danpost

2021/4/8

#
TechNick wrote...
And what to do if there are multiple bars in my world? I have a bar called healthBar and a bar called boostBar. I would like to use the setSafeColor(Color) method to change my bars colors individually.
Then you will need to keep references to them (which I think you already have). Try:
((MyWorld)getWorld()).bar.setValue(lifes);
The world type may need corrected.
TechNick TechNick

2021/4/8

#
This is my world:
public class Game extends SWorld  {      

	public Bar healthBar = new Bar("", "", 0, 3);         
	public Bar boostBar = new Bar("", "", 0, 100);      

	public Game()     
	{             
	super(800, 800, 1, 1920, 1440);        
	prepare();                                 
	}     
	public void prepare()     
	{       if (StartButton.mapChosen == 1)         
		{             
			setBackground(new GreenfootImage("map.png"));              
			addObject(healthBar, 151, 51, false);             
			addObject(boostBar, 649, 51, false);         
		}     
	} 
}
I've got 2 variables in my main actors class, lifes and boost. I would like to let the healthBar display my main actors lifes (Max = 3) and the other one the boost variable (max = 100). These variables are both static ints of my main actors class Car. Could you help me to achieve this? I don't really understand what you said above.
danpost danpost

2021/4/8

#
TechNick wrote...
This is my world: << Code Omitted >>
Okay, then:
((Game)getWorld()).healthBar.setValue(lifes);
I've got 2 variables in my main actors class, lifes and boost. I would like to let the healthBar display my main actors lifes (Max = 3) and the other one the boost variable (max = 100). These variables are both static ints of my main actors class Car.
These variables really should not be in your Car class as each Bar object you create keeps track of its own value. So, what you really want is something like this:
if (isTouching(Mole.class)) loseLife();
private void loseLife()
{
    Bar bar = ((Game)getWorld()).healthBar;
    bar.subtract(1);
    Color[] colors = { Color.RED, Color.RED, Color.YELLOW };
    bar.setSafeColor(colors[bar.getValue()]);
    if (bar.getValue() == 0) resetGame();
}

private void resetGame()
{
    //
}
(I am only concerning myself with the bar code here)
hernry2812 hernry2812

2021/4/8

#
Those methods are now in our main actor car:
    public void checkIfTouchedMole()
    {
        if (touchingMole != isTouching(Mole.class))     
        {
            touchingMole = !touchingMole; 
            if (touchingMole)
            {
                loseLife();
            }
        }
    }

    public void loseLife()
    {
        Bar bar = ((Game)getWorld()).healthBar;
        bar.subtract(1);
        Color[] colors = { Color.GREEN, Color.YELLOW, Color.RED };
        bar.setSafeColor(colors[bar.getValue()]);
        if (bar.getValue() == 0)
        {
            setToStartLocation(); //Teleport to start location
            bar.add(3); //Lifes should be reset to 3
            warnCounter = 1;
            speed = 0;
            boost = 0;
        }
    }
The method checkIfTouchedMole() is in act. You can see the bar call in our post from above (maybe something is wrong with it). I set refText and unitType to "" because we don't want numbers or text to be shown (we've got images for that). But for some reason the value of the the bar stays at 3 and only changes the color once to red after the car hit a mole? Hope you can help
danpost danpost

2021/4/8

#
You color list is not as I have given above. Should be red, red and yellow (for 0, 1, and 2, respectively). And if it changes color at all and the size of the bar does not change, I can only presume you made unauthorized changes to the Bar class.
You need to login to post a reply.