ios - 当 ccmenuitem 靠近屏幕边缘放置时,其触摸精度较低

标签 ios cocos2d-iphone ios6 iphone-5 ccmenuitem

当我尝试在 iTouch 5 上构建和运行我的游戏(基于 Cocos2d 1.0.1,内置于带有 iOS 6.0 SDK 的 Xcode 4.5 中)时,我发现 CCMenuItems 行为不正常:当菜单项与屏幕边缘相邻时, edge border 似乎不太容易在里面被点击以响应触摸事件(抱歉我的表达不佳)。

为了演示这个问题,我用Cocos2d模板用Xcode 4.3写了一个demo app,修改了HelloWorldLayer的init方法,还是出现了这个现象。代码如下:

    -(void) init
    {
            // always call "super" init
            // Apple recommends to re-assign "self" with the "super" return value
            if( (self=[super init])) {

            CCLayerColor *cl = [CCLayerColor layerWithColor:ccc4(ccWHITE.r, ccWHITE.g, ccWHITE.b, 255)];
            [self addChild:cl];
                    // create and initialize a Label
                    CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];

                    // ask director the the window size
                    CGSize size = [[CCDirector sharedDirector] winSize];

                    // position the label on the center of the screen
                    label.position =  ccp( size.width /2 , size.height/2 );

                    // add the label as a child to this Layer
                    [self addChild: label];

                    float width = 160;

                    CCSprite *sp1 = [CCSprite node];
                    [sp1 setContentSize:CGSizeMake(width, width)];
                    [sp1 setTextureRect:CGRectMake(0, 0, width, width)];
                    [sp1 setColor:ccc3(0xff, 0xff, 0)];

                    CCSprite *sp2 = [CCSprite node];
                    [sp2 setContentSize:CGSizeMake(width, width)];
                    [sp2 setTextureRect:CGRectMake(0, 0, width, width)];
                    [sp2 setColor:ccc3(0, 0, 0xff)];

                    CCMenuItemSprite *button = [CCMenuItemSprite itemFromNormalSprite:sp1 selectedSprite:sp2 target:nil selector:nil];
                    CCMenu *menu = [CCMenu menuWithItems:button, nil];
                    [self addChild:menu];
                    menu.position = ccp(0, 0);
                    button.anchorPoint = ccp(1, 1);
                    button.position = ccp([[CCDirector sharedDirector] winSize].width,
                                  [[CCDirector sharedDirector] winSize].height);


            }
            return self;
    }

我在网上找遍了,但没有运气,想知道有人可以帮我解决这个问题。非常感谢!

最佳答案

一些预感:

  • 不要设置 sprite 的 contentSizes。它们应自动设置,并可由 CCMenu 使用。
  • 确保您没有更改 CCMenuItem 的位置或 anchor 。也不要更改 CCMenu 的 anchorPoint。这会搞乱触摸检测。仅使用 CCMenu 的位置属性。
  • 确保项目中其他地方没有其他触摸代码干扰并且可能会吞噬触摸。手势识别器也可能导致 CCMenu 行为异常。

如果您想自由放置菜单项,请确保将每个菜单项都包装在一个 CCMenu 节点中。然后您可以通过菜单定位项目。

关于ios - 当 ccmenuitem 靠近屏幕边缘放置时,其触摸精度较低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13227966/

相关文章:

ios - 验证失败时防止光标移动

iphone - 在两个 Sprite 之间创建距离缓冲区

ios - cocos2d ios 应用程序崩溃且没有错误 - 意外的 dealloc

ios - 如何在 UIBarButtonItem 显示后将自定义 View 移动到不同的 super View ?

ios - 使透明的stickyHeader像天气iOS一样

ios - 获取 iOS 上导航栏的默认高度(或最大值)

ios - UIImage.pngData(_ :) losing Camera photo orientation

ios - Cocos2d 边界框保持为零

ios - CBPeripheralManager 限制

iphone - iOS 7是否支持为旧版iOS开发的应用程序