我正在尝试创建一个精灵,如果精灵被点击,它会打印出一条消息。我创建了一个精灵,但当我添加一个侦听器时,它会给我错误:
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();
}
}发布于 2015-12-24 19:15:42
侦听器函数签名必须为Dynamic -> Void,因为单击时会将Event (或MouseEvent)对象作为参数传递。
所以应该是这样的:
public function click(e:Dynamic) {
trace('test');
}Flash模仿了Flash,因此addEventListener函数的工作方式与这里介绍的非常相似:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/EventDispatcher.html#addEventListener()
https://stackoverflow.com/questions/34451025
复制相似问题