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