加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-js rocker

发布时间:2020-12-14 20:22:17 所属栏目:百科 来源:网络整理
导读:pre name="code" class="javascript"var ControlLayer=cc.Layer.extend({rocker:null,startTouch:null,ctor:function(){this._super();this.rocker=new Rocker(this,cc.p(500,500),res.rockerBg_png,res.rockerCnt_png);},onTouchBegan:function(touches,eve
<pre name="code" class="javascript">var ControlLayer=cc.Layer.extend({
	rocker:null,startTouch:null,ctor:function(){
		this._super();
		this.rocker=new Rocker(this,cc.p(500,500),res.rockerBg_png,res.rockerCnt_png);
	},onTouchBegan:function(touches,event){
		var touch=touches[0];
		cc.log("onTouchBegan");
		this.startTouch=touch.getLocation();
		event.getCurrentTarget().rocker.moveRockerToPoint(this.startTouch);
		return true;
	},onTouchMoved:function(touches,event){
		var touch=touches[0];
		var currentTouch=touch.getLocation();
		var step=currentTouch.x-this.startTouch.x;
		var range=64;
		if(step>0){
			step=range;
		}else if(step<0){
			step=-range;
		}

		cc.log(step);

		event.getCurrentTarget().rocker.changeStep(step);
	},onTouchEnded:function(touches,event){
		var touch=touches[0];
		cc.log("onTouchEnded");
		event.getCurrentTarget().rocker.resetPosToDefult();
	}
})

var Rocker=cc.Class.extend({
	defultPos:null,bgSprite:null,cntSprite:null,ctor:function(parent,pos,res1,res2){
		this.defultPos=pos;
		this.bgSprite=new cc.Sprite(res1);



		parent.addChild(this.bgSprite)

		this.cntSprite=new cc.Sprite(res2);
		this.bgSprite.addChild(this.cntSprite);

		this.resetPosToDefult();
		cc.eventManager.addListener({
			event:cc.EventListener.TOUCH_ALL_AT_ONCE,swallowTouches:true,onTouchesBegan:parent.onTouchBegan,onTouchesMoved:parent.onTouchMoved,onTouchesEnded:parent.onTouchEnded
		},parent);

	},moveRockerToPoint:function(pos){
		this.bgSprite.setPosition(pos);
	},resetPosToDefult:function(){
		this.bgSprite.setPosition(this.defultPos);
		var contentSize=this.bgSprite.getContentSize();
		this.cntSprite.setPosition(cc.p(contentSize.width/2,contentSize.height/2));
	},changeStep:function(num){
		var contentSize=this.bgSprite.getContentSize();
		this.cntSprite.setPosition(cc.p(contentSize.width/2+num,contentSize.height/2));
	}

})

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读