Actionscript problem in Dynamic TextField

落花浮王杯 提交于 2019-12-01 14:03:44

Actually contrary to the other answers for this question, you can(kind of). By using an image tag in a string value for a Textfield object's htmlText property you can use a display object in the library as its source. Then you can use the Textfield object's getImageReference() method to get the image tag which has the display object from the library as its source. Heres an example:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;
    import flash.events.MouseEvent;

    public class Main extends Sprite 
    {
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var textField:TextField = new TextField()
            textField.width = 125;
            textField.height = 50;
            textField.htmlText = "This is a button <img id='myButton' src='MyButton'/> ";
            addChild(textField);

            var myButton:Sprite = textField.getImageReference("myButton") as Sprite;
            myButton.addEventListener(MouseEvent.CLICK, onMyButtonClick);

        }// end function

        private function onMyButtonClick(e:MouseEvent):void
        {
            trace("CLICKED!!!");

        }// end function

    }// end class

}// end package

there's no addChild method in TextField. But you may cheat and place button over TextField (i.e. add it to container of a TextField).

Short answer, no. You'd need to make a wrapper class/MovieClip containing both the button and text field.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!