Animate ActionScript 3.0中的按钮超链接

时间:2018-05-28 09:50:47

标签: actionscript-3 adobe-animate

我尝试使用ActionScript在Adobe Animate中制作三个按钮。以下是我用于按钮1的代码:

struct DefaultsKey<ValueType> {
  private let key: String
  private let defaultValue: ValueType

  public init(_ key: String, defaultValue: ValueType) {
    self.key = key
    self.defaultValue = defaultValue
  }

  var value: ValueType {
    get {
      let value = UserDefaults.standard.object(forKey: key)
      return value as? ValueType ?? defaultValue
    }
    set {
      UserDefaults.standard.setValue(newValue, forKey: key)
      UserDefaults.standard.synchronize()
    }
  }
}

class AppUserDefaults {
  private var _username = DefaultsKey<String>("username", defaultValue: "Unknown")
  var username: String {
    set {
      _username.value = newValue
    },
    get {
      return _username.value
    }
  }
}

let props = AppUserDefaults()
props.username = "bla"
print("username: \(props.username)")

(这些URL仅用于演示目的。)正如您所见,按钮2和3的代码完全相同,但在不同的图层上。我还更改了开始时的实例名称,函数名称和每个按钮的URL。但是当我按下CTRL + Enter时,所有按钮都会指向同一个网页(在这种情况下为&#34; website.com&#34;),这是我首先放入的网页。它应该导致我输入的不同的URL,但它们都只是去同一个。为什么会发生这种情况?如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

The problem is, although you've defined 3 separate handler functions for the 3 separate buttons, you are attaching the first handler function to all 3 buttons:

button. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
button2. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
button3. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);

What you mean to do is this:

button. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
button2. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler2);
button3. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler3);

Though, as an aside, you could do it all with one handler function using the event's current target parameter to figure which button was clicked:

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));
}