flash actionscript:
:confused;I have drug a button component (instance name: myButton_btn) onto a movie clip in the library. I have set the library movieclip linkage for exporting. I now use the this.attachMovieClip(xx, xx, xx) on a frame in the root timeline to place a copy of the library movieclip on the stage with the instance name of myClip_mc. I now attempt to label the button using the AS below in a root time line frame. myClip_mc.myButton_btn.label = "test"; No label appears when I test this movie, but a blank button does appear. Does anyone have an ideal what I am doing wrong?
I'm going to have to assume you have made a text field inside of the button symbol or movie clip with an instance name of label. If this is the case, then you need to add .text. _root.mymovieclip.mybutton.label.text = "string";
Label is a property ovf the button component and I shouldn't need to use a text field. If I place the AS myButton_btn.label="test"; In a frame of the library movieclip.t works fine.
If you want to create buttons (or other components) dynamically, you can use the following approach. Make sure a copy of every component you want to use is in the library. // import the component classes in case you want to use other components too // else use import mx.controls.Button; import mx.controls.*; // attach mc 'box_mc' from the library var container:MovieClip=this.attachMovie("box_mc","box_mc",1); // create a button in the attached movie var myBtn_btn:Button=container.createClassObject(Button,"button1_btn",1); // position the button myBtn_btn.move(20,20); myBtn_btn.label="Test"; myBtn_btn.onRelease=function():Void{ trace("This works just fine."); };
LuigiL, I don't quite understand what you mean by: "Make sure a copy of every component you want to use is in the library." I tried to implement the code in a blank flash document, but no button. I am trying to dynamically create buttons. Thank you, Jon
If you test the code in a blank flash document do the following: drag a button component to the stage from the components pane. Now delete the button. You now have a copy of the button component in the library. Test the movie.
Ok, nevermind, I found out my problem. I did not link my movie clip properly.
Couple of more questions: Can you add more than one button to the same movie clip/container? I have an array of variables, and I would like to create on button per variable, how would I go about doing that? Thank you
LuigiL, I dont know if you go my last message, because I replied to my own post. I saw your speed resoponse, so I figured, you were notified every time some one replys to your messages. Sorry to bug you, but I had a couple more questions that I posted. Thank you.
You can create more buttons and you can create complete forms using all the components you need. An example to create more buttons (I use an empty movieclip this time): // import the component classes in case you want to use other components too // else use import mx.controls.Button; import mx.controls.*; // create an empty movieclip to hold the buttons var container:MovieClip=this.createEmptyMovieClip("box_mc",1); var buttonText:Array=["Test 1","Test 2","Test 3"]; var rollOverText:Array=["This is button 1","This is button 2","This is button 3"]; // create buttons in a movieclip function createButtons(target_mc:MovieClip,numberOfButtons:Number):Void{ for(i=0;i<numberOfButtons;i++){ var myBtn_btn:Button=container.createClassObject(Button,"button"+i+"_btn",i+1); // position the button myBtn_btn.move(20,20+(30*i)); setButtonText(myBtn_btn,i); setOnRelease(myBtn_btn,i); } } function setButtonText(target:Button,i:Number):Void{ target.label=buttonText[i]; } function setOnRelease(target:Button,i:Number):Void{ target.onRelease=function():Void{ trace(rollOverText[i]); }; } // and create the buttons in our empty movieclip createButtons(container,3);
Don't see what you're looking for? Try a search.
|