How to create a textbutton in Flash using ActionScript 3?

旧时模样 提交于 2019-12-24 03:18:05

问题


When I tried using a textfield as a button, it seems it is not having the buttonMode property.

How can I programatically create a text button using ActionScript in a Flash project. It should be a simple text, which is clickable.


回答1:


You can add the TextField to a Sprite and use it as the button. buttonMode is the property of Sprite class.

If you really want to use just a TextField, you can assign an anchor tag <a href="event:something">label</a> to its htmlText or listen to mouseOver and mouseOut events and show a custom hand cursor after hiding the default mouse pointer using Mouse.hide()




回答2:


You can use a TextField, set the text format as you wish, set selectable to false, etc. If you want the hand cursor, just nest the textfield into a sprite, and set mouseChildren to false.

e.g.

var textButton:Sprite = getTextButton('Push Me!');
addChild(textButton);
textButton.addEventListener(MouseEvent.CLICK, function(event:MouseEvent){trace('click')});

function getTextButton(label:String):Sprite{
    var txt:TextField = new TextField();
    txt.defaultTextFormat = new TextFormat('Verdana',10,0x000000);
    txt.text = label;
    txt.autoSize = TextFieldAutoSize.LEFT;
    txt.background = txt.border = true;
    txt.selectable = false;
    var btn:Sprite = new Sprite();
    btn.mouseChildren = false;
    btn.addChild(txt);
    btn.buttonMode = true;
    return btn;
}



回答3:


What if you use a button object and style it in the way of a clickable text.




回答4:


I don't really understand your question! If it's a static text, then you can put a LinkButton or if it's a place where user can input text, then add an event listener for the textfield:

myTextField.addEventListener(MouseEvent.CLICK,clickListener);

function clickListener(e:Event):void
{
    // if a user clicks the textfield this function will be called
}


来源:https://stackoverflow.com/questions/3084362/how-to-create-a-textbutton-in-flash-using-actionscript-3

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