2011-04-06 42 views
0

我正在使用圖像作爲光標的應用程序。現在我想知道光標在哪個對象上徘徊。有點像HitTestObject(*),然後我可以看到*代表什麼對象。有沒有人有任何想法我可以做到這一點? (並且使用鼠標不是選項)AS3:HitTest任何對象

+0

你是否真的需要用圖像本身進行測試?鼠標仍然在那裏,你只是看不到它。你可以測試/ mouseOver與該 – divillysausages 2011-04-06 14:39:16

+0

鼠標不存在,這就是問題所在;)我使用不同的方法來控制圖像:) – 2011-04-06 14:53:34

+0

你是如何控制光標? – divillysausages 2011-04-06 15:09:42

回答

1

我已經解決了這個問題:)因爲光標是在不同的精靈比別人,我有這樣做,因爲我無法將對象傳遞到數組中。

 //First we will create a point that contains the x and y of this cursor. 
     var _position:Point = new Point(x + (width/2), y + (height/2)); 

     //Secondly, we will get an array of elements that are under this point. 
     var _objects:Array = parentApplication.getObjectsUnderPoint(_position); 

     //If the length of the objectsList is longer than or equal to 2, we may assume that 
     //there is an object 
     if(_objects.length >= 2) 
     { 
      //Set the currentObject variable to the object the cursor is hovering over. 
      //The minus two is simple. The cursor is always the last object under that point, 
      //so we need the object before that. 
      _currentObject = _objects[_objects.length - 2]; 

      //dispatch the event in the object. 
      dispatchCursorEventToObject(EyeEvent.CURSOROVER); 
     } 
1

將要監視的元素放置在單獨的陣列中,然後向連接到鼠標的對象添加onEnterFrame偵聽器,該對象遍歷數組並執行hitTests與每個對象。

var hitTestClips:Array; 
// populate hitTestClips with the items you want to hitTest 

,這會轉變的onEnterFrame處理您的鼠標連接的對象:

for(var item:MovieClip in hitTestClips) 
{ 
    if(item.hitTest(this.x, this.y, true)) 
    { 
    trace('now hovering above ' + item); 
    } 
} 
+0

我曾嘗試過,但無法使用它,因爲一些對象不是光標的子項:)謝謝反正 – 2011-04-06 15:01:52