Haxe / OpenFL - 检查是否点击了精灵

时间:2015-12-24 10:09:15

标签: haxe openfl

我正在尝试创建一个精灵,如果点击精灵,它会打印出一条消息。我创建了一个精灵,但是当我添加一个监听器时,它给了我错误:

src/Main.hx:26: characters 39-44 : Void -> Void should be Dynamic -> Void
src/Main.hx:26: characters 39-44 : For function argument 'listener'

我删除了监听器然后它工作得很好问题是什么?

我的主要课程:

package;


import openfl.display.Sprite;
import openfl.events.MouseEvent;
import openfl.display.SimpleButton;


class Main extends Sprite {

private var button:SimpleButton;
private var s:Spritetest;

public function new () {
    super ();

    this.mouseChildren = false;
    this.buttonMode = true;

    init();
}

public function init() {
    fillBackGround(0xff00ff, 640, 960);
    s = new Spritetest();
    s.addEventListener(MouseEvent.CLICK, click);
    addChild(s);

}

public function fillBackGround(color:Int, w:Int, h:Int) {
    this.graphics.beginFill(color);
    this.graphics.drawRect(0, 0, w, h);
    this.graphics.endFill();
}

public function click() {
    trace("test");
}

}

我的Sprite类:

package;

import openfl.display.Sprite;

class Spritetest extends Sprite {

public function new() {
    super();
    this.graphics.beginFill(0xffffff);
    this.graphics.drawRect(20 , 20, 40, 40);
    this.graphics.endFill();
}

}

1 个答案:

答案 0 :(得分:1)

侦听器函数签名必须为Dynamic -> Void,因为当您单击时,Event(或MouseEvent)对象将作为参数传递。

因此它应该是这样的:

public function click(e:Dynamic) {
   trace('test');
}

OpenFL API模仿Flash,因此addEventListener函数的工作原理与此处记载的非常相似: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/EventDispatcher.html#addEventListener()