INTRODUCTION TO FOR LOOPS
Now, we look at loops because loops are one of the easiest ways to automate tasks in programming and game development.
It is important. It is called the ‘For loops test’
We can make the green ‘Sprite’ and change the size to 32 by 32.
Everything’s working fine here.
Take a look at the ‘For’ loop.
We go to ‘System’. There are different types of ‘For’ loops like ‘For Each (ordered)’ and ‘For Each’. We’ll talk about all this, but the most important is the ‘For’ loop. Let me explain what the ‘For’ loop does.
A ‘For’ loop is a loop with a certain amount of action.
Therefore, if you need to do a lot of repetition ‘For’ loop is helpful. The example that I want to use is if you have to wash the dishes and every one of these actions you have to write down. You take a plate, wet it, rinse it, and dry it. Then you put it on the drying rack. That’s four steps. Let’s say you have 100 dishes and don’t use a ‘For’ loop, you will have to write out those four steps 100 times, and it’s not reasonable for you to do so. You can repeat that instead. We’ll call it ‘Sprite spawn’
Now we will have index numbers from 1 to 10. In most ‘For’ loops, you start counting based on zero, but in ‘Structure 3’, you start at 1. In this case, it will generate these patterns ten times. You’ll see that it comes up with this little icon here.
That’s a loop, and it is important. It’s a different color, so your eyes can get drawn to it. We can do anything we want here.
We can loop anything we need five times or ten times. In this case, let’s create an object and sprite, then we will randomize it.
Go ahead and run it!
It won’t stop. If you want to stop, you must do one of the following.
You have to put it at the start of the layout or call it a function. Let’s add in a function here and we call this function ‘SwanSpriteFunction’
This loop here.
You don’t want it to be the same as the function.
You go to ‘System’ ‘On start of layout’ it will call the function of ‘SwanSpriteFunction’
Look at that here, and let’s run it!
I add some functionality and show you how this work.
We’ll destroy all the sprites on the screen and then call the function.
I copy and paste the function here.
They all get put in that little block area. It makes sense to destroy all the sprites first and then create them.
Run it here!
Let’s look at a few other things we can do. We’ll ‘Add global variable’ named ‘AmountOfSpritesToSpawn’ and make it ten.
In the ‘For’ loop you can add the ‘AmountOfSpritesToSpawn’ variables.
These sprites overlap, so let’s change some of them.
We will change the border here.
I show you a little trick whenever you are doing pixel art, add a larger border than you think.
Everything looks fine. But what if we want to add one? Let’s continue!
Every time we click. It will add another one.
We count one, two, three, four, five, six, seven, eight, nine, and ten.
Click it again, and it’s over ten.
It gets added to a lot here.
To get this, look at the ‘Debug layout’
You can see that the ‘AmountOfSpritesToSpawn’ is 20.
If we click it again.
It’s 40, 80, and 160. That’s weird because we only added one to this here.
Let’s take it out of the loop!
Go back to ‘Debug layout’
You can see that ‘AmountOf SpritesToSpawn’ is 11,12, and 14.
When you put it in the ‘For’ loop, it’s called a lot of times. Therefore, if you want to add one to the ‘AmountOfSpritesToSpawn’ you must keep it outside the ‘For’ loop.
Let’s look at it again!
Every time you click here, it will be added.
Now you can also subtract one.
Let’s run that in the ‘Debug layout’
We’ve got several ‘AmountOfSpritesToSpawn’ looking at here.
You can see the loop somewhere in the back end. This index is supposed to be absolute, meaning it will always be a positive value.
I find that quite interesting because technically can’t have negative times in a ‘For’ loop. That doesn’t make any logical sense. That’s how loops work, and there are more things we need to talk about in the next section.