Javascript 如何在easeljs0.7.0中处理事件调度

Javascript 如何在easeljs0.7.0中处理事件调度,javascript,events,easeljs,Javascript,Events,Easeljs,一直在看easeljs,似乎对什么是进行活动授权的最佳方式感到困惑。以下是我正在尝试的: function init() { var canvas = document.getElementById('canvas'); var stage = new createjs.Stage(canvas); var txt1 = new createjs.Text('Test With Hit Area', '24px Arial', '#0000ff'); txt1.

一直在看easeljs,似乎对什么是进行活动授权的最佳方式感到困惑。以下是我正在尝试的:

function init() {
    var canvas = document.getElementById('canvas');
    var stage = new createjs.Stage(canvas);

    var txt1 = new createjs.Text('Test With Hit Area', '24px Arial', '#0000ff');
    txt1.x = 50;
    txt1.y = 50;
    console.log('txt1', txt1);
    var hit = new createjs.Shape();
    hit.graphics.beginFill('#000000').drawRect(500, 50, txt1.getMeasuredWidth(), txt1.getMeasuredHeight())
    txt1.hitArea = hit;
    // neither seem to work with a hitArea
    // txt1.addEventListener('click', handleClick);
    txt1.on('click', handleClick);

    var txt2 = new createjs.Text('Test Without Hit Area', '24px Arial', '#0000ff');
    txt2.x = 50;
    txt2.y = 80;
    console.log('txt2', txt2);
    // both addEventListener() and on() work fine without hit area
    // txt2.addEventListener('click', handleClick);
    txt2.on('click', handleClick);


    stage.addChild(txt1, txt2);
    stage.update();
}

function handleClick() {
    console.log('clicked', this);
}

init();

我在这里创建了一个简单的JSFIDLE来演示我的尝试:

命中区域将自动与形状对齐,因此您应该将矩形的x&y设置为0,0

hit.graphics.beginFill('#000000')
    .drawRect(0, 0, txt1.getMeasuredWidth(), txt1.getMeasuredHeight())

我打开mouseover并设置光标,这样更容易显示碰撞区域。
干杯。

太好了!非常感谢这个兰尼。我自己永远也不会发现这一点。OOC,你知道这是否在文档中的任何地方吗?是的,这是在“hitArea”属性中记录的-尽管可能更清楚。