在Flash / Actionscript中添加指向Button的链接

时间:2010-02-24 15:57:02

标签: flash actionscript button

如何在Flash影片剪辑中的按钮上建立链接?

1 个答案:

答案 0 :(得分:5)

假设您的按钮的实例名称为“myButton”,则在您的按钮位于时间轴中的帧上,您将添加以下(或类似)Actionscript。

<强> AS2

myButton.onPress = function()
{
   getURL("http://stackoverflow.com", "_blank");
}

<强> AS3

import flash.events.MouseEvent;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;

myButton.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(event:MouseEvent):void
{
   var request:URLRequest = new URLRequest("http://stackoverflow.com");
   request.method = URLRequestMethod.GET;
   var target:String = "_blank";
   navigateToURL(request, target);
}

AS3的向后兼容getURL

/**
 * Backwards compatibility for global getURL function.
 *
 * @param url     Url to go to.
 * @param method  'get' or 'post'.
 * @param target  Window target frame [ex. '_blank'].
 */
public static function getURL(url:String, target:String = '_blank', method:String = 'get'):void
{
    method = method.toUpperCase();
    target = target.toLowerCase();

    if (method != URLRequestMethod.POST && method != URLRequestMethod.GET) method = URLRequestMethod.GET;
    if (target != "_self" && target != "_blank" && target != "_parent" && target != "_top") target = "_blank";
    var request:URLRequest = new URLRequest(url);
    request.method = method;
    navigateToURL(request, target);
}

请注意,在AS2和AS3中,您可以在类中编写此代码,并将该类设置为button / movieclip的导出类。这可能有点复杂了。