2013-03-05 47 views
4

我的Flash項目中有StaticText字段,當鼠標懸停在它們上方時,我需要運行一些代碼。所以我試過這個代碼如何檢測AS3中的StaticText?

stage.addEventListener(MouseEvent.MOUSE_OVER, mouseRollOver); 
    function mouseRollOver(event:MouseEvent):void { 
    var tf:StaticText = event.target as StaticText; 
    if (tf){ 
    //my code 
    } 
} 

但它不起作用。當我使用動態文本字段並在var tf中用TextField替換StaticText時,它工作正常。我還認爲,如果我可以讓鼠標檢測到不是StaticText作爲目標,但是某種具有某些文本屬性的對象(如「selectable」設置爲true),我可以使用靜態文本字段來處理這個事情,但是我不能'弄清楚如何做到這一點。無論如何,我需要以某種方式檢測靜態文本字段作爲目標。任何幫助,將不勝感激。
在此先感謝

+1

最簡單的方法:將所有textfirlds封裝在movieclip中(全部在一個或一個),並使用它。您可以使用DisplayObject的hitTest函數檢測碰撞 – Smolniy 2013-03-05 14:40:21

+0

您可以使用[TextSnapshot](http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/text/TextSnapshot.html)類來處理靜態文本字段,但它們只是只讀的,不能設置文本。你仍然可以使用[hitTestTextNearPos](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextSnapshot.html#hitTestTextNearPos()) – 2013-03-05 14:54:48

回答

2

您最好的選擇是將靜態文本框放入動畫片段,然後爲其分配您的代碼。靜態文本框沒有實例名稱,無法操作。

0

這很難做到。看到這個鏈接enter link description here 正如你所看到的,你可以檢查DisplayObject是否爲StaticText,以及通過檢查mousX和MouseY屬性,你可以發現翻轉是否與這個字段有關。通過如果你使用動態文本,並取消選擇的領域你會得到作爲StaticField

編輯 這是解釋我的意思是一個文本框: 讓我們有一個靜態文本字段成黑色Flash文檔中的階段。

var myFieldLabel:StaticText 
var i:uint; 

//This for check for all staticFields in state and trace its text. It is possible and it is working. I my case I have only one field and I get reference to it in myFieldLabel:StaticText var. Also I change it's alpha to 0.3. 
for (i = 0; i < this.numChildren; i++) 
{ 
var displayitem:DisplayObject = this.getChildAt(i); 
if (displayitem instanceof StaticText) { 
    trace("a static text field is item " + i + " on the display list"); 
    myFieldLabel = StaticText(displayitem); 

    trace("and contains the text: " + myFieldLabel.text); 
    trace(myFieldLabel.mouseX); 
    myFieldLabel.alpha = 0.3; 
} 
} 

//Adds event listener to the stage for mouse move event 
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseRollOver); 

//This is an event handler. I check if the mouse position is within the static field 
function mouseRollOver(evnt:MouseEvent):void 
{ 
if (0 <= myFieldLabel.mouseX && myFieldLabel.mouseX <= myFieldLabel.width && 0 <= myFieldLabel.mouseY && myFieldLabel.mouseY <= myFieldLabel.height) 
{ 
    mouseOverStaticText(evnt) 
} 
else 
{ 
    mouseNotOverStaticText(evnt) 
} 
} 

// this two methods change the static field alpha. Thay are only to show that is posible to detect and manipulate some properties of the StaticField. 
function mouseOverStaticText(evnt) 
{ 
myFieldLabel.alpha = 1; 
} 
function mouseNotOverStaticText(evnt) 
{ 
myFieldLabel.alpha = 0.3; 
} 

我不確定管理StaticText字段的目的是什麼。如果您必須執行某些操作,則StaticText不是設計來管理的,這幾乎可以確定該字段不能是靜態的 - 它們可以是動態的(不具有可選屬性),也可以使用MovieClip封裝,或者可以有不同的解決方案案件。

+0

這個答案很混亂。因爲StaticText不在InteractiveObject的繼承鏈中,所以沒有鼠標事件可以響應。使用'DisplayObject.mouseX'和/或'DisplayObject.mouseY'並沒有什麼意義,因爲'mouseX'和'mouseY'提供的位置信息與相關DisplayObject的原點有關。你可以找到這種方式是:鼠標相對於我的DisplayObject(在這種情況下是靜態文本)。 – 2013-03-07 00:00:52

+0

所以我更新我的答案。你說得對,靜態文本不是繼承InteractiveObject,不能關注事件,但是staticText繼承DisplayObject,你可以根據這個字段獲得Mouse的相對位置。你需要的是一個能夠引發鼠標事件的對象(在這種情況下是舞臺)。在事件的處理程序中,您必須檢查鼠標X和Y的位置。在我的示例中,mouseRollOver處理程序中的「if」會執行此操作。如果光標在靜態字段上或不在靜態字段中,您將看到靜態字段的字母變化。 – 2013-03-07 08:55:28