0

When I tried to build and run my game(based on Cocos2d 1.0.1, built in Xcode 4.5 with iOS 6.0 SDK) on iTouch 5, I found the CCMenuItems do not behave normally: when the menuitem is adjacent to screen edge, the edge border seems not so easy to be tapped inside to respond the touch event(Sorry for my poor expression).

To demonstrate the problem, I wrote a demo app with Xcode 4.3 using Cocos2d template and just modify the HelloWorldLayer's init method, and the phenomenon still happens. The code is below:

    -(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;
    }

I looked all over the internet and had no luck, wonder someone can help me with that. Many Thanks!

3
  • This could be a hardware limitation / bad calibration. You could make a small "display a crosshair on every touch point" application to verify. Nov 5, 2012 at 7:49
  • also think that this is hardware limitation. you can check that in all UIKit applications, that are native for iOS, there is no buttons right near the screen edge.
    – Morion
    Nov 5, 2012 at 9:51
  • Also I found it's easier to tap on the bottom-left corner of a menuitem no matter where i put it on screen than the top-right corner, is it my illusion or truly the system works like this? Nov 5, 2012 at 10:24

1 Answer 1

2

Just a few hunches:

  • Refrain from setting the sprites contentSizes. They should be set automatically, and may be used by CCMenu.
  • Make sure you don't change position or anchorPoint of CCMenuItem. Don't change anchorPoint of CCMenu either. This will mess up touch detection. Only use position property of CCMenu.
  • Make sure no other touch code elsewhere in your project interferes and may be swallowing touches. Gesture Recognizers can also cause CCMenu to misbehave.

If you want to freely position your menu items, make sure to wrap each in a CCMenu node. Then you can position the item via the menu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.