Cocos2d iphone CCPanZoomController&x2B;可触摸/可点击精灵

Cocos2d iphone CCPanZoomController&x2B;可触摸/可点击精灵,cocos2d-iphone,Cocos2d Iphone,我使用的是使我的“地图”(一幅图像)可缩放和平移。 在这张地图上,我希望有可点击/可触摸的精灵,当点击时会改变精灵中的图像 问题是,当用户挤压屏幕(缩小/缩小)时,他们可能会触摸精灵,这会改变精灵的图像,这是我不想要的 我有一个解决这个问题的想法,但由于我是Cocos2d新手,我不知道如何实现它: 我认为我可以通过检测用户第一次触摸屏幕时(将初始触摸转换为坐标),以及用户停止触摸屏幕时(将其转换为坐标),来检测用户何时触摸屏幕/精灵,并且不移动他们的触摸(好像捏或平移),然后比较两者,如果他们的

我使用的是使我的“地图”(一幅图像)可缩放和平移。 在这张地图上,我希望有可点击/可触摸的精灵,当点击时会改变精灵中的图像

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

我有一个解决这个问题的想法,但由于我是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");
    }
}

所以我自己也在游戏中与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");
    }
}

我找到了解决这个问题的简单方法。但是,它可能不适合您的需要

  • 我对CCMenu类进行了子类化,并覆盖了
    -(void)ccTouchMoved:(UITouch*)touch with event:(UIEvent*)事件
    ,如下所示:

    -(无效)ccTouchMoved:(UITouch*)触摸事件:(UIEvent*)事件 { [_selecteditemunselected]; _selectedItem=nil; }

  • 我已将该新菜单实例的
    touchWallow
    属性设置为
    NO


  • 我找到了解决这个问题的简单方法。但是,它可能不适合您的需要

  • 我对CCMenu类进行了子类化,并覆盖了
    -(void)ccTouchMoved:(UITouch*)touch with event:(UIEvent*)事件
    ,如下所示:

    -(无效)ccTouchMoved:(UITouch*)触摸事件:(UIEvent*)事件 { [_selecteditemunselected]; _selectedItem=nil; }

  • 我已将该新菜单实例的
    touchWallow
    属性设置为
    NO


  • 谢谢你的回复,不过这并不是我真正想要的——不过我以后可能会在我的游戏中使用它!没问题。最后一个注意事项……我忘了包含添加到CCPanZoomController类以清空数组的方法的代码。这很简单,只是以防万一,给你公共方法:当禁用临时触摸以在重新启用时正常工作时清除触摸阵列的自定义方法-(void)removeTouchesFromArray{//从触摸阵列[self.toucks removeAllObjects]中移除所有对象;}感谢您的回复,不过这并不是我真正想要的-不过我以后可能可以在游戏中使用它!没问题。最后一个注意事项……我忘了包含添加到CCPanZoomController类以清空数组的方法的代码。这很简单,只是以防万一,给你公共方法:当禁用临时触摸以在重新启用时正常工作时清除触摸阵列的自定义方法-(void)removeTouchesFromArray{//从触摸阵列[self.toucks removeAllObjects];}