![]() |
![]() |
||||||
|
|||||||
| Tags: |
![]() |
|
|||
|
I have 3 questions regarding my migration from AS2 to AS3.
First, in AS2, I could generate variables on the fly. For example, I could write: var textHolder=new MovieClip textHolder.myVar=1 I believe that doing this in AS3 requires me to extend MovieClip and to declare myVar in the sub-class. Is this correct? Second, in the code below, a text field (i.e. tf) is a child of a Movie Clip (i.e. textHolder), however, I don't seem to be able to control the text field though the chain textHolder.tf For example, if I try the following: trace("name="+e.target.tf.name), I get the error message: A term is undefined and has no properties. Is there another way to do this. Third, I notice that the following line is very important: textHolder.mouseChildren =false Removing it allows me to use the following trace statement: trace("name="+e.target.text) What is happening here? package{ import flash.display.MovieClip; import flash.events.Event; import flash.events.MouseEvent; import flash.text.*; public class Simon extends MovieClip{ private var tf:TextField=new TextField private var myWordsArray:Array=[] private var textFieldSpacing:int private var format1:TextFormat private var presentWord:String private var presentWordNum:int private var correctArray:Array=[] public function Simon(){ var tempWordsArray:Array = "A KING LIVED IN A CASTLE".split("") var myWords:String="" //Add spaces between each of the characters, unless the character is a space (omit the last character)... for(var i:int=0;i<tempWordsArray.length-1;i++){ if(tempWordsArray[i] !=" "){ myWords+=tempWordsArray[i]+" " } } //...now add the final character, minus an extra space myWords+=tempWordsArray[tempWordsArray.length-1] var myWordsArray:Array = myWords.split(""); var lineOffset:int = 0; var presentWord:String="" for (var counter:int=0; counter<myWordsArray.length; counter++) { presentWord = "word"+counter.toString(); presentWordNum=counter //Create a movie clip for each word in the string format1=new TextFormat() format1.size=48 var tf:TextField=new TextField tf.autoSize=TextFieldAutoSize.LEFT tf.selectable=false tf.background=true tf.text=myWordsArray[counter] tf.setTextFormat(format1); var textWidth:int=tf.width var textHolder:MovieClip=new MovieClip textHolder.addChild(tf) textHolder.name=presentWord textHolder.x=textFieldSpacing textFieldSpacing+=textWidth textHolder.buttonMode=true textHolder.mouseChildren =false addChild(textHolder) textHolder.addEventListener(MouseEvent.CLICK, mc_CLICK) } return } private function mc_CLICK(e:Event):void{ trace("name="+e.target.name) } } } |
| Sponsored Links |
|
|||
|
No, the MovieClip class is still dynamic so you can add properties. If you
extend MovieClip but you dont declare your subclass dynamic, then you cannot add properties. To access a display list you need to use getChildByName or getChildAt. If you would add an instance name on your tf you can retrieve that tf using getChildByName(), see attached snippet (assuming you assign an instance name in the loop using tf.name = "tf"; You need to read up on the target and currentTarget properties of events. //I've added a property to textHolder in the loop var textHolder:MovieClip=new MovieClip textHolder.identifier = "I'm just a word"; // and uncommented //textHolder.mouseChildren =false private function mc_CLICK(e:MouseEvent):void{ var clip:MovieClip = e.currentTarget as MovieClip; trace("Holder: " + clip.name); trace("Dynamic property: " + clip.identifier); var tf:TextField = clip.getChildByName("tf") as TextField; trace("tf text: " + tf.text); } |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
- Contact Us
-|-
Adobe Dreamweaver Forums -|-
Archive -|-
Top -|-Rules/Disclaimer-|-Help/Support-|-Advertise