actionscript-3 - Animate ActionScript 3.0 中的按钮超链接
问题描述
我正在尝试使用 ActionScript 在 Adobe Animate 中制作三个按钮。这是我用于按钮 1 的代码:
button. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void {
navigateToURL(new
URLRequest("https://website.com/"));
}
button2. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler2(event:MouseEvent):void {
navigateToURL(new
URLRequest("https://anotherwebsite.com/"));
}
button3. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler3(event:MouseEvent):void {
navigateToURL(new
URLRequest("https://yetanotherwebsite.com/"));
}
(这些 URL 是编造的,仅用于演示目的。)如您所见,按钮 2 和 3 的代码完全相同,但位于不同的层。我还在开始时更改了实例名称、函数名称和每个按钮的 URL。但是当我按下 CTRL + Enter 时,所有按钮都指向同一个网页(在本例中为“website.com”),这是我首先输入的网页。它应该导致我输入的不同 URL,但它们都指向同一个。为什么会发生这种情况,我该如何解决?
解决方案
问题是,尽管您为 3 个单独的按钮定义了 3 个单独的处理函数,但您将第一个处理函数附加到所有 3 个按钮:
button. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
button2. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
button3. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
你的意思是这样的:
button. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
button2. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler2);
button3. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler3);
不过,顺便说一句,您可以使用事件的当前目标参数使用一个处理函数来完成这一切,以确定单击了哪个按钮:
function mouseDownHandler(event:Event):void {
var url:String;
//event.currentTarget is a reference to the object that you attached the event listener to
switch(event.currentTarget){
case button:
url = "https://website.com/";
break;
case button2:
url = "https://anotherwebsite.com/";
break;
default:
url = "https://yetanotherwebsite.com/";
}
navigateToURL(new URLRequest(url));
}
推荐阅读
- java - 在 Java 中如何引用在方法之后声明的变量?
- apache-spark - pyspark 中的 K-means 聚类算法:定义初始种子的语法
- python - 是否可以将 x 轴刻度与 matplotlib 直方图中的相应条对齐?
- python - 将 json 文件转换为 dataframe 解析嵌套的 json 数据
- java - Mockito 在 void 方法中捕获异常
- node.js - 如何在nodejs中向XML-WSDL中的服务发送请求
- fortran - 读取以空格分隔的整数
- php - 我安装了 guzzlehttp/guzzle 6.0,现在 laravel 护照不起作用
- docker - docker-compose 中 -h 的等价物是什么?
- python - 将参数作为 python 字典的成员传递