Home‎ > ‎

Too Many Objects?-2

Another way that you can reduce the amount of code you need to write for many objects, is to Group them together...

If you have many enemies on the screen, you can group them together and just write the code for the group instead of each enemy
For instance...You have many enemies flying around the screen

   if intersect (button "box", button "enemy1", "pixels") then
        answer "You lose"
    end if
    if intersect (button "box", button "enemy2", "pixels") then
        answer "You lose"
    end if
    if intersect (button "box", button "enemy3", "pixels") then
        answer "You lose"
    end if

    ... (same for enemies 4 - 31)

    if intersect (button "box", button "enemy32", "pixels") then
        answer "You lose"
    end if
        answer "You lose"
    end if
    if intersect (button "box", button "enemy33", "pixels") then
        answer "You lose"
    end if
    if intersect (button "box", button "enemy34", "pixels") then
        answer "You lose"
    end if
    ...
  

They can be "Grouped" together (selecting all of them by pressing the mouse button and dragging it over them all) and given a name:

    Group "enemies"
 
    Then you can replace all the code above with the following code:

   repeat with i = 1 to 32
        if intersect (button "box", group "enemies" , "pixels") then
            answer "You lose"
        end if
    end repeat


Comments