cocos2d touch事件处理 控制

单点 touch 代理
 1 @protocol CCTargetedTouchDelegate <NSObject>
 2 
 3 /** Return YES to claim the touch.
 4 
 5  @since v0.8
 6 
 7  */
 8 
 9 - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event;
10 
11 @optional
12 
13 // touch updates:
14 
15 - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event;
16 
17 - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event;
18 
19 - (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event;
20 
21 @end
CCTargetedTouchDelegate

多点 touch 代理

1 @protocol CCStandardTouchDelegate <NSObject>
2 @optional
3 - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
4 - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
5 - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
6 - (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
7 @end
CCStandardTouchDelegate

 CCLayer 为事件响应基类 

@interface CCLayer : CCNode <UIAccelerometerDelegate, CCStandardTouchDelegate, CCTargetedTouchDelegate>
@interface CCSprite : CCNode <CCRGBAProtocol, CCTextureProtocol>

 CCLayer 的 registerWithTouchDispatcher 方法完成注册 

CCTouchDispatcher 作为 CCDirector 单例的成员负责事件分发

-(void) registerWithTouchDispatcher
{
	CCDirector *director = [CCDirector sharedDirector];
	[[director touchDispatcher] addStandardDelegate:self priority:0];
}

 可重写 registerWithTouchDispatcher 方法以改变事件优先级 从而改变事件响应顺序

-(void) registerWithTouchDispatcher
{
    CCDirector *director = [CCDirector sharedDirector];
    [[director touchDispatcher] addTargetedDelegate:self priority:-128 swallowsTouches:YES];
}

配合 CCTargetedTouchDelegate 方法

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event

返回值 以控制是否终止事件在响应链中的传递

 

CCLayer 设置事件可接受 setIsTouchEnabled

-(BOOL) isTouchEnabled
{
    return isTouchEnabled_;
}

-(void) setIsTouchEnabled:(BOOL)enabled
{
    if( isTouchEnabled_ != enabled ) {
        isTouchEnabled_ = enabled;
        if( isRunning_ ) {
            if( enabled )
                [self registerWithTouchDispatcher];
            else {
                CCDirector *director = [CCDirector sharedDirector];
                [[director touchDispatcher] removeDelegate:self];
            }
        }
    }
}

CCLayer 在 onEnter 调用注册事件方法 registerWithTouchDispatcher

-(void) onEnter
{
#ifdef __CC_PLATFORM_IOS
    // register 'parent' nodes first
    // since events are propagated in reverse order
    if (isTouchEnabled_)
        [self registerWithTouchDispatcher];//调用注册事件方法

#elif defined(__CC_PLATFORM_MAC)
    CCDirector *director = [CCDirector sharedDirector];
    CCEventDispatcher *eventDispatcher = [director eventDispatcher];

    if( isMouseEnabled_ )
        [eventDispatcher addMouseDelegate:self priority:[self mouseDelegatePriority]];

    if( isKeyboardEnabled_)
        [eventDispatcher addKeyboardDelegate:self priority:[self keyboardDelegatePriority]];

    if( isTouchEnabled_)
        [eventDispatcher addTouchDelegate:self priority:[self touchDelegatePriority]];

#endif

    // then iterate over all the children
    [super onEnter];
}

 

 

posted @ 2013-08-27 23:09  thc  阅读(422)  评论(0)    收藏  举报