zoukankan      html  css  js  c++  java
  • cocos2d ccLayer响应触摸事件方法

    查看官方文档可知,cocos2d支持两种不同的处理触摸事件的方法

    分别为:standard touch delegate 和 Targeted touch delegate

    1、standard touch delegate

    1.1定义

    @protocol CCStandardTouchDelegate <NSObject>
    @optional
    - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
    @end
    

    1.2使用

    在cclayer子类中加入::即可触发事件。

    self.isTouchEnabled = YES;
    

    2、Targeted touch delegate

    2.1定义

    @protocol CCTargetedTouchDelegate <NSObject>
    - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event;
    @optional
    // touch updates:
    - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event;
    - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event;
    - (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event;
    @end
    

    2.2使用

    - (void)onEnter
    {
    	[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
    	[super onEnter];
    }
    - (void)onExit
    {
       [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
         [super onExit];
    }
    另外ccTouchBegan是必须实现的,而且当返回TRUE的时候触发其他事件。

    但是官方的说法是cclayer子类必须重写registerWithTouchDispatcher,我这种方法是在非cclayer中实现响应才会用到的,但我自己测试并不可行,不知到是不是文档太旧了,还是如何。

  • 相关阅读:
    python搭建开发环境
    django初探
    linux下安装Composer
    文件记录追加 file_put_content
    自定义导出表格
    异步处理接口 fsockopen
    appcache checking update
    js pix
    Event Aggregator
    ko list and css gradient
  • 原文地址:https://www.cnblogs.com/jeekun/p/2032914.html
Copyright © 2011-2022 走看看