2012-08-22 60 views
1

我正在使用CCPanZoomController來使我的'地圖'(一個圖像)可縮放和平移。 在這張地圖上,我希望有可點擊/可觸摸的精靈,點擊後可以更改精靈中的圖像。CCPanZoomController +可觸摸/可點擊的精靈

問題是當用戶捏住屏幕(縮小/放大)時,他們可能會觸摸精靈,這會改變精靈的圖像,這是我不想要的。

我有一個想法來解決這個問題,但因爲我是Cocos2d的新手,我不知道如何實現它: 我認爲我可以檢測到用戶觸摸屏幕/精靈時,通過檢測用戶何時第一次觸摸屏幕(將初始觸摸轉換爲座標),然後當用戶停止觸摸屏幕(將其轉換爲座標)時,移動他們的觸摸(如同捏或平移),以及比較兩者,如果它們沒有變化(或變化很小),那麼更改精靈的圖像?

我該如何去做這件事?非常感謝任何能夠幫助的人!

回答

1

所以我一直在我的遊戲中使用CCPanZoomController,並且遇到類似的問題,但有很多不同的方面,比如當他們觸摸一個精靈時,我不想讓背景移動或者我當背景縮放時,想讓精靈不移動。所以我所做的就是製作方法來「關閉」那些我不想做出反應的圖層,並在其他圖層的動作完成後重新啓用它們。

我在每個圖層內創建了以下方法來禁用它或啓用它用於從不同的觸摸事件調用的觸摸。

// Public Method: Allows for disabling touch for this layer and re-enabling it 
-(void)enableTouches:(BOOL)enable 
{ 
    // Check if the bool value is to enable or disable touches 
    if (enable) { 
     // Call for the removal of all touch locations in array in the CCLayerPanZoom instance 
     [_panZoomLayer removeTouchesFromArray]; 

     // Call the touch dispatcher and add the CCLayerPanZoom back as a delegate for touches 
     [[CCTouchDispatcher sharedDispatcher] addStandardDelegate:_panZoomLayer priority:0]; 

     CCLOG(@"PanZoomWrapperLayer:enableTouches - LayerPanZoom touches enabled"); 

    } else { 

     // Call the touch dispatcher to remove the CCLayerPanZoom as a delegate to disable touches 
     [[CCTouchDispatcher sharedDispatcher] removeDelegate:_panZoomLayer]; 

     CCLOG(@"PanZoomWrapperLayer:enableTouches - LayerPanZoom touches disabled"); 
    } 
} 
+0

感謝您的回覆,但是這並不是我真正想要的 - 我可能會在以後的遊戲中使用它! – Harry

+0

沒問題。最後的筆記...我忘了包括我添加到CCPanZoomController類的方法的代碼來清空數組。這很簡單,但以防萬一,在這裏你去。 // Public Method:自定義方法,用於在禁用觸摸時臨時清除觸摸陣列,以便在重新啓用時正常工作。 - (void)removeTouchesFromArray { //刪除觸摸數組中的所有對象 [self.touches removeAllObjects]; } – XBXSlagHeap

0

我已經找到了解決這個問題的簡單方法。但它可能不適合您的需求!

  1. 我子類的CCMenu類,並覆蓋了-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event如下:

    - (空)ccTouchMoved:(UITouch *)觸摸withEvent:方法(*的UIEvent)事件 { [_selectedItem未選]; _selectedItem = nil; }

  2. 我設置新菜單的實例的touchSwallow屬性NO