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

2021/11/11

How do i loop?

jeppe9756 jeppe9756

2021/11/11

#
Why can't i get my code to loop? What should i use for the loop?
    public void antaltrykknap1()
    {
        Knap1 knap1 = (Knap1)getWorld().getObjects(Knap1.class).get(0);
        World TurtleWorld;
        TurtleWorld = getWorld();
        TurtleWorld space = (TurtleWorld)TurtleWorld;
        Counter counter = space.getCounter();
        int antaltryk1 = 0;
        if (Greenfoot.mousePressed(knap1))
        {
            antaltryk1++;
        }
        if (Greenfoot.mousePressed(knap1) && counter.moneys>=10)//&& antaltryk1>=1)
        {
            //if (counter.moneys>=10)
                for(;;)
                {
            
                    //while (antaltryk1 >= 1)
                    //{
                    counter.moneys++;
                    Greenfoot.delay(1);
                    break;
                    //}
            }
        }
    }
danpost danpost

2021/11/11

#
jeppe9756 wrote...
Why can't i get my code to loop? What should i use for the loop?
How many times do you want the loop to execute and what exactly are you trying to accomplish within the loop?
jeppe9756 jeppe9756

2021/11/15

#
I want it to loop indefinitely, and im trying to get my counter to count by itself. Its for an "idle tapper" game im trying to make.
jeppe9756 jeppe9756

2021/11/15

#
I figured out how to loop the thing, but i want it to run slower inside my for loop. I tried using .delay(), but that seems to slow down everything else, which i dont want. Is there a way to make the loop only execute half the time or something?
danpost danpost

2021/11/15

#
jeppe9756 wrote...
I want it to loop indefinitely
The act method produces your initial looping by itself.
im trying to get my counter to count by itself. Its for an "idle tapper" game im trying to make.
What do you mean "count by itself"? Should not the counter increment once each time the actor is "tapped"?
danpost danpost

2021/11/15

#
I think you want this:
public void antaltrykknap1()
{
    Knap1 knap1 = (Knap1)getWorld().getObjects(Knap1.class).get(0);
    Counter counter = ((TurtleWorld)getWorld()).getCounter();
    if (Greenfoot.mousePressed(knap1) && counter.moneys >= 10))
    {
        counter.moneys++;
    }
}
I am just wondering how the counter.moneys value is going to get to 10 to begin with.
You need to login to post a reply.