2014-08-28 33 views
0

我目前正在編程一種點&點擊AS3中的遊戲與Adobe Flash CS5。當光標改變時點擊不被識別

每當我點擊一扇門時,我想檢查一下是否按住了鑰匙以解鎖它。當從清單中選擇鍵時,我想要光標(不是原始光標,但是與startDrag鏈接的特定光標)獲取該鍵的圖標。

這裏是我的代碼:

var selectedKey:Boolean = false; 

    key_obj.addEventListener(Mouse.CLICK, selectKey); 
    door.addEventListener(Mouse.CLICK, openDoor); 
    inventory_spot.addEventListener(Mouse.CLICK, drop);//send back key to inventory 


    function selectKey(e:MouseEvent):void 
    { 
    cursor.stopDrag(); 
    removeChild(cursor); //disable the old cursor style 
    key_obj.removeEventListener(Mouse.CLICK, selectKey); 
    key_obj.startDrag(true); 
    selectedKey = true; 

    addChild(inventory_spot); 
    } 

    function openDoor(e:MouseEvent):void 
    { 
    if (selectedKey) 
     // open the door 
    else 
     // error : you don't have the key 
    } 

    function drop(e:MouseEvent):void 
    { 
    key_obj.stopDrag(); 
    key_obj.addEventListener(Mouse.CLICK, selectKey); 
    selectedKey = false; 
    addChild(cursor); // enable the old cursor 
    cursor.startDrag(true); 
    key_obj.x = inventory_spot.x [...] // position of the key in the inventory 
    key_obj.y = inventory_spot.y [...] 
    removeChild(inventory_spot); 
    } 

這裏是我的問題:當我門上的鑰匙光標點擊,實際上該計劃甚至不叫openDoor()

什麼也沒有發生,但一旦我放棄關鍵回到庫存,並得到舊的光標,然後openDoor()工作得很好。

我不明白,函數不是因爲我改變了光標而被調用的?

感謝您的幫助

回答

0

因爲點擊很可能將你的光標鍵代替了門。要解決這個問題,你想讓你的遊標mouseEnabled = false和mouseChildren = false。

我使用以下來檢查點擊事件的傳播。因此,在您的示例中,您可以檢查最初點擊的內容以及事件傳播的位置。 「永遠不會被調用的問題」通常是因爲你認爲被點擊的東西不是。

所以補充一點:

stage.addEventListener(MouseEvent.CLICK,Functions.checkMouseEventTrail,false,0,true); 

要調用這個:

function checkMouseEventTrail($e:MouseEvent):void{ 
// displays the mouse event trail for any selected item 
var p:* = $e.target; 
trace("\nMOUSE EVENT TRAIL\n time: "+getTimer()+" type: "+$e.type+" target: "+$e.target.name+" cur target: "+$e.currentTarget); 
var spacing:String=""; 
while (p){ 
    var curFrame = "none"; 
    if (p is MovieClip){curFrame = String(p.currentFrame);} 
    trace(spacing+">>", p.name,": ",p+" vis: "+p.visible+" loc: "+p.x+","+p.y+" width: "+p.width+" height: "+p.height+" frame : "+curFrame+" class: "+getClassName(p)); 
    if (p != null){p = p.parent;} 
    spacing+=" "; 
} 
trace(""); 

}

,你會在什麼發生在您的單擊事件的輸出中看到。

+0

你說得對,mouseEnabled = false解決了一切。我的key_obj光標的註冊點是movieClip的中心,也許這就是爲什麼每次點擊都是按鍵的原因,而我的其他自定義箭頭狀光標沒有出現該問題?無論如何,問題解決了,謝謝 – 2014-08-28 17:16:41