import ViewBase from "./ViewBase"; import LDataCannonManager from "../datas/LDataCannonManager"; import CannonManager from "../manager/CannonManager"; import CannonCtr from "../CannonCtr"; import UIManager from "../manager/UIManager"; import Define from "../common/Define"; import Common from "../common/Common"; import SelectCannonItem from "../SelectCannonItem"; import UserInfo from "../UserInfo"; import WXHelper from "../common/WXHelper"; import GameManager from "../manager/GameManager"; import LDataCannon from "../datas/LDataCannon"; import {TrackingManager, TrackingType} from "../Tracking/TrackingManager"; // Learn TypeScript: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html // Learn Attribute: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html const {ccclass, property} = cc._decorator; class ScrollInfo{ index:number; offsetX:number; distanceX:number; constructor(index:number,offsetX:number){ this.index = index; this.offsetX = offsetX; } } @ccclass export default class SelectCannonViewCtr extends ViewBase { @property(cc.ScrollView) scrollView: cc.ScrollView = null; @property(cc.Node) nodeContent: cc.Node = null; @property(cc.Node) eventContent: cc.Node = null; @property(cc.Node) btn_back: cc.Node = null; @property(cc.Node) btn_video: cc.Node = null; @property(cc.Node) btn_use: cc.Node = null; @property(cc.Node) btn_share: cc.Node = null; @property(cc.Node) node_item: cc.Node = null; maxCount:number = 0; curCount:number = 0; scrollInfoArr:ScrollInfo[] = []; itemArr:SelectCannonItem[] = []; isCanAutoScroll:boolean = false scrollState:number = 0; curSelectIndex:number; isClickShare:boolean = false; refreshView(isFristRefresh:boolean = false){ this.isClickShare = false this.refreshItemState() this.scrollToIndex(UserInfo.curUseCannonId) } refreshItemState(){ for (let index = 0; index < this.itemArr.length; index++) { this.itemArr[index].refreshItemInfoByData(LDataCannonManager.dataList[index]); } } addEvent(){ Common.addClickEvent(this.eventContent,this.onClick.bind(this),false); Common.addClickEvent(this.btn_back,this.onClick.bind(this)); Common.addClickEvent(this.btn_use,this.onClick.bind(this)); Common.addClickEvent(this.btn_video,this.onClick.bind(this)); Common.addClickEvent(this.btn_share,this.onClick.bind(this)); } onClick(tag:string){ if(tag == "btn_back"){ UIManager.getInstance().showView(Define.viewMain) UIManager.getInstance().showView(Define.viewUpgrade) UIManager.getInstance().hideView(Define.viewSelectCannon) }else if(tag == "btn_use"){ if(UserInfo.curUseCannonId != this.curSelectIndex){ UserInfo.setCurUseCannonId(this.curSelectIndex) GameManager.instance.changeCannonById(this.curSelectIndex) UIManager.getInstance().showView(Define.viewMain) UIManager.getInstance().showView(Define.viewUpgrade) UIManager.getInstance().hideView(Define.viewSelectCannon) } }else if(tag == "btn_video"){ TrackingManager.send(TrackingType.RewardCannon); WXHelper.showVideo(function(state){ if(state == 1){ //播放成功奖励大炮 let data = LDataCannonManager.dataList[this.curSelectIndex] if(data != null && data.getmode == Define.cannonVideo){ UserInfo.addNewCannon(this.curSelectIndex,2);//获得一个新的大炮 this.refreshButtonsState() this.refreshItemState() } } }.bind(this)) }else if(tag == "btn_share"){ GameManager.instance.wxHelper.shareAppMessage("") this.isClickShare = true } } initView(){ this.maxCount = LDataCannonManager.dataList.length for (let index = 0; index < this.maxCount; index++) { let itemNode = cc.instantiate(this.node_item) itemNode.position = new cc.Vec2(index*213 + 320,-150) itemNode.setParent(this.nodeContent) this.scrollInfoArr.push(new ScrollInfo(index,itemNode.position.x)) let item:SelectCannonItem = itemNode.getComponent(SelectCannonItem); this.itemArr.push(item); } this.createCannon(this.curCount); this.nodeContent.width = 213 * (this.maxCount + 2) } createCannon(index){ CannonManager.createCannon(index,this.nodeContent.children[index],function(cannonCtr:CannonCtr){ cannonCtr.node.position = new cc.Vec2(0,55) cannonCtr.node.setScale(0.8) this.curCount++ if(this.curCount < this.maxCount){ this.createCannon(this.curCount) } }.bind(this)) } //滑动到合适的位置 scrollToFitPos(){ let tempX = Math.abs(this.nodeContent.position.x) console.log(tempX); for (let index = 0; index < this.scrollInfoArr.length; index++) { this.scrollInfoArr[index].distanceX = Math.abs(this.scrollInfoArr[index].offsetX - tempX) } this.scrollInfoArr.sort((a, b) => { return a.distanceX - b.distanceX; }); let tempIndex = this.scrollInfoArr[0].index; let state:number = UserInfo.getCannnonState(tempIndex); if(state == 1){ UserInfo.addNewCannon(tempIndex,2) //这里更改查看过的状态 this.refreshItemState() } this.scrollToIndex(tempIndex) } scrollToIndex(index){ this.curSelectIndex = index this.refreshButtonsState(); this.scrollView.scrollTo(new cc.Vec2(this.curSelectIndex/(LDataCannonManager.dataList.length - 1),0),1) } //刷新按键的状态 refreshButtonsState(){ this.btn_back.active = false this.btn_use.active = false this.btn_video.active = false this.btn_share.active = false if(UserInfo.isOwnCannonById(this.curSelectIndex)){ if(UserInfo.curUseCannonId == this.curSelectIndex){ this.btn_back.active = true }else{ this.btn_use.active = true } }else{ let data = LDataCannonManager.dataList[this.curSelectIndex] if(data.getmode == Define.cannonVideo && WXHelper.isLoadVideoSuccessful){ this.btn_video.active = true }else if(data.getmode == Define.cannonShare){ this.btn_share.active = true } else{ this.btn_back.active = true } } } update(dt){ if(this.scrollView.isScrolling()){ this.scrollState = 1 }else{ if(this.scrollState == 1){ this.scrollState = 0 this.scrollToFitPos() } } } executeEvent(eventTag:string = "defualt"){ let tempData:LDataCannon = LDataCannonManager.GetDataById(this.curSelectIndex); if(tempData != null && tempData.getmode == Define.cannonShare){ UserInfo.addNewCannon(this.curSelectIndex,2);//获得一个新的大炮 分享获得大炮 this.refreshButtonsState() this.refreshItemState() } } }