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中实现响应才会用到的,但我自己测试并不可行,不知到是不是文档太旧了,还是如何。

  • 相关阅读:
    第三方组件之SinGooCMS.Plugins
    .net 工具集之SinGooCMS.Utility
    轻量级ORM之SinGooCMS.ADO
    Mustache 使用说明
    npm install -g 错误集锦
    获取Rtx用户状态方法
    sudo 运行 npm或node时,出现找不到命令
    install build essential
    安装hadoop 2.2.0
    异步更新界面方法
  • 原文地址:https://www.cnblogs.com/jeekun/p/2032914.html
Copyright © 2011-2022 走看看