这几天做效果,文案要求实现,手指触摸后 等待一个时间然后让我的道具对象 形状变大
于是难着我了, 百思不得其解之后,我于是去问度娘,于是乎还真有这个方法,我也不知道 官方有没有这个方法,如果有还请各位留言给我
此方法不是我原创,帖子结尾我会把原来的大神的地址贴出来
各位看官先看看我的吧
首先我们定义一个Layer 继承于 cc.Layer
/**
* Created by jsroads on 2014/12/11.15:47
* E-mail: jsroads@163.com
* Note:
*/
"use strict";
var ZhuBaJieLayer = cc.Layer.extend({
bLeftButtonClick : false,
bRightButtonClick : false,
sprite1 : null,
sprite2 : null,
ctor:function () {
this._super();
this.initUI();
},
initUI : function(){
cc.spriteFrameCache.addSpriteFrames(res.myBtn_plist);
this.sprite1 = cc.Sprite.create("#MyBtn_n.png");
this.sprite1.x = cc.director.getWinSize().width/2;
this.sprite1.y = cc.director.getWinSize().height/2;
this.addChild( this.sprite1);
this.sprite2 = cc.Sprite.create("#MyBtn_s.png");
this.sprite2.x = cc.director.getWinSize().width/2+200;
this.sprite2.y = cc.director.getWinSize().height/2+100;
this.addChild( this.sprite2);
// Make sprite1 touchable
var listener1 = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE, //TOUCH_ONE_BY_ONE 为单次触摸事件监听器
swallowTouches: true,
onTouchBegan: function (touch, event) {
var target = event.getCurrentTarget();
var locationInNode = target.convertToNodeSpace(touch.getLocation());
var s = target.getContentSize();
var rect = cc.rect(0, 0, s.width, s.height);
if (cc.rectContainsPoint(rect, locationInNode)) {
this.schedule(this.update,1);
if (target == this.sprite2) {
this.bRightButtonClick = true;
} else if (target == this.sprite1) {
this.bLeftButtonClick = true;
}
// 需要返回true,否则不会调用后面的onTouchEnded方法
return true;
}
return false;
}.bind(this),
onTouchMoved: function(touch,event){
cc.log("moved");
return false;
},
onTouchEnded: function (touch, event) {
cc.log("弹起");
this.unschedule(this.update);
var target = event.getCurrentTarget();
if (target == this.sprite2) {
this.bRightButtonClick = false;
this.sprite2.scale = 1;
} else if (target == this.sprite1) {
this.bLeftButtonClick = false;
this.sprite1.scale = 1;
}
}.bind(this)
});
cc.eventManager.addListener(listener1, this.sprite1);
cc.eventManager.addListener(listener1.clone(), this.sprite2);
},
initData : function(){
},
refresh : function(){
},
// 定时任务
update:function (dt) {
{
this.unschedule(this.update);
if (this.bLeftButtonClick)
{
this.sprite1.scale = 1.2;
cc.log("更新Left");
}
//this._man.runLeft(dt);
else if (this.bRightButtonClick){
this.sprite2.scale = 1.2;
cc.log("更新Right");
}
//this._man.runRight(dt);
}
}
});
```
最后 参考原帖地址 :http://www.cnblogs.com/linn/p/3658140.html