2013-11-21 44 views
0

一直在尋找畫架,並且似乎對什麼是關於事件委派的最佳方式感到困惑。這裏就是我想:如何處理畫架中的事件派發0.7.0

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

我創建了一個簡單的jsfiddle在這裏展示我的嘗試:http://jsfiddle.net/brrWn/1/

回答

1

的hitArea將自動隨形排列,所以你應該設置x &ÿ的矩形爲0,0。

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

http://jsfiddle.net/lannymcnie/brrWn/2/

我打開鼠標懸停,並設置光標,這使得它更容易顯現hitArea。 乾杯。

+0

完美!非常感謝這個Lanny。我從來沒有發現過我自己。 OOC,你知道這是否在任何地方的文檔? –

+0

是的,這是記錄在「hitArea」屬性 - 雖然也許它可以更清楚。 http://www.createjs.com/Docs/EaselJS/classes/DisplayObject.html#property_hitArea – Lanny