Home‎ > ‎

Too Many Objects?

If you have many objects and are getting tired of repetitious code, then you need to use the Repeat command

        eg

    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
    ...
    if intersect (button "box", button "enemy32", "pixels") then
        answer "You lose"
    end if
        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
    ...
    if intersect (button "box", button "enemy32", "pixels") then
        answer "You lose"
    end if

All of the above can be replaced with:

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

Another way to handles many objects together is to "Group" them - Too Many Objects-2

Comments