クラス名から表示オブジェクトのインデックスを配列で取得

by meat18
クラス名から表示オブジェクトのインデックスを配列で取得。
getChildIndexByClassname(表示オブジェクトコンテナ,対象の表示オブジェクトのクラス名)
偶数インデックスのTextFieldだけを一括削除、とかに使うかもしれない。
 
♥0 | Line 36 | Modified 2010-03-14 14:55:27 | MIT License
play

ActionScript3 source code

/**
 * Copyright meat18 ( http://wonderfl.net/user/meat18 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/iR84
 */

/*

クラス名から表示オブジェクトのインデックスを配列で取得。
getChildIndexByClassname(表示オブジェクトコンテナ,対象の表示オブジェクトのクラス名)
偶数インデックスのTextFieldだけを一括削除、とかに使うかもしれない。
 
*/

package {
	import flash.display.Sprite;
	import caurina.transitions.*;
	import flash.utils.*;
	import flash.text.*;
	public class FlashTest extends Sprite {
		public function FlashTest() {
			// write as3 code here..
			for(var i:int = 0;i < 10;i ++){
				if(Math.random() < 0.5){
					var tf:TextField = TextField(stage.addChild(new TextField));
					tf.x = Math.floor(Math.random()*400);
					tf.y = Math.floor(Math.random()*400) + 40;
					tf.text = "text field";
				}else{
					var sp:Sprite = Sprite(stage.addChild(new Sprite));
					sp.graphics.beginFill(0xFF0000);
					sp.graphics.drawCircle(0,0,20); 
					sp.x = Math.floor(Math.random()*400);
					sp.y = Math.floor(Math.random()*400) + 60;
				}
			}
			var tfd:TextField = new TextField();
			tfd.text = " stage上のtextfieldのインデックス " + getChildIndexByClassname(stage,TextField).toString() + " spriteのインデックス " + getChildIndexByClassname(stage,Sprite).toString();
			stage.addChild(tfd);
			
			//一括でプロパティ変更
			getChildIndexByClassname(stage,TextField).forEach(function(tfIndex:int, index:int, arr:Array):void{TextField(stage.getChildAt(tfIndex)).autoSize = "left";});
			
			//一括tween
			getChildIndexByClassname(stage,Sprite).forEach(function(spIndex:int, index:int, arr:Array):void{Tweener.addTween(Sprite(stage.getChildAt(spIndex)),{time:2,x:Math.random()*100 + 100,transition:["linear","easeOutBounce","easeOutElastic"][int(Math.random()*4)]});});
		}
		public function getChildIndexByClassname(obj:*,classname:Class):Array{
			var ar:Array = [];
			for(var i:int = 0;i < obj.numChildren;i ++){
				if(obj.getChildAt(i) is classname) ar.push(i);
			}
			return ar;
		}
	}
}