【问题标题】:Haxe/OpenFL - Check if sprite is clickedHaxe/OpenFL - 检查精灵是否被点击
【发布时间】:2015-12-24 10:09:15
【问题描述】:

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

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

}

【问题讨论】:

    标签: haxe openfl


    【解决方案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()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      • 2014-01-08
      • 2013-05-09
      相关资源
      最近更新 更多