zoukankan      html  css  js  c++  java
  • cocos2d ccTouchBegan不被调用

    参考自:http://blog.sina.com.cn/s/blog_61e26bcb0100xwqe.html

    CCLayer 里面的 ccTouchBegan 和 ccTouchesBegan 到底调用哪个?

    默认调用的是 ccTouchesBegan 方法~

    添加了如下代码的话

    /**
     * 2012。08。30。12。56~
     * 启用 ccTouchEnded,禁用默认的 ccTouchesEnded~
     */
    -(void) registerWithTouchDispatcher {
        [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:true];
    }

    就会调用 ccTouchBegan 方法~

    而且,ccTouchBegan 里面返回 NO,触摸事件就不会继续往下传递


    为什么呢?

    下面是我在 CCTouchHandler.m 里面扒出来的两块儿代码:

    -(id) initWithDelegate:(id)del priority:(int)pri
    {
    	if( (self=[super initWithDelegate:del priority:pri]) ) {
    		if( [del respondsToSelector:@selector(ccTouchesBegan:withEvent:)] )
    			enabledSelectors_ |= kCCTouchSelectorBeganBit;
    		if( [del respondsToSelector:@selector(ccTouchesMoved:withEvent:)] )
    			enabledSelectors_ |= kCCTouchSelectorMovedBit;
    		if( [del respondsToSelector:@selector(ccTouchesEnded:withEvent:)] )
    			enabledSelectors_ |= kCCTouchSelectorEndedBit;
    		if( [del respondsToSelector:@selector(ccTouchesCancelled:withEvent:)] )
    			enabledSelectors_ |= kCCTouchSelectorCancelledBit;
    	}
    	return self;
    }
    

    - (id)initWithDelegate:(id)aDelegate priority:(int)aPriority swallowsTouches:(BOOL)swallow
    {
    	if ((self = [super initWithDelegate:aDelegate priority:aPriority])) {	
    		claimedTouches = [[NSMutableSet alloc] initWithCapacity:2];
    		swallowsTouches = swallow;
    		
    		if( [aDelegate respondsToSelector:@selector(ccTouchBegan:withEvent:)] )
    			enabledSelectors_ |= kCCTouchSelectorBeganBit;
    		if( [aDelegate respondsToSelector:@selector(ccTouchMoved:withEvent:)] )
    			enabledSelectors_ |= kCCTouchSelectorMovedBit;
    		if( [aDelegate respondsToSelector:@selector(ccTouchEnded:withEvent:)] )
    			enabledSelectors_ |= kCCTouchSelectorEndedBit;
    		if( [aDelegate respondsToSelector:@selector(ccTouchCancelled:withEvent:)] )
    			enabledSelectors_ |= kCCTouchSelectorCancelledBit;
    	}
    	
    	return self;
    }
    观察后发现,其实就是做一些配置,然后就会注册各自的一批关于触摸的方法~
    最后,使用的是默认的配置调用 ccTouchesBegan 等方法的话,

    可以采用如下写法的:

    UITouch* touch = [touches anyObject];

    不一定非要快速遍历一趟的

    for(UITouch* touch in touches) {
        CGPoint location = [touch locationInView:[touch view]];
        location = [[CCDirector sharedDirector] convertToGL:location];
    }

    (如果不在 RootViewController.m 中添加开启多点触控特性的代码的话,touches 里面是不会有多个触摸点对象的)~



  • 相关阅读:
    写给QA/软件测试新人
    互联网产品线上故障管理规范
    爬了世纪佳缘后发现了一个秘密,世纪佳缘找对象靠谱吗?
    网传美团今年应届生年薪 35w+,严重倒挂老员工,为什么互联网大厂校招的薪资一年比一年高?...
    MySQL大表优化方案
    步入AI领域2年连升3级,我只是找对了学习方法而已……
    BZOJ 4008 亚瑟王(概率DP 奥妙重重)
    BZOJ 4318 OSU! (概率DP)
    BZOJ 3812 主旋律 (状压DP+容斥) + NOIP模拟赛 巨神兵(obelisk)(状压DP)
    BZOJ 4145 [AMPPZ2014]The Prices (状压DP)
  • 原文地址:https://www.cnblogs.com/java20130723/p/3212130.html
Copyright © 2011-2022 走看看