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

2022/5/25

My for clause doesn't work

Turbo_Thorsten Turbo_Thorsten

2022/5/25

#
I need to have a for clause in my programm but it doesn'T work (not in world nor actors). The method where the for clause is written is called but only the for clause doesn't work. What is wrong?
for (int e = 0;e == 250;e++)
          {
           getImage().scale(250, getImage().getHeight()-4);
           setLocation(getX(), getY()+2);
           getWorld().showText("test", 100,100);
        }
RcCookie RcCookie

2022/5/25

#
Turbo_Thorsten wrote...
for (int e = 0;e == 250;e++)
As a reminder, a for loop works as follows:
for(<expression at the start of the loop>; <condition for the loop to run again>; <expression to run each iteration>)
I think with "e == 250" you wanted to specify the break condition. It actually has to be the opposite though:
for(int e=0; e<250; e++)
Thus you for-loop never runs.
RcCookie RcCookie

2022/5/25

#
However, I don't think that what you are trying to archive with the loop will work. All the iterations will run in a single frame, and you will only see the result after all iterations did run. The world only gets updated after "act" was called on all objects once.
danpost danpost

2022/5/25

#
Also, line 3, within a for loop, is troublesome because you a scaling the same image multiple times, which never works well. Best is to save the original image in an instance field, and on each iteration of the loop, create a copy of that image and then scale the copy once.
You need to login to post a reply.