首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >滑动CCMenu

滑动CCMenu
EN

Stack Overflow用户
提问于 2012-08-12 18:25:36
回答 1查看 577关注 0票数 3

我有一个包含CCMenuItemImages的菜单("myMenu")。我希望这个菜单能够检测手指滑动并相应地滑动。

我的问题是,CCMenuItemImage似乎吸收了触摸事件。当用户触摸CCMenuItemImages外部的菜单时,滑动效果很好,但当触摸发生在这些菜单上时,滑动效果就不好了。

我试着把我的菜单项放在一个层中来检测触摸(参考回答Scrollable menu using MenuItem's),但这似乎也不起作用。知道为什么吗?

代码语言:javascript
复制
+(id) scene
{
    CCScene *scene = [CCScene node];
    ModeMenuScene *layer = [ModeMenuScene node];
    [scene addChild: layer];
    return scene;
}

-(id) init
{
    if( (self=[super init] )) {


        CGSize winSize = [[CCDirector sharedDirector] winSize];
        CCSprite *background = [CCSprite spriteWithFile:@"bg.png"];
        background.position=ccp(winSize.width/2,winSize.height/2);
        [self addChild:background];

        mode1 = [CCMenuItemImage itemFromNormalImage:@"Mode1.png" selectedImage: @"Mode1.png" target:self selector:@selector(goToMode1:)];
        mode1label = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Level 1 %d", n] dimensions:CGSizeMake(220,53) alignment:UITextAlignmentCenter fontName:@"Arial" fontSize:20.0];
        mode1label.color = ccc3(167,0,0);
        mode1label.position=ccp(55,-30);
        [mode1 addChild:mode1label];

    // here same kind of code to define mode2,mode3,mode4 (taken out to reduce size of code)

        myMenu=[CCMenu menuWithItems:mode1,mode2,mode3,mode4,nil];
        [myMenu alignItemsHorizontallyWithPadding:25];
        myMenu.position=ccp(winSize.width/2+40,180);
        menuLayer = [CCLayer node];
        [menuLayer addChild:myMenu];
        [self addChild:menuLayer];

        [self enableTouch];

    }
    return self;
}

-(void) disableTouch{
    self.isTouchEnabled=NO;
    menuLayer.isTouchEnabled=NO;
}

-(void) enableTouch{
    self.isTouchEnabled=YES;
    menuLayer.isTouchEnabled=YES;
}


-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    if(location.y>100 && location.y<260) {
        draggingMenu=1;
        x_initial = location.x;
    }
    else draggingMenu=0;
}


-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    if(draggingMenu==1) {
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        int x = myMenu.position.x+location.x-x_initial;
        x = MAX(0,x);
        x = MIN(x,winSize.width/2+40);
        myMenu.position=ccp(x,180);
        x_initial=location.x;
    }
}

- (void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    draggingMenu=0;
}


- (void)dealloc {
    [super dealloc];
}

@end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-13 20:47:03

通过添加以下内容解决了此问题:

代码语言:javascript
复制
-(void) registerWithTouchDispatcher
{
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN+1 swallowsTouches:NO];
}

问题是CCMenuItemImage接受了触摸,并将高优先级设置为-128。因此,需要在INT_MIN+1上设置优先级

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11921491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档