2012-08-12 211 views
3

我有一個包含CCMenuItemImage s的菜單(「myMenu」)。我希望這個菜單檢測手指滑動並相應滑動。滑動CC菜單

我的問題是,CCMenuItemImage似乎吸收觸摸事件。當用戶觸摸CCMenuItemImages之外的菜單時,滑動效果很好,但當觸摸發生在這些滑動時,滑動效果不佳。

我試圖把我的菜單項放在一個圖層來檢測觸摸(參考答案Scrollable menu using MenuItem's),但這似乎也不工作。任何想法爲什麼?

+(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 

回答

3

加入解決了這個問題:

-(void) registerWithTouchDispatcher 
{ 
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN+1 swallowsTouches:NO]; 
} 

的問題是,CCMenuItemImage燕子的接觸和具有高優先級設置爲-128。因此需要設置優先級INT_MIN+1

+0

你在哪裏添加了這段代碼?我也堅持同樣的觀點? – user1201239 2012-09-19 03:48:35

+0

在包含CCMenuItemImages的CCScene類中 – David 2012-09-19 16:01:08

+0

Thanks dude !!! – user1201239 2012-09-20 09:08:31