CCPanZoomController +可触摸/可点击的精灵

问题描述:

我正在使用CCPanZoomController来使我的'地图'(一个图像)可缩放和平移。 在这张地图上,我希望有可点击/可触摸的精灵,点击后可以更改精灵中的图像。CCPanZoomController +可触摸/可点击的精灵

问题是当用户捏住屏幕(缩小/放大)时,他们可能会触摸精灵,这会改变精灵的图像,这是我不想要的。

我有一个想法来解决这个问题,但因为我是Cocos2d的新手,我不知道如何实现它: 我认为我可以检测到用户触摸屏幕/精灵时,通过检测用户何时第一次触摸屏幕(将初始触摸转换为坐标),然后当用户停止触摸屏幕(将其转换为坐标)时,移动他们的触摸(如同捏或平移),以及比较两者,如果它们没有变化(或变化很小),那么更改精灵的图像?

我该如何去做这件事?非常感谢任何能够帮助的人!

所以我一直在我的游戏中使用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

我已经找到了解决这个问题的简单方法。但它可能不适合您的需求!

  1. 我子类的CCMenu类,并覆盖了-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event如下:

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

  2. 我设置新菜单的实例的touchSwallow属性NO