Calling functions from a textfield in as3



The good old asfunction in as2 is no more. so does that mean we cant call action script functions from text fields ? Well yes we can . asfunction mechanism has been replaces by a more prominent and organized methodology in action script 3.0. flash.text.TextField dispatches event:link . Means we can setup the text to broadcast event when it is clicked. We can also pass in parameters with this event.

Following example shows how to control a movieclip’s navigation with text links.

Get Adobe Flash player

This code resides in the first frame of the movie.


textBox.htmlText = "<a href="\">Sit</a>";
textBox.htmlText += "<a href="\">Stand</a>";
textBox.addEventListener(TextEvent.LINK, clickHandler);

function clickHandler(linkEvent:flash.events.TextEvent)
{
switch(linkEvent.text)
{
case "frame1":
frog.gotoAndStop(1);
break;

case "frame2":
frog.gotoAndStop(2);
break
}
}

As you see the code is pretty short. For design arrangements , we will create a movie clip, with a different picture in each of its frame, and a total of two frames. The movie clip will have “stop()” in its first frame to prevent auto play.

Next we set up a text field on stage with its HTML rendering turned on. having done this we give it a instance name : textBox and get back to our code for explanation.

textBox.htmlText = "<a href="\">Sit</a>";
textBox.htmlText += "<a href="\">Stand</a>";
textBox.addEventListener(TextEvent.LINK, clickHandler);

We assign html text to the text field’s htmlText property. note the event:frame2
event is the type of event we broadcast (an object of event type). This event is actually of type TextEvent.
But it dosent matter here when broadcasting. Just passing a generic event object is ok. The frame1 and frame2 are the text property of the TextEvent.

Now we assign a event listener for the text field which handles the “TextEvent.LINK” . now again the text field need not be the object to assign the listener to . It can be assigned even to the stage, because the “TextEvent.LINK” will be broadcasted only when user click on the text.

Finally we create our handler -

function clickHandler(linkEvent:flash.events.TextEvent)

which takes an object of type TextEvent. we then test for the text property of the TextEvent using simple switch case to determine which text was clicked. Accordingly we change the frame of the movieclip on the stage. simple enough :)

Download fla source here

Popularity: 15% [?]




October 18, 2009   Posted in: Actionscript 3.0, General

29 Responses

  1. goblin - March 9, 2011

    Affordable Web Hosting…

    [...]below you’ll find the link to some sites that we think you should visit[...]…

  2. http://breastactivesexposed.com/tag/develop-breast - March 10, 2011

    World Wide Research…

    [...] following you will find the hyperlink to somewhat of an online that any of us assume this take in [...]…

  3. Pepper Mint - March 10, 2011

    Quite the read…

    [...]very interesting commonality to our prior article… But be critical on resources[...]…

  4. Student Loan Consolidation News - March 11, 2011

    First student loan…

    [...]below you’ll find the link to some sites that we think you should visit[...]…

  5. הכרויות בטלפון - March 13, 2011

    הכרויות בטלפון…

    [...]below you’ll find the link to some sites that we think you should visit[...]…

  6. Wrinkle Cream - March 13, 2011

    Wrinkle Cream Critic…

    [...]below you’ll find the link to some sites that we think you should visit[...]…

  7. how to get abs fast - March 16, 2011

    how to get abs fast…

    [...]below you’ll find the link to some sites that we think you should visit[...]…

  8. get rid cellulite treatment - March 17, 2011

    Amazing site…

    [...]the time to read or visit the content or sites we have linked to below the[...]…

  9. Anonymous - March 17, 2011

    how to tone your stomach…

    [...]the time to check out sites we have linked to underneath the[...]…

  10. jamie hudson - March 17, 2011

    online mlm secrets…

    [...]the time to read or visit the content or sites we have linked to below the[...]…

  11. viagra - March 20, 2011

    Hi…

    This website is the top internet sites….

  12. margaritaville margarita machine - March 21, 2011

    margarita machine or a blender…

    [...]the time to read or visit the content or sites we have linked to below the[...]…

  13. bamboo sheets - March 22, 2011

    Fantastic Post…

    [...]is always a good read, take a look now to see if there is anything new and let me know if you[...]…

  14. Washington Divorce Lawyers - March 24, 2011

    Attorneys…

    [...]below, are some quite dissimilar places to mine, however the sites are definitely worth checking out[...]…

  15. submersible pump - March 29, 2011

    how to stop puppies biting…

    [...]followed below there can be the website link towards a world-wide-web sites i visualise be certain to use[...]…

  16. la ink tattoos - March 29, 2011

    Trackback, Thanks very much…

    la ink tattoo designs, has linked to your web page….

  17. Conveyancing Solicitor - March 29, 2011

    Conveyancing Solicitors…

    [...]below you’ll find the link to some sites that we think you should visit[...]…

  18. Property Solicitors - March 30, 2011

    Conveyancing Solicitors…

    [...]below you’ll find the link to some sites that we think you should visit[...]…

  19. train horns - April 1, 2011

    Amazing site…

    [...]the time to read or visit the content or sites we have linked to below the[...]…

  20. Approved - April 1, 2011

    [...]the time to read or visit the content or sites we have linked to below the[...]…

  21. richard chorley - April 3, 2011

    richard chorley golf…

    Thanks for such an excellent page, bookmarked, ill visit soon!…

  22. health and safety signs - April 4, 2011

    safety signs…

    Thanks for such an excellent page, bookmarked, ill visit soon!…

  23. laptop battery - April 4, 2011

    laptop charger…

    Thanks for such an excellent page, bookmarked, ill visit soon!…

  24. Dremel Stylus - April 6, 2011

    Dremel Stylus…

    Bookmarked your site, so I can visit frequently, thank you!…

  25. Hunstanton - April 6, 2011

    Hunstanton…

    Trackback, I have linked to your site, thank you!…

  26. market place - April 7, 2011

    market place…

    Trackback, I have linked to your site, thank you!…

  27. TV Shack - April 7, 2011

    TV Shack…

    Trackback, I have linked to your site, thank you!…

  28. low carb meals - April 8, 2011

    low carb meals…

    Fantastic blog, I have linked back to your site!…

  29. mobile web design - April 8, 2011

    mobile website design…

    Fantastic blog, I have linked back to your site!…

Leave a Reply