事件处理 – 如何在cocos2d中的模态层下停止所有CCTouch?

事件处理 – 如何在cocos2d中的模态层下停止所有CCTouch?,第1张

概述在我的cocos2d游戏中,我有一个“设置”按钮,它启动一个模态层,用于锁定其下的所有内容.为此,我使用菜单状态方法的组合锁定所有CCMenuItems并使用覆盖层;两者都在代码中. 问题是这两种解决方案似乎都不适用于CCScrollLayers.当我单击按钮(启动模态)时,仍然可以滚动CCScrollLayer,这不是我想要的. 我想: >按下禁用按钮ALL触摸并禁用包括CCScrollLaye 在我的cocos2d游戏中,我有一个“设置”按钮,它启动一个模态层,用于锁定其下的所有内容.为此,我使用菜单状态方法的组合锁定所有CcmenuItems并使用覆盖层;两者都在代码中.

问题是这两种解决方案似乎都不适用于CCScrollLayers.当我单击按钮(启动模态)时,仍然可以滚动CCScrollLayer,这不是我想要的.

我想:

>按下禁用按钮ALL触摸并禁用包括CCScrollLayers在内的所有元素
>启动模态(仅允许触摸模态)

我试过了:

>使用touch使用CCTargetedtouchDelegate吞下所有触摸

[[CCtouchdispatcher shareddispatcher] addTargetedDelegate:self priority:0 swallowstouches:YES];

>我试过了

self.istouchEnabled =启动模态的图层上的NO

>我已经尝试调整MenuStatus方法,以便它适用于CCScrollLayers但它似乎不起作用.

我不确定我做错了什么.我的代码如下.

// My main layer which launches the Settings Modal Layer#pragma mark - Lock/Unlock layers-(voID) doSettings{        [self lockLayers];    SettingsModalLayer *sml = [[[SettingsModalLayer alloc] init] autorelease];    [sml showSettingsOnLayer:self closeBlock:^{[self unlockLayers];}];}-(voID) lockLayers{    [[CCtouchdispatcher shareddispatcher] addTargetedDelegate:self priority:0 swallowstouches:YES];    [self MenuStatus:NO Node:self];   }-(voID) unlockLayers{    [self MenuStatus:YES Node:self];}// Disabled/Enable layers-(voID) MenuStatus:(BOol)_enable Node:(ID)_node{    for (ID result in ((CCNode *)_node).children) {                if ([result isKindOfClass:[Ccmenu class]]) {            for (ID result1 in ((Ccmenu *)result).children) {                if ([result1 isKindOfClass:[CcmenuItem class]]) {                    ((CcmenuItem *)result1).isEnabled = _enable;                }            } // next        }     } // next}-(voID) registerWithtouchdispatcher {    [[CCtouchdispatcher shareddispatcher] addTargetedDelegate:self priority:INT_MIN+1 swallowstouches:YES];}- (voID)cctouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"Event: %@",event);    for( UItouch *touch in touches )    {        CGPoint location = [touch locationInVIEw: [touch vIEw]];        location = [[CCDirector sharedDirector] convertToGL: location];                cclayer *gl = (cclayer *)[self getChildByTag:4];        [gl setIstouchEnabled:NO];    }}-(voID) cctouchCancelled:(UItouch *)touch withEvent:(UIEvent *)event{}-(voID) cctouchended:(UItouch *)touch withEvent:(UIEvent *)event{    [self removeFromParentAndCleanup:YES];    }-(BOol) cctouchBegan:(UItouch *)touch withEvent:(UIEvent *)event{    return YES;}// Settings Modal Layer-(voID) showSettingsOnLayer:(cclayer *)layer closeBlock:(voID (^)())noBlock {    CoverLayer *coverLayer = [[CoverLayer alloc] init];    [layer addChild:coverLayer z:1000];    [coverLayer runAction:[CCFadeto actionWithDuration:kAnimationTime opacity:155]]; // smooth fade-in to dim with semi-transparency... // Other stuff goes here}    // CoverLayer    // This is meant to stop all touches,but it doesn't really work on CCScrollLayer#define kDialogTag 1234#import "CoverLayer.h"// class that implements a black colored layer that will cover the whole screen // and eats all touches except within the dialog Box child@implementation CoverLayer- (ID)init {    self = [super init];    if (self) {        [self initWithcolor:ccc4(0,0)                       wIDth:[CCDirector sharedDirector].winSize.wIDth                      height:[CCDirector sharedDirector].winSize.height];        self.istouchEnabled = YES;    }    return self;}- (voID)dealloc {    [[CCtouchdispatcher shareddispatcher] removeDelegate:self];    [super dealloc];}- (BOol)cctouchBegan:(UItouch *)touch withEvent:(UIEvent *)event {    CGPoint touchLocation = [self converttouchToNodeSpace: touch];    CCNode *dialogBox = [self getChildByTag: kDialogTag];    // eat all touches outsIDe of dialog Box    return !CGRectContainsPoint(dialogBox.boundingBox,touchLocation);}
解决方法 您只需要了解优先级如何与CCtouchdispatcher一起使用.
具有最小优先级值的层将首先接收触摸事件.现在您只需要相应地调整优先级.

在使用CCtouchdispatcher注册并覆盖cctouchBegan时,创建一个阻塞层类并将其优先级设置为最小值,并在其中返回YES.

如果您查看Ccmenu类,您会注意到默认优先级是kCcmenutouchPriority = -128,这就是菜单项具有更高触摸优先级的原因.

总结

以上是内存溢出为你收集整理的事件处理 – 如何在cocos2d中的模态层下停止所有CCTouch?全部内容,希望文章能够帮你解决事件处理 – 如何在cocos2d中的模态层下停止所有CCTouch?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1000020.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-21
下一篇2022-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存