zoukankan      html  css  js  c++  java
  • cocos2d-html5学习之三-为sprite添加触摸事件

    在斗地主中,使用了cc.Sprite来实现扑克,但是cc.Sprite默认并不能接收触摸事件,需要手动将它注册到事件分配器中。

    1. 在onEnter中注册为代理,由于扑克牌会产生重叠,在选择的时候不能让触摸事件传递到被覆盖的牌上,因此不能使用standardTargetedDelegate。

    onEnter:function(){
            cc.registerTargetedDelegate(0, true, this);
            this._touchEnabled=true;
            this._super();
        }

    2. 实现其它几个触摸事件,其中onTouchBegan中需要返回true,否则不会调用后面的onTouchEnded方法。

    onTouchBegan:function(touches,event){
        	var rect = this.touchRect();
        	var point = touches.getLocation();
            if(cc.rectContainsPoint(this.touchRect(),touches.getLocation())){
                this._touchBegan=true;
                return true;
            }
    
            return false;
        }
    
        onTouchEnded:function(touches,event){
            if(this._touchBegan){
                this._touchBegan=false;
    
                if(this.active) {
            		this.active = false;
    
            		this.setPositionY(this.getPositionY() - 30);
            	}
            	else {
            		this.active = true;
    
            		this.setPositionY(this.getPositionY() + 30);
    	        }
            }
        }

    详情见:戴维营教育

  • 相关阅读:
    1.惨不忍睹凌乱的定时任务
    二维码名片
    给定的逗号分隔的数字字符串转换为Table
    sql 列集合转换成逗号分隔的字符类型
    linq 分组
    触发器
    整合思路、步骤
    整合注意事项
    配置文件
    Struts2的线程安全性
  • 原文地址:https://www.cnblogs.com/jsxh/p/3495158.html
Copyright © 2011-2022 走看看