
/** * Created by heXm on 2017/3/8. */cc.Class({ extends : cc.Component,propertIEs:{ itemGroup:{ default:[],type:cc.Node },_sensitiveArg:{ default:150,type:cc.Integer },_itemLen:3,_itemContent:[],_itemPosY:506,_roomType:0 },onLoad:function () { this.initItemGroup(); this.selectIDx = 1; this.addListeners(); },initItemGroup:function () { let self = this; let url = 'prefab/ListItem'; let x = cc.winSize.wIDth; let itemPosX = [ x/10,x/2,x*9/10 ]; cc.loader.loadRes(url,function (err,prefab) { if(err){ return 0; } for(let i=0;i<self._itemLen;i++){ let item = cc.instantiate(prefab); item.parent = self.node; item.setposition(cc.v2(itemPosX[i],self._itemPosY)); self.itemGroup.push(item); } self.setItemIDx(); self.addItemListeners(); }); },addListeners:function () { "use strict"; this.node.on(cc.Node.EventType.touch_START,this.ontouchstart,this); this.node.on(cc.Node.EventType.touch_MOVE,this); this.node.on(cc.Node.EventType.touch_END,this); },addItemListeners:function () { let len = this.itemGroup.length; for(let i=0;i<len;i++){ this.itemGroup[i].on(cc.Node.EventType.touch_END,this.onItemSelected,this); } },onItemSelected:function (e) { cc.log(e.target.IDx); },enterGame:function () { let contentLen = this._itemContent.length; let IDx = (this.getContentIDx()+1)%contentLen; let ID = this._itemContent[IDx][3]; let game = null; for(let index in GameConfig.game_List){ if(ID == GameConfig.game_List[index].gameID){ game = GameConfig.game_List[index]; } } cc.log('game',ID,game); if(!!game){ let GameRoomname = { 0:'normal',1:'normal',2:'card',3:'match' }; let roomname = GameRoomname[this._roomType]; require('LoadGame').loadGame(game,roomname); } },setItemIDx:function () { let len = this.itemGroup.length; for(let i=0;i<len;i++){ this.itemGroup[i].IDx = i; if(i!==this.selectIDx){ this.itemGroup[i].getComponent(cc.Animation).play('List_item',0.5); } } this.steps = 0; this.setItemContent(); },ontouchstart:function (e) { switch (e.type){ case 'touchstart': break; case 'touchend': let touch = e.gettouches()[0]; let start = touch.getStartLocation(); let current = touch.getLocation(); let xDiff = start.x-current.x; if(xDiff>this._sensitiveArg){ this.scrollToleft(); } else if(xDiff<0-this._sensitiveArg){ this.scrollToRight(); } break; case 'touchmove': break; } },onClose:function (e) { this.node.removeFromParent(true); },isLoadFinished:function () { return this.itemGroup.length===this._itemLen; },scrollToleft:function () { if(typeof this.steps === 'undefined'){ this.steps = 0; } if(!this.isLoadFinished()){ return; } this.steps++; this.moveGroup(this.steps,false); },scrollToRight:function () { if(typeof this.steps === 'undefined'){ this.steps = 0; } if(!this.isLoadFinished()){ return; } this.steps--; this.moveGroup(this.steps,true); },test:function () { //this.moveGroup(Math.ceil(1000*Math.random())); },getPosByStep:function (step,i) { let x = cc.winSize.wIDth; let xArr = { 0:[0.1,0.9,0.5],1:[0.5,0.1,0.9],2:[0.9,0.5,0.1] }; let pos = cc.v2(xArr[i][step]*x,this._itemPosY); return pos; },getGroupByStep:function (step) { let arr = []; //let len = this.itemGroup.length; //let start = step%len; //arr.push(this.itemGroup[start],this.itemGroup[(start+1)%len],this.itemGroup[(start+2)%len]); arr.push(this.itemGroup[0],this.itemGroup[1],this.itemGroup[2]); return arr; },moveGroup:function (steps,isIn) { cc.log('steps',steps); let arr = this.getGroupByStep(steps); if(steps<0){ steps = (this._itemLen - Math.abs(steps%this._itemLen))%this._itemLen; }else { steps = Math.abs(steps%this._itemLen); } this.selectIDx = (steps+1)%this._itemLen; let move = isIn?this.moveIn:this.moveOut; for(let i=0;i<this._itemLen;i++){ arr[i].stopAllActions(); let pos = this.getPosByStep(steps,i); let IDx = isIn?this.getMoveInIDx(steps,i):this.getMoveOutIDx(steps,i); if(isIn){ if(IDx!==-1){ move.bind(this)(steps,i,arr[i]); this.showItemAnimation((this._itemLen+(IDx-1))%this._itemLen,false); this.showItemAnimation((this._itemLen+(IDx-2))%this._itemLen,true); } else{ arr[i].runAction(cc.moveto(0.5,pos)); } }else{ if(IDx!==-1){ move.bind(this)(steps,arr[i]); this.showItemAnimation((IDx+1)%this._itemLen,false); this.showItemAnimation((IDx+2)%this._itemLen,pos)); } } } },getMoveInIDx:function (step,i) { if(step==i){ return i; } return -1; },getMoveOutIDx:function (step,i) { let IDx = (this._itemLen+(step-1))%this._itemLen; if(i===IDx){ return IDx; } return -1; },moveIn:function (steps,item) { let pos = this.getPosByStep(steps,i); let startPos = this.getStartPos(); let endPos = this.getEndPos(); let self = this; item.runAction( cc.sequence(cc.moveto(0.2,endPos),cc.callFunc(function () { item.setposition(startPos); self.setItemContent(); }),cc.moveto(0.3,pos)) ); },moveOut:function (steps,startPos),cc.callFunc(function () { item.setposition(endPos); self.setItemContent(); }),getEndPos:function () { let x = cc.winSize.wIDth; return cc.v2(1.1*x,this._itemPosY); },getStartPos:function () { let x = cc.winSize.wIDth; return cc.v2(-0.1*x,getContentIDx:function () { let contentLen = this._itemContent.length; let cIDx = this.steps%contentLen; if(cIDx>=0){ }else{ cIDx = contentLen+cIDx; } return cIDx; },/** * @eg. [0,1,2,3,4,5,6]---item is [0,2] * by one step * ----------->[1,6,0]---item is [1,3] */ setItemContent:function () { let contentLen = this._itemContent.length; let cIDx = this.getContentIDx(); this.setContent(cIDx,(this._itemLen+this.selectIDx-1)%this._itemLen); this.setContent((cIDx+1)%contentLen,this.selectIDx); this.setContent((cIDx+2)%contentLen,(this.selectIDx+1)%this._itemLen); },setContent:function (cIDx,IDx) { let logo = this.itemGroup[IDx].children[1].getComponent(cc.Sprite); let logoUrl = this._itemContent[cIDx][0]; getSpriteFrameByUrl(logoUrl,frame) { if(err){ //logo.spriteFrame = new cc.SpriteFrame(cc.url.raw('resources/'+self._itemContent[cIDx][0]+'.png')); return 0; } logo.spriteFrame = frame; }); let nameSprite = this.itemGroup[IDx].children[2].getComponent(cc.Sprite); let nameUrl = this._itemContent[cIDx][1]; getSpriteFrameByUrl(nameUrl,frame) { if(err){ return 0; } nameSprite.spriteFrame = frame; }); //this.itemGroup[IDx].children[1].getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(cc.url.raw(this._itemContent[cIDx][1])); },showItemAnimation:function (IDx,isIn) { let animation = this.itemGroup[IDx].getComponent(cc.Animation); if(!isIn){ animation.getAnimationState('List_item').wrapMode = cc.WrapMode.normal; }else{ animation.getAnimationState('List_item').wrapMode = cc.WrapMode.Reverse; } animation.play('List_item'); },addContent:function (contentArr) { Array.prototype.push.apply(this._itemContent,contentArr); },setRoomType:function (roomType) { this._roomType = roomType; },setonlineNumbers:function (totalNumber) { if(!!totalNumber){ this.node.children[2].getComponent(cc.Label).string = '在线{0}人'.format(totalNumber); } },});@H_502_1@ 总结 以上是内存溢出为你收集整理的cocos2dx creator 创建类似gallery控件的循环效果全部内容,希望文章能够帮你解决cocos2dx creator 创建类似gallery控件的循环效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)