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

2013/8/28

Help Please

1
2
3
4
mattjames mattjames

2013/9/2

#
danpost - that will work well but what I need now if how will I be able to read that in my car class, i.e. how do I write the code which will can see what value the variable choice is at?
mattjames mattjames

2013/9/2

#
Hi guys again. Been working on the code and have a counter and a method in it called bumpcount as shown below
public void bumpCount(int amount)
    {
        totalCount += amount;
        setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLACK));
    }
I was wondering if there was a bit of code which would make it so that method would set the counter to zero.
SPower SPower

2013/9/2

#
Do you mean this?:
totalCount = 0;
setImage(new GreenfootImage(""+totalCount,20,Color.WHITE,Color.BLACK));
mattjames mattjames

2013/9/2

#
Can you check this to see if it would work. When clicked I want it to set the counter (called counter3) to zero. In the button actor:
if (Greenfoot.mouseClicked(this))
        {
            
        world worldWorld = (world) getWorld();
        Counter3 counter3 = worldWorld.getCounter3();
        counter3.setCount(0);
        
        

    }
and in the counter class public void setCount(int amount) { totalCount = 0; setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLACK)); }
danpost danpost

2013/9/2

#
nvm, do not see any problems with the code of your last post.
SPower SPower

2013/9/2

#
Well, to set it to 0 you should use the code you just gave, but change setCount to this again:
public void setCount(int amount)
{
    totalCount = amount;
    setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLACK));
}
mattjames mattjames

2013/9/2

#
Okay will that always set it to zero when I click on it in the actor above?
danpost danpost

2013/9/2

#
Your 'counter3.setCount(0);' tells it to set it to zero.
mattjames mattjames

2013/9/2

#
Excellent thanks guys
mattjames mattjames

2013/9/2

#
Sorry I was wondering what is the code that will read the counter and then store it in an int variable, named choice?
SPower SPower

2013/9/2

#
Well, if you're using the same code for the Counter class as shown in your 'Counter help' discussion, add the method I showed you there, and simply do this:
int choise = counter3.getValue();
where counter3 is the Counter object you need the value of (I guess that's the name, considering the previous code you showed)
mattjames mattjames

2013/9/2

#
Okay I presume I need the world worldWorld = (world) getWorld(); Counter3 counter3 = worldWorld.getCounter3(); before it?
davmac davmac

2013/9/2

#
I think the answer to your question above is 'yes' but - - you've called your world class 'world'? so 'world extends World'? I think you're asking for trouble. Convention in Java is to begin class names with a capital letter. Use 'MyWorld' or similar.
mattjames mattjames

2013/9/2

#
If it is like that won't it work?
SPower SPower

2013/9/2

#
No, it will work, but it's confusing and a bit, if I may say so, messy.
There are more replies on the next page.
1
2
3
4