当我尝试在 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/