[英]Haxe/OpenFL - Check if sprite is clicked
I am trying to create a sprite and if the sprite is clicked it prints out a message. 我正在尝试创建一个精灵,如果单击该精灵,它将打印出一条消息。 I created a sprite but when I added a listener it gives me the errors:
我创建了一个精灵,但是当我添加一个侦听器时,它给了我错误:
src/Main.hx:26: characters 39-44 : Void -> Void should be Dynamic -> Void
src/Main.hx:26: characters 39-44 : For function argument 'listener'
I removed the listener and then it worked just fine what is the problem? 我删除了侦听器,然后工作正常,这是什么问题?
My main class: 我的主班:
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");
}
} }
my Sprite class: 我的雪碧课:
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();
}
}
The listener function signature must be Dynamic -> Void
, because an Event
(or MouseEvent
) object will be passed as argument when you click. 侦听器函数签名必须是
Dynamic -> Void
,因为单击时将传递Event
(或MouseEvent
)对象作为参数。
Thus it should be like that: 因此应该是这样的:
public function click(e:Dynamic) {
trace('test');
}
The OpenFL API mimics Flash's, and thus the addEventListener
function works pretty much like documented here : http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/EventDispatcher.html#addEventListener() OpenFL API模仿Flash,因此
addEventListener
函数的工作原理与此处记录的非常相似: http : //help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/events/EventDispatcher.html#addEventListener()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.