2013-05-25 49 views
7

我使用組來存儲一些圖像並將它們繪製到SpriteBatch。現在我想檢測點擊了哪個圖像。出於這個原因,我將一個InputListener添加到組中以觸發事件。傳入的InputEvent獲得了一個方法(getTarget),該方法返回對點擊的Actor的引用。對具有重疊透明圖像的組的輸入檢測

如果我點擊一個Actor的透明區域,我想忽略傳入的事件。如果後面有一個Actor,我想用它來代替。我想過這樣的事情:

myGroup.addListener(new InputListener() { 
     @Override 
     public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { 
      Actor targetActor = event.getTarget(); 
      // is the touched pixel transparent: return false 
      // else: use targetActor to handle the event and return true 
     }; 
    }); 

這是正確的方法嗎?我認爲返回假的方法touchDown將繼續傳播的事件,並讓我也收到touchDown事件的其他角色在同一位置。但是,這似乎是一場誤會......

UPDATE

P.T.s回答解決獲得正確的事件的問題。現在我已經有了這個問題來檢測命中像素是否透明。在我看來,我需要將圖像作爲Pixmap來訪問。但我不知道如何將圖像轉換爲Pixmap。我也懷疑這是否是一個很好的解決方案,在性能和內存使用方面..

+0

你不會從圖像到Pixmap。你從Pixmap到紋理到圖像可繪製。 – NateS

+0

您是否嘗試過我提供的解決方案? – ManishSB

回答

5

我想你想重寫Actor.hit()方法。請參閱scene2d Hit Detection wiki

要做到這一點,子類Image,並把你的hit專業化在那裏。喜歡的東西:

public Actor hit(float x, float y, boolean touchable) { 
    Actor result = super.hit(x, y, touchable); 
    if (result != null) { // x,y is within bounding box of this Actor 
    // Test if actor is really hit (do nothing) or it was missed (set result = null) 
    } 
    return result; 
} 

我相信你不能touchDown偵聽器內做到這一點,因爲舞臺將會跳過了演員「背後的」這一個(唯一的「家長」的演員,如果你返回false在這裏將得到觸下事件) 。

相關問題