gpt4 book ai didi

cocoa - 滑动 CCMenu

转载 作者:行者123 更新时间:2023-12-03 16:19:41 29 4
gpt4 key购买 nike

我有一个包含 CCMenuItemImage 的菜单(“myMenu”)。我希望此菜单能够检测手指滑动并相应地滑动。

我的问题是 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

最佳答案

通过添加解决它:

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

问题是 CCMenuItemImage 吞掉了触摸并且将高优先级设置为 -128。因此需要将优先级设置为INT_MIN+1

关于cocoa - 滑动 CCMenu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11921491/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com