你需要的.getChildAt()
结果,先投放到您的naftaBonusClass
对象:
if(getChildAt(i).localToGlobal(new Point(stage.y, 0)).y > 650)
{
var myObj:naftaBonusClass = getChildAt(i) as naftaBonusClass;
trace(myObj.tipo);
removeChildAt(i);
}
或者干脆:
trace((getChildAt(i) as naftaBonusClass).tipo);
Here's some more information about type casting in AS3。
至于不知道每个对象会是什么,这是你需要清理和正确管理的东西。例如,您可以将每个元素存储在一个数组中,并且每个数组都应该包含某种类型的对象。然后,您就可以查看阵列与一个类型for each()
循环:
for each(var i:naftaBonusClass in naftaList)
{
trace(i.tipo);
}
变本加厉,你可以为你的对象的基类和存储方法,在那里,这将在你的循环调用。这样,你只需要强制转换为基类,并调用该方法,您也可以在子类中重写:
class MyChild extends MovieClip
{
public function update():void{ }
}
然后修改后的naftaBonusClass
:
class naftaBonusClass extends MyChild
{
private var _tipo:String = "nafta";
// This is where the work should be done.
override public function update():void
{
trace(tipo);
}
public function get tipo():String
{
return _tipo;
}
}
而更新后的循环:
if(getChildAt(i).localToGlobal(new Point(stage.y, 0)).y > 650)
{
(getChildAt(i) as MyClass).update();
removeChildAt(i);
}
的事情是,我不知道这个类,因为我的孩子的可各种不同的类别。 –
但他们都有财产“tipo”。 –
@JonathanCalb创建一个基类,*您的所有对象*将从定义的属性'tipo'继承。如果你需要对每个元素做不同的事情,你应该用一个'update()'方法创建一个基类,所有的对象都会扩展,然后在每个子类中做任何你需要做的事情。 – Marty