cocos2d-js rocker

前端之家收集整理的这篇文章主要介绍了cocos2d-js rocker前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<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));
	}

})
原文链接:https://www.f2er.com/cocos2dx/344347.html

猜你在找的Cocos2d-x相关文章