1
對於我的Flash應用程序,我需要計算過程中的所有對象,無論是計時器,牆壁,坦克還是導彈!我在尋找,但我發現了一些函數來計算相同類型的對象的數量。有沒有一個功能,類或任何能夠做我需要的東西?計算as3中的所有對象
對於我的Flash應用程序,我需要計算過程中的所有對象,無論是計時器,牆壁,坦克還是導彈!我在尋找,但我發現了一些函數來計算相同類型的對象的數量。有沒有一個功能,類或任何能夠做我需要的東西?計算as3中的所有對象
我也許有一個解決方案給你,但它不是很美麗:p 你可以使用一個爲你實例化一切的類,並保持實例的數量。
看看這個類爲例:
package
{
import flash.utils.Dictionary;
import flash.utils.getQualifiedClassName;
public class ClassInstaciator
{
/** keep the count of the instance by class names **/
private static var _counts:Dictionary;
public function ClassInstaciator()
{
}
/** instanciate a class **/
public static function instanciate(classe:Class, ...arg):*
{
// create dictionary if not exists
if(!_counts) _counts = new Dictionary();
// get class name
var className:String = getQualifiedClassName(classe);
// if the class isn't int he dictionaray then add it, else increment count
if(!_counts[className]) _counts[className] = 1;
else _counts[className] += 1;
// return the instance depending on args length
switch (arg.length) {
case 0: return new classe();
case 1: return new classe(arg[0]);
case 2: return new classe(arg[0], arg[1]);
case 3: return new classe(arg[0], arg[1], arg[2]);
case 4: return new classe(arg[0], arg[1], arg[2], arg[3]);
case 5: return new classe(arg[0], arg[1], arg[2], arg[3], arg[4]);
case 6: return new classe(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5]);
case 7: return new classe(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], arg[6]);
case 8: return new classe(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], arg[6], arg[7]);
case 9: return new classe(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], arg[6], arg[7], arg[8]);
case 10: return new classe(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], arg[6], arg[7], arg[8], arg[9]);
}
// return null mean you have more than 10 args ... add cases to the switch ...
return null;
}
/** return the number of instance created for a Class **/
public static function countFor(classe:Class):int
{
// get class name
var className:String = getQualifiedClassName(classe);
// no instances for this class
if(!_counts || !_counts[className]) return 0;
// return the instance count
return _counts[className];
}
/** return a string representing the count of the classes instance by name **/
public static function toString():String
{
var s:String = '';
for(var name:String in _counts)
{
s += name+'::'+_counts[name]+'\r';
}
return s;
}
}
}
,看看如何使用它:
var mc :MovieClip = ClassInstaciator.instanciate(MovieClip) as MovieClip;
var time :Timer = ClassInstaciator.instanciate(Timer, 200, 2) as Timer;
var time2 :Timer = ClassInstaciator.instanciate(Timer, 200, 2) as Timer;
var bd :BitmapData = ClassInstaciator.instanciate(BitmapData, 200, 200) as BitmapData;
trace(ClassInstaciator.countFor(Timer));
//result -> 2
trace(ClassInstaciator.toString());
// result ->
//flash.utils::Timer::2
//flash.display::MovieClip::1
//flash.display::BitmapData::1
正如我說這之前不是很美麗,但它的工作原理;)
也許它可以幫助你。
如果要對實例進行計數的所有類都是您的,則可以爲每個類添加一個靜態var計數並在每個構造函數上增加它。這更好,更簡單。如果它們不是你的,你可以擴展它們來添加count屬性。 –
如果它們是DisplayObject實例(Sprite/MovieClip等),則可以使用容器的numChildren,然後根據需要迭代/過濾 –