Lesson 8 - Repetition
We have to test for collisions with several sprites. The most obvious thing to do might be to repeat the intersection test over and over like this:
if sprite mysprite intersects 3 then
hit sprite 3
end if
if sprite mysprite intersects 4 then
hit sprite 4
end if
if sprite mysprite intersects 5 then
hit sprite 5
end if
if sprite mysprite intersects 6 then
hit sprite 6
end if
...and so on. This would work fine, if being a little inelegant, but imagine what a bother this would be if we decided to move our invaders into different sprite channels. Imagine how long and unwieldy your script would become if you decided to add eight more invaders.
The key to simplifying an operation like this, and making it scalable, is the word repeat. If we could repeat the same instruction over and over just changing the sprite channel each time it would be great, and indeed we can!
on exitFrame me
if shooting then
set myV to the locV of sprite mySprite
if myV < 0 then
set shooting to false
set the locV of sprite mysprite to -999
return
end if
repeat with target = 3 to 10
if sprite mysprite intersects target then
hit sprite target
set shooting to false
set the locV of sprite mysprite to -999
return
end if
end repeat
set the locV of sprite mySprite to myV - 1
end if
end
We now have a loop containing the necessary test. Every time around the loop, the value of the variable target increases so that all the channels from 3 to 10 are tested for collisions with the if test. If you run the movie you will find that you can now shoot at all the invaders and, if you hit them they will all disappear.