我正在使用CCPanZoomController使我的“地图”(一张图片)可以缩放和平移。 在这张地图上,我希望有可点击/可触摸的精灵,点击它时会改变精灵中的图像。
问题是当用户捏住屏幕(缩小/缩小)时,他们可能会触摸精灵,这会改变精灵的图像,这是我不想要的。
我有一个想法来解决这个问题,但由于我是Cocos2d的新手,我不知道如何实现它: 我认为我可以检测用户何时触摸屏幕/精灵,并且通过检测用户第一次触摸屏幕时不会移动他们的触摸(就像捏或平移)(将初始触摸转换为坐标) ,然后当用户停止触摸屏幕(将其转换为坐标),并比较两者,如果它们没有变化(或变化很小),那么改变精灵的图像?
我该怎么做呢?非常感谢任何可以帮助的人!!
答案 0 :(得分: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");
}
}
答案 1 :(得分:0)
我找到了解决这个问题的简单方法。但它可能不适合您的需求!
我已经将CCMenu类子类化并覆盖了-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
,如下所示:
- (void)ccTouchMoved:(UITouch *)触及withEvent:(UIEvent *)事件 { [_selectedItem unselected]; _selectedItem = nil; }
我已将该新菜单实例的touchSwallow
属性设置为NO
。