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

2019/6/10

Help Witch This Code

gacha321 gacha321

2019/6/10

#
Can you help my why this code isn't work when I click the button ??
public class Button1 extends Button
{
    /**
     * Act - do whatever the Button1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        if(Greenfoot.mouseClicked(this)){
            Counter counter = ((BeliHewan)getWorld()).getCounter();
            if (counter.getValue() > 100)
            {
                Greenfoot.playSound("ChaChing.mp3");
                counter.walkCount(counter.getValue()-100);
            }
        }
    }    
}
I don't know why this code is can't work when I clicked the button :(
gacha321 gacha321

2019/6/10

#
I tried to change line 14 to line 12 it has sound but the counter didnt - by 100 idk why :(
gacha321 gacha321

2019/6/10

#
We can't use nested if in the act method?
Zamoht Zamoht

2019/6/10

#
Well if you are able to play the sound it must mean that the if statements work fine. Can you show the code for counter.walkCount? Preferably the whole counter code.
danpost danpost

2019/6/10

#
What is the initial value of the counter when the button is clicked?
gacha321 gacha321

2019/6/11

#
well, I've change the code like this..
Animals an = new Animals();
        Counter counter = ((BuyAnimals)getWorld()).getCounter();
        if(jRadioButton1.isSelected() && counter > 700){
            counter.walkCount(-700);
but it show an error : bad operand types for binary operator '>' first type : counter, second type : int do you know where's the problem?? when I tried to change the code like this
Animals an = new Animals();
        Counter counter = ((BuyAnimals)getWorld()).getCounter();
        if(jRadioButton1.isSelected()){
            counter.walkCount(-700);
It works, but when I buy the Animals without the (counter > 700), the gold will be reduce till negative if we don't have a 700 golds :((
gacha321 gacha321

2019/6/11

#
Can you show the code for counter.walkCount? Preferably the whole counter code. well, here's the code
    public void walkCount (int amount) 
    {
        // Add your action code here.
        count += amount;
        setImage(new GreenfootImage("Gold : " + (100+count),20, Color.WHITE, Color.BLACK)); 
    }    
    
    public int getValue(){
        return count;
    }
gacha321 gacha321

2019/6/12

#
HELPPPP T^T
danpost danpost

2019/6/12

#
The error message is saying that you cannot treat a Counter object as a value. You need to get the value of the Counter object and compare that to 700.
gacha321 gacha321

2019/6/12

#
so, should I make a variabel to save the counter value and compare that??
gacha321 gacha321

2019/6/12

#
it show counter can not converted to int :((
danpost danpost

2019/6/12

#
gacha321 wrote...
so, should I make a variabel to save the counter value and compare that??
No need. Just use counter.getValue().
gacha321 gacha321

2019/6/12

#
My bad :((( but, thank you for your helpp it works!
You need to login to post a reply.