2011-03-09 47 views
0

我想检查我的角色是否击中了数组中的任何项目(true)并且他不是(false)。现在布尔值位于for循环中,因此每次程序更新时都会返回一个“true”和多个“false”语句。我只想要一个返回,如果角色正在击中数组中的影片剪辑,则返回true,如果不是,则返回false。下面的代码:as3命中测试数组从布尔型多重返回

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; 
      } 
} 
+1

你想退出循环的第一个真正的你找到返回true。 – Tim 2011-03-09 15:12:52

回答

0

我想你想要的是一个函数,通过steps数组,然后一旦命中就返回true。如果没有命中,则默认返回“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

非常感谢!得到它与该功能的工作。 – 2011-03-09 17:23:19

+0

@vince没问题:)如果你的问题解决了,你可以点击我的答案旁边的“接受”复选标记 – 2011-03-09 18:20:53

+0

对不起,只是检查了你的答案。再次感谢。 – 2011-03-19 08:54:21

0

Array对象已有的方法

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

其中在的情况下返回true,本案Array满足任何元件的回调函数,false没有元件的Array满足回调函数。

这里的文档:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#some()

您的代码将是这样的:

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