RSS twitter

Posts tagged ‘TextEvent’

link button with actionscript 3

Event Object Type: flash.events.TextEvent
TextEvent.type property = flash.events.TextEvent.LINK

Language Version : ActionScript 3.0
Runtime Versions : AIR 1.0, Flash Player 9

Dispatched when a user clicks a hyperlink in an HTML-enabled text field, where the URL begins with “event:”. The remainder of the URL after “event:” will be placed in the text property of the LINK event.

Note: The default behavior, adding the text to the text field, occurs only when Flash Player generates the event, which in this case happens when a user attempts to input text. You cannot put text into a text field by sending it textInput events.

Defines the value of the type property of a link event object.This event has the following properties:

Property Value
bubbles true
cancelable false; there is no default behavior to cancel.
currentTarget The object that is actively processing the Event object with an event listener.
target The text field containing the hyperlink that has been clicked. The target is not always the object in the display list that registered the event listener. Use the currentTarget property to access the object in the display list that is currently processing the event.
text The remainder of the URL after “event:”

Example

In the following example, the playMP3() function is defined. A TextField object named list is created and populated with HTML text. The text "Track 1" and "Track 2" are links inside the text field. The playMP3() function is called when the user clicks either link. The name of the MP3 file, which follows the string “event:” in the href attribute of the HTML tag, is passed to the linkHandler() method as the text property of the link event object.

package {
    import flash.display.Sprite;
    import flash.errors.IOError;
    import flash.events.IOErrorEvent;
    import flash.events.TextEvent;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;

    public class TextField_event_link extends Sprite
    {
        private var myMP3:Sound;
        public function TextField_event_link() {
            myMP3 = new Sound();
            var list:TextField = new TextField();
            list.autoSize = TextFieldAutoSize.LEFT;
            list.multiline = true;
            list.htmlText = "<a href=\"event:track1.mp3\">Track 1</a><br>";
            list.htmlText += "<a href=\"event:track2.mp3\">Track 2</a><br>";
            addEventListener(TextEvent.LINK, linkHandler);
            addChild(list);
        }

        private function playMP3(mp3:String):void {
            try {
                myMP3.load(new URLRequest(mp3));
                myMP3.play();
            }
            catch(err:Error) {
                trace(err.message);
            }
            myMP3.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
        }

        private function linkHandler(linkEvent:TextEvent):void {
            playMP3(linkEvent.text);
        }

        private function errorHandler(errorEvent:IOErrorEvent):void {
            trace(errorEvent.text);
        }
    }
}

In this flash tutorial you will see how to through code, dynamically create a text link in flash and give it an event when its clicked. If this sounds complicated, just take a look below, its quite simple, even though AS3 is still a bit different the the pervious version and I am still learning.

This flash example we are going to make does not require much visual work on the stage, the only thing we need is to make a text field so grab the text tool and drag a text field on the stage, go to the properties panel and make it a dynamic text field (so it can receive input). Then give it an instance name, I named mine message_ (not message because that word is reserved by flash code).

Now everything else is done by code, so open up the flash action script panel and type in the following code, I made some description in between the code to make it more understandable.
view plain?

//First we declare our textfield object so it exists in flash.
var thelink:TextField = new TextField();

//Now we give the text field a value to show and as we use the htmltext
//it renders the content as html code, (quite a nice feature I think).
//An inportant thing to notice here is the event:you clicked it. this,
//this is the event that will be raised when the link is clicked.
thelink.htmlText = ’<a href=”event:You%20clicked%20it”>Click here</a>’;

//here we add the link to the main stage (of cause if you want to add it
//else where, just refere to the position (eg. movieclip.addChild(thelink).
addChild(thelink);

//These two lines is just to place the link somewhere on the stage and not in position 0,0.
thelink.x = 50;
thelink.y = 20;

//And we are working in AS3 so we need to add an eventlistener to tell flash to listen to,
//if the link is clicked.
thelink.addEventListener(TextEvent.LINK, linkEvent);

//And here is the function to “run” the event, it bascilly just says to wirte the event text
//in our text field which we named “message_”
function linkEvent(event:TextEvent):void {
message_.text = event.text
}