重申我的问题更简单......
我希望能够将Action Script事件链接到文本框中的单击文本。我唯一能看到的是创建基本的Hyper Link,但不应用任何操作:
我已经搞乱了一个多小时,但却看不到应用动作脚本的方法,互联网上的所有教程似乎都是针对ActionScript 3而不是完全按照我的意愿行事。
原因是该网站有背景音乐,当YouTube启动时,需要静音。我知道代码要静音并在之前对自定义对象执行此操作,但我看不到将脚本应用于文本框超链接的任何方法。
虽然我希望这样做,但我很乐意考虑任何导致打开页面并使网站静音的解决方案。
老实说,我尝试快速切换到AS3,但由于有很多问题需要解决,我宁愿花时间将网站转换为HTML / Jquery甚至是Silverlight ....我只是希望有一些我忽略的小事可以在不需要太多改动的情况下完成。
答案 0 :(得分:1)
您可以使用TextEvent.LINK事件来监听文本字段中链接的用户点击次数:
import flash.text.TextField;
import flash.events.TextEvent;
var textField:TextField = new TextField();
textField.htmlText = "<a href='event:arg1,arg2'><b>hyperlink</b></a>";
addChild(textField);
textField.addEventListener(TextEvent.LINK, onTextFieldLink);
function onTextFieldLink(e:TextEvent):void
{
var args:Array = e.text.split(",");
trace(args); // output: arg1, arg2
}// end function
“event:”之后的任何部分都存储在事件对象的text属性中。您可以使用String.split()方法模拟对事件对象的解析参数,以使用逗号分隔符来拆分文本字符串。然后,您可以将每个单独的元素存储在一个数组中。
<强> [UPDATE] 强>
对于您的特定情况(据我所知),以下内容可能更适合您:
import flash.text.TextField;
import flash.events.TextEvent;
import flash.net.navigateToURL;
import flash.net.URLRequest;
var textField:TextField = new TextField();
textField.htmlText = "<a href='event:watch?v=JZweDwbJ_Ic'><b>hyperlink</b></a>";
addChild(textField);
textField.addEventListener(TextEvent.LINK, onTextFieldLink);
function onTextFieldLink(e:TextEvent):void
{
var url:String = "http://www.youtube.com/" + e.text;
var urlRequest:URLRequest = new URLRequest(url);
navigateToURL(urlRequest, "_blank");
}// end function
答案 1 :(得分:1)
您还可以使用 TextArea类(不是TEXTAREA THE COMPONENT!它是由doitflash创建的全新OOP类),而不是在您需要工作的地方使用 TextField 类在项目中使用文字!
该网站制作了一个名为“TextArea”的类,它是原始“TextField”类的扩展,它结束了TextField的缺点。
TextArea允许您直接从文本块内的超链接调用自己的自定义函数,而不是仅仅通过在单击超链接时添加侦听器来调用外部链接或混淆自己......因为如果您想要甚至通过你的文本块中的函数传递参数,这很难,但TextArea也回答了这个问题:)
以下是通过文本块内的超链接调用函数的示例代码:
import flash.text.TextFieldAutoSize;
import com.doitflash.text.TextArea;
// set TextArea
var _textArea:TextArea = new TextArea();
_textArea.condenseWhite = true;
_textArea.autoSize = TextFieldAutoSize.LEFT;
_textArea.embedFonts = false;
_textArea.border = true;
_textArea.multiline = true;
_textArea.wordWrap = true;
_textArea.width = 200;
_textArea.holder = this;
_textArea.client = this; // must be where you have your 'allowed functions' saved
_textArea.funcSecurity = true;
_textArea.allowedFunctions(stringLink, objectLink, arrayLink, arrayObjectStringLink);
_textArea.mouseRollOverEnabled = true;
_textArea.fmlText = "<p>Pass String as arguments in this <a href='event:stringLink(simple string)'>link</a>.</p>";
/*
my custom functions that I call from _textArea.fmlText by using <a /> tags, like we used to insert hyperlinks inside our text blocks.
for example, to call stringLink() from _textArea.fmlText, you can write:
_textArea.fmlText = "my hyperlink: <a href='event:stringLink(this is my passed value)'>link</a>.";
NOTE: your custom functions arguments can be as many as you like (supported argument types: Object, Array, String)
*/
function stringLink($value:String):void
{
trace("custom function");
}
它具有更多功能,并不特别限于此问题。查看doitflash了解更多信息。该网站提供平台并下载它也是免费的:)