0
votes

I want to check if my character is hitting any of the items in an array (true) and if he isn't (false). right now the boolean is in a for loop so it returns one "true" and multiple "false" statements each time the program updates. I just want one return, true if the character is hitting a movie clip in the array, and false if he isn't. Here's the code:

for(var i:int = 0; i<steps.length; i++){
            if(steps[i].hitTestPoint(hero.x,hero.y+hHeight/2, true)){
                onSteps = true;
            }else{
                onSteps = false;
            }   
}
3
You want to exit the loop returning true on the first true you find.Tim

3 Answers

0
votes

I think what you want is a function that goes through the steps array and then returns true as soon as one hits. If none have hit, it defaults to returning "false".

function checkForHits():Boolean {
    for(var i:int = 0; i<steps.length; i++){
        if(steps[i].hitTestPoint(hero.x,hero.y+hHeight/2, true)){
            return true;
        }
    }
    return false;
}
0
votes

The Array object already has a method

some(callback:Function, thisObject:* = null):Boolean

which returns true in the case that any element of the Array satisfies the callback function, and false in the case that no element of the Array satisfies the callback function.

Here's the docs: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#some()

Your code would be something like:

onSteps = steps.some(function (item:*, index:int, array:Array):Boolean
            {
               return item.hitTestPoint(hero.x,hero.y+hHeight/2, true);
            });