操作脚本3如何创建使用数组激活的if语句

时间:2013-03-08 04:17:48

标签: arrays actionscript-3 if-statement

如果我发了一个if语句,我想让它在目标击中另一个影片剪辑时做某事(我已经知道该怎么做了),但只有当它是正确的目标时,我该怎么做?

function stopdrag(e:MouseEvent):void{
    e.currentTarget.stopDrag();
    if(e.currentTarget.hitTestObject(destination) || //right here is where the script would `check the name of the target// ){`
        scaryface.visible=true;
    }
}

如果目标是数组中的特定值,我将如何确保它只能起作用?

4 个答案:

答案 0 :(得分:0)

您可以在创建该阵列时创建该目标的自定义属性,如ID(arr [0] .ID =“abc”)或类似内容,然后您可以检查ID。

如果条件是你试图匹配数组中的任何值,那么你可以检查for循环中的索引。

答案 1 :(得分:0)

我无法确切地说出你在问什么。如果您正在尝试检查目标是否在数组中包含,您可以使用类似下面的代码:

function stopdrag(e:MouseEvent):void{
    e.currentTarget.stopDrag();
    if(e.currentTarget.hitTestObject(destination) && yourArray.indexOf(e.currentTarget) > 0){
         scaryface.visible=true;
    }
}

这将检查目标是否与目标发生碰撞,如果是,则检查目标是否包含在yourArray内。

答案 2 :(得分:0)

你可以设置一个开关/案例或嵌套的if语句......如果你没有大量的目标......例如:

if (e.currentTarget.hitTestObject(destination1){
doSomething();
}else if{
(e.currentTarget.hitTestObject(destination2){
doSomethingElse();
}

另外,类似Vipul上面的解决方案是非常明智的

答案 3 :(得分:0)

我认为在不考虑性能的情况下,最简单,最懒惰的方法是

if(array.indexOf(e.currentTarget) >= 0){
    //do something
}