AS3数组indexOF

时间:2016-10-30 13:51:04

标签: arrays actionscript-3 indexof

我正在开发一款游戏并且遇到了这个阵列的麻烦。舞台上的所有水果都是由阵列水果产生的。当上诉将击中篮筐时,我们会计算它们。甜瓜不算数。我怎样才能知道当前的水果是上浆还是瓜?

var array_fruit:Array = new Array(appel1_mc, appel2_mc, melon_mc);
var appelsOnstage:Array = new Array();
var appelsCollected:Number = 0;


for (var i:int = 0; i<10; i++) {
  var pickappels = array_fruit[int(Math.random()* array_fruit.length)];
  var spel_appels:MovieClip = new pickappels();
  addChild(spel_appels);
  spel_appels.x = (Math.random() * 800) + 100;
  spel_appels.y = Math.random() * -500;
  spel_appels.speed = Math.random() * 10 + 2;
  appelsOnstage.push(spel_appels);

}


stage.addEventListener(Event.ENTER_FRAME, catchappels);

function catchappels(e:Event):void {

for (var i:int = appelsOnstage.length-1; i > -1; i--) { 
    var currentfruit:MovieClip = appelsOnstage[i];


    if (currentfruit.hitTestObject(basket_mc)) {        

        if(array_fruit.indexOf(currentfruit) == melon_mc ){
               trace("melon");
            } else {
               trace("no melon");
               appelsCollected++;
            }
    }
 }
}

4 个答案:

答案 0 :(得分:0)

indexOf将返回int值。请参阅下面的代码编辑。

if (currentfruit.hitTestObject(basket_mc)) {        
    if(array_fruit.indexOf(currentfruit) == 2 ){
       trace("melon");
    } else {
       trace("no melon");
       appelsCollected++;
    }
}

indexOf有两个参数。第一个是您要搜索的确切元素。第二个参数是从中开始搜索的索引位置。默认值为位置0。

请参阅API文档here。 (总是很乐意先快速阅读这些内容。)

答案 1 :(得分:0)

  

a_fruit.indexOf(currentfruit)= -1

这里的问题是类和对象之间的混淆。

  • a_fruit包含,这些将由此代码实例化

    var pickappels = array_fruit[int(Math.random()* array_fruit.length)];
    var spel_appels:MovieClip = new pickappels();
    
  • appelsOnstage拥有这些类的对象,并在此填充

    appelsOnstage.push(spel_appels);
    

类和对象 - 请原谅双关语! - 苹果和橘子,非常不同的东西。 课程就像是建筑物的蓝图或餐食的配方。您无法将对象与类进行比较,并期望它们是相同的。

相反,您应该找到对象的类,然后将此类与另一个类进行比较。为此,您使用is operator。像

这样的东西
if(currentfruit is melon_mc)

应该告诉你是否有瓜。

答案 2 :(得分:0)

您应该在此处使用is运算符,以便更好地进行编码。如果您更改数组中类的位置会怎样?

if ((currentfruit is appel1_mc) || (currentfruit is appel2_mc)) {
    // apple
}

答案 3 :(得分:0)

只是在伪代码中找出处理这个问题的可能方法。 在时间表上:

import com.fruits.Fruit;
import com.fruits.Melon;
import com.fruits.Apple
var fruit_1:Melon = new Melon();
var fruit_2:Apple = new Apple();
if(fruit_1 is Melon){
    trace("anyway my SuperclassName is : " + fruit_1.getType());
    trace (fruit_1.getMyType());
    trace("");
}
if(fruit_2 is Apple){
    trace("anyway my SuperclassName is : " + fruit_2.getType());
    trace (fruit_2.getMyType());
    trace("");
}

在com.Fruit.as中:

package com.fruits {
    public class Fruit {
        import flash.utils.getDefinitionByName;
        import flash.utils.getQualifiedClassName;
        import flash.utils.getQualifiedSuperclassName;
        public function Fruit() {
            trace ("I'm a Fruit");
        }
        public function getType():String{
            var type:String = getQualifiedSuperclassName(this)
            var str:String = (type);
            return str;
        }
    }
}

com.Melon:

package com.fruits {
    public class Melon extends Fruit {
        import flash.utils.getDefinitionByName;
        import flash.utils.getQualifiedClassName;
        import flash.utils.getQualifiedSuperclassName;
        public function Melon() {
            super();
            trace ("Melon says : ");
            trace (" because I'm Fruit and not happy to be a Melon");
            trace("");
        }
        public function getMyType():String{
            var type:String = getQualifiedClassName(this)
            var str:String = ("Im a " + type);
            trace("Class said :  I worth nothing because I'm an Fruit and not proud to be an Melon");
            str += "\n" + "but in fact I'm a " + getQualifiedSuperclassName(this)
            return str;
        }
    }
}

在com.Apple中:

package com.fruits {
    public class Melon extends Fruit {
        import flash.utils.getDefinitionByName;
        import flash.utils.getQualifiedClassName;
        import flash.utils.getQualifiedSuperclassName;
        public function Melon() {
            super();
            trace ("Melon says : ");
            trace (" because I'm Fruit and not happy to be a Melon");
            trace("");
        }
        public function getMyType():String{
            var type:String = getQualifiedClassName(this)
            var str:String = ("Im a " + type);
            trace("Class said :  I worth nothing because I'm an Fruit and not proud to be an Melon");
            str += "\n" + "but in fact I'm a " + getQualifiedSuperclassName(this)
            return str;
        }
    }
}

这只是一个想法,请告诉我有关您的目标的更多信息,以及这是否有助于您...... 最好的问候尼古拉斯。

PS:我很抱歉,但我的英语非常糟糕。所以我试着把这个问题告诉我......

您可能会忘记indexOf并随时使用addChild您想要将对象/实例设置为另一个.... 所以你不必检查索引。 addChild(一些旧的Child)将使“old Child”成为最后一个索引(在其他实例上)。 对不起,我没有写你的代码,但标题说你要检查水果是甜瓜还是苹果...... 对不起,如果我误解了。 :(

相关问题