IDを可変文字で指定してプロパティを変更
FlexでIDを可変文字で指定してプロパティを変更する方法です。
地味に便利なので知っておくと便利かも。
myimage1,myimage2・・・myimage5っていうimageクラスを作成して、それのx,yプロパティをいっぺんい変更します。
mxml部分でImageクラスをIDを指定しつつとりあえず作成
<mx:Image x="10" y="10" width="145" height="159" id="myImage1" source="image.png"/> <mx:Image x="20" y="20" width="145" height="159" id="myImage2" source="image.png"/> <mx:Image x="30" y="30" width="145" height="159" id="myImage3" source="image.png"/> <mx:Image x="40" y="40" width="145" height="159" id="myImage4" source="image.png"/> <mx:Image x="50" y="50" width="145" height="159" id="myImage5" source="image.png"/>
actionscript側で一括プロパティ変換。
for( var i:int = 1 ; i<= 5 ; i ++ ){ // id を stringで指定 var idString:String = "myImage" + i; this[idString].x = 100 + i*100; this[idString].y = 100 + i*100; //this["myImage"+i].x でもOK }
ここでミソとなる部分が←の部分。this[ここにクラスID]で、ストリングからクラスのIDを指定することができます。
( "myimage" + i ).x = 100 + i*100;
っとかできないのでご注意です。
複数のID処理には配列カッコを使いましょう。
これ知る前はすごい悩んだ挙句、IDのプロパティを一個一個指定して行ったり
this.getChildByName("myImage" + i)でなんとかならないかすごい模索してました(^^;