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

2021/11/17

How do i get my variable to add 1 each time i click?

jeppe9756 jeppe9756

2021/11/17

#
Im trying to get my "int antaltryk" to add 1 each time i click the "knap1" class, but it only works once, and then i cant press it anymore. What should i do? The .moneys has to be 10 or greater than, and when it gets to 10 again i cant press it
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Knap1 extends DelayableActor
{
    int antaltryk = 0;
    public void run()
    {
        tryk();
        autoklik();
        autoklik2();
        tekst();
    }
    public void tryk()
    {
        Knap1 knap1 = (Knap1)getWorld().getObjects(Knap1.class).get(0);
        World TurtleWorld;
        TurtleWorld = getWorld();
        TurtleWorld space = (TurtleWorld)TurtleWorld;
        Counter counter = space.getCounter();
        if(counter.moneys>=10 && Greenfoot.mousePressed(knap1))
        {
            antaltryk++;
            counter.minusscore();
        }
    }
    public void autoklik()
    {
        Knap1 knap1 = (Knap1)getWorld().getObjects(Knap1.class).get(0);
        World TurtleWorld;
        TurtleWorld = getWorld();
        TurtleWorld space = (TurtleWorld)TurtleWorld;
        Counter counter = space.getCounter();
        if(antaltryk==1)
            for(;;)
            {
                counter.moneys++;
                setDelayingTime(2000);
                break;
            }
    }
        public void autoklik2()
    {
        Knap1 knap1 = (Knap1)getWorld().getObjects(Knap1.class).get(0);
        World TurtleWorld;
        TurtleWorld = getWorld();
        TurtleWorld space = (TurtleWorld)TurtleWorld;
        Counter counter = space.getCounter();
        if(antaltryk==2)
            for(;;)
            {
                counter.moneys++;
                setDelayingTime(1000);
                break;
            }
    }
danpost danpost

2021/11/17

#
On line 20, instead of mousePressed, use Greenfoot.mouseClicked(this).
jeppe9756 jeppe9756

2021/11/18

#
Doesnt work
danpost danpost

2021/11/18

#
jeppe9756 wrote...
Doesnt work
It is probably the delaying that is making it seem like you cannot click on it again. Please show the DelayableActor class codes.
Gabe1098 Gabe1098

2021/11/18

#
jeppe9756 wrote...
Doesnt work
Greenfoot.mouseClicked(knap1.class) {

}
danpost danpost

2021/11/18

#
Gabe1098 wrote...
Greenfoot.mouseClicked(knap1.class) {

}
Does not help. Invalid parameter; and ";" would be expected before "{".
You need to login to post a reply.