当前位置:编程学习 > wap >>

Cocos-2d 关于SwallowTouch,进一步解释触摸事件分发机制

问题情境
模拟一个类似游戏提示信息的层:
1.游戏主场景可触摸,可交互;
2.当提示显示提示信息时,只有提示信息这一层可触摸同用户交互,其背景则不能继续响应触摸事件
3.当提示信息层从主场景中移除之后,游戏主场景才能继续响应触摸事件进行交互。
这里,我们暂时把“提示信息层”称为SwallowTouchLayer;将游戏主场景曾称为GameLayer
进一步描述上述情景的实质问题:
添加一个层“吃掉”(swallow)原有层touch事件
1.SwallowTouchLayer必须swallowTouches = YES;
2.SwallowTouchLayer的触摸事件优先级 > GameLayer的触摸事件优先级
为了解决上述问题,我们首先要了解cocos-2d中触摸事件的分发机制,问题解决就明了了。
这里以HellowWorld为例,在HellowWorld之上添加一个SwallowTouchLayer
参照下图

 

代码实现
1.首先在HellowWorldLayer.m中添加一个新的按钮项
CCMenuItem *addSwallowTouchLayerItem = [CCMenuItemFontitemWithString:@"addSwallowTouchLayer"block:^(id sender){
            [selfaddChild:[[SwallowTouchLayeralloc]init]];
        }];       
CCMenu *menu = [CCMenumenuWithItems:itemAchievement, itemLeaderboard,addSwallowTouchLayerItem,nil];


2.实现SwallowTouchLayer
SwallowTouchLayer.h
#import<Foundation/Foundation.h>
#import"cocos2d.h"

@inte易做图ce SwallowTouchLayer :CCLayer
{
    CCSprite *grayBackground;
    CCMenu *menu;
}

@end
SwallowTouchLayer.m
//
//  SwallowTouchLayer.m
//  SwallowTouch
//

#import"SwallowTouchLayer.h"

@implementation SwallowTouchLayer
- (id)init
{
    if(self = [super init])
    {
       //开启接收触摸事件
        self.isTouchEnabled = YES;
       
        //添加灰度背景
        grayBackground = [CCSprite spriteWithFile:@"swallowBackground.png"];
        grayBackground.position =ccp(240,160);
        [self addChild:grayBackground];
       
       //初始化Menu       
        CCMenuItem *menuItem = [CCMenuItemToggle itemWithTarget:self
                                                      selector:@selector(removeTeaching)
                                                          items:[CCMenuItemImage itemWithNormalImage:@"Icon.png"                                                                                          selectedImage:nil],                                                                         [CCMenuItemImage itemWithNormalImage:@"Icon.png"                                                                                          selectedImage:nil],
                                                                 nil];
       //设置第一步教程的位置
        menuItem.position =ccp(0,0);
       
        menu = [CCMenu menuWithItems:menuItem,nil];
        menu.position =ccp(240,160);
       
        //添加教程
        [self addChild:menu];       
    }
   returnself;
}

- (void)onEnter
{
   //在super onEnter中调用自身和孩子节点的registerWithTouchDispatcher,添加触摸代理
    [super onEnter];
   
    //重新调整menu响应优先级,使其能够响应触摸事件,视实际情况,可以省略该步
    [menu setHandlerPriority:kCCMenuHandlerPriority -2];   
}

- (void)onExit
{
    [[[CCDirector sharedDirector] touchDispatcher] removeDelegate:self];
    [super onExit];
}

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

//将自己从父场景中移除
- (void)removeTeaching
{
    [self removeFromParentAndCleanup:YES];
}

#pragma mark - Swallow Touch Input
/*
 *将CCLayer注册到CCTargetedTouchDelegate中,并将其响应优先级调至大于(等于)要覆盖的
 *优先级对象的响应优先级
 */
- (void)registerWithTouchDispatcher
{
    CCTouchDispatcher *touchDispatcher = [[CCDirector sharedDirector] touchDispatcher];
    [touchDispatcher addTargetedDelegate:self
                               priority:kCCMenuHandlerPriority -1
                         swallowsTouches:YES];
}

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
   //如果return NO,则不阻拦触摸消息
  

补充:移动开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,