asfunction在as3中的代替品

as3中不再使用asfunction来实现文本和ActionScript的交互。

as3中使用“事件”模式来实现原来的asfunction功能。具体做法:

1、在href属性中添加Event:href="event:event字符串"

2、侦听文本域(TextField):TextField.addEventListener(TextEvent.LINK,linkHandler)

3、处理事件:function linkHandler(e:TextEvent):void{}

ActionScript源码: 

var myHTMLText:String = "Sample text in an html enabled text box.\n"+
"Here's a normal link to <a href='http://bloc.circlecube.com'>circlecube</a> putting the link into the href attribute like normal!\n"+
"<a href='event:clickLink'>Click this circlecube</a>, to see the text event link in action! \n"+
"And some more links that don't go anywhere, but they do call functions in actionscript. "+
"Click this to move <a href='event:moveUp'>UP</a>, click me move back "+
"<a href='event:moveDown'>DOWN</a>.\n"+
"Also, one last example <a href='event:testing'>click for a trace test</a>";

//create and initialize css
var myCSS:StyleSheet = new StyleSheet();
myCSS.setStyle("a:link", {color:'#0000CC',textDecoration:'none'});
myCSS.setStyle("a:hover", {color:'#0000FF',textDecoration:'underline'});
myHTML.styleSheet = myCSS;
myHTML.htmlText = myHTMLText;

myHTML.addEventListener(TextEvent.LINK, linkHandler);

function linkHandler(linkEvent:TextEvent):void {
    switch (linkEvent.text) {
        case "clickLink":
            clickLink();
            break;
        case "moveUp":
            moveUp();
            break;
        case "moveDown":
            moveDown();
            break;
        default:
            giveFeedback(linkEvent.text);
    }   
}
//function to be called from html text
function clickLink():void {
    giveFeedback("Hyperlink clicked!");
    var myURL:String = "http://blog.circlecube.com";
    var myRequest:URLRequest = new URLRequest(myURL);
    try {            
        navigateToURL(myRequest);
    }
    catch (e:Error) {
        // handle error here
        giveFeedback(e);
    }
}

//another function to be called from html text, recieves one argument
function moveUp():void {
    feedback.y -= 10;
    giveFeedback("Up");
}

//a simple trick to allow passing of multiple arguments
function moveDown():void {
    feedback.y += 10;
    giveFeedback("Down");
}

function giveFeedback(str):void {
    trace(str);
    feedback.appendText(str +"\n");
    feedback.scrollV = feedback.maxScrollV;
}

 下载实例文件For Flash CS4:http://blog.circlecube.com/wp-content/uploads/2008/12/textlinkevent_as3.fla

英文版:http://blog.circlecube.com/2008/12/tutorial/asfunction-texteventlink-tutorial-for-as3-flash-html-link-to-call-actionscript-function-tutorial/

公告栏

  • 姓名:林剑锋(不见不散)
  • 来自:中国-广州
  • 简介:潮汕人,出生于广东揭西。醉心于Web开发8年,挑战了一年整体策划和网站运营。目前就职于平成混媒IT部。
  • Email/QQ:admin@ljf.cn

QQ群:设计学院 68075618,网站设计师 9908776

统计

文章:335篇
评论:163条 (2条Spam)
相册:1个 (120张图片)
主题:Nagrand新主题

www.ljf.cn网站PR查询