const ListAdapter = require('../utils/ListViewAdapter'); const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey; const GameModule = require("../utils/GameModule"); cc.Class({ extends: cc.Component, properties: { scrollView: cc.ScrollView, content: cc.Node, item: cc.Prefab, itemScriptName: 'LevelHomeItem', // topItem: cc.Prefab, bottomItem: cc.Prefab, }, // LIFE-CYCLE CALLBACKS: onLoad () { this.isMoving = false; this.listAdapter = new ListAdapter(this.scrollView); this.listAdapter.shouldRemove = false; this._isTouch = false; this.setupEvent(); }, setupEvent() { this.scrollView.node.on(cc.Node.EventType.TOUCH_START, (event) => { //// 如果有购买商品 一秒钟点击十次 if (!this._isTouch) { if (Global.isLongPressClick === true) { /// 最起码要按住1秒 this._isTouch = true; this._addClick = false; this._timeCount = 0; this.schedule(this.timeAction, 1); } } }); this.scrollView.node.on(cc.Node.EventType.TOUCH_CANCEL, () => { /// 防止多点点击 if (this._isTouch) { if (Global.isLongPressClick === true && this._addClick) { let clickCount = 1 / GameModule.userInfo.secondClick; clickCount -= 10; GameModule.userInfo.secondClick = 1 / clickCount; this._addClick = false; } else { this._addClick = true; } this._isTouch = false; this.unschedule(this.timeAction, this); } if (this.isMoving) { this.isMoving = false; return; } }); this.scrollView.node.on(cc.Node.EventType.TOUCH_END, (event) => { /// 防止多点点击 if (this._isTouch) { if (Global.isLongPressClick === true && this._addClick) { let clickCount = 1 / GameModule.userInfo.secondClick; clickCount -= 10; GameModule.userInfo.secondClick = 1 / clickCount; this._addClick = false; } else { this._addClick = true; } this._isTouch = false; this.unschedule(this.timeAction, this); } if (this.isMoving) { this.isMoving = false; return; } this.clickAddMoney(); }, this); this.scrollView.node.on(cc.Node.EventType.TOUCH_MOVE, (event) => { let x = event.getLocationX(); let y = event.getLocationY(); let startLocation = event.getStartLocation(); if (Math.abs(x - startLocation.x) > 5 || Math.abs(y - startLocation.y) > 5) { this.isMoving = true; } }, this); GameEvent.on(GameNotificationKey.StarEnterRoom, this, (starId, roomId) => { this.refreshStarEnterRoom(starId, roomId); }); GameEvent.on(GameNotificationKey.StarLeaveRoom, this, (starId, roomId) => { this.refreshStarLeaveRoom(starId, roomId); }); GameEvent.on(GameNotificationKey.AllStarLeaveRoom, this, this.refreshAllStarLeaveRoom); //领取完教程26奖励后可以滚动 GameModule.homeGuide.on('Fire_state26', () => { this.canScroll = true; this.scrollView.vertical = true; }, this); this.canScroll = true; this.scrollView.node.on("scroll-began", (event) => { if (!this.canScroll) { this.scrollView.vertical = false; } }, this); }, clickAddMoney() { GameEvent.fire(GameNotificationKey.ClickAddMoney); }, timeAction() { this._timeCount += 1; if (this._timeCount == 1) { if (!this._addClick) { let clickCount = 0; if (GameModule.userInfo.secondClick == 0) { clickCount = 10; } else { clickCount = 1 / GameModule.userInfo.secondClick; clickCount += 10; } GameModule.userInfo.secondClick = 1 / clickCount; this._addClick = true; } } else { GameModule.userInfo.clickCount += 10; } }, start () { }, configBuildings(dataList, isUnlock = false) { this.dataList = dataList; if (isUnlock) { this.listAdapter.addItemsToDataList(this.dataList[0],true); //上面的方法会重新计算cotent总高度,所以需要再加回底部的高度 this.bottomItemNode.setPosition(0, -(this.bottomItemHeight * 0.5 + this.content.height)); this.content.height += this.bottomItemHeight; if (GameModule.homeGuide.getComponent('HomeGuide').curState == 'state21' || GameModule.homeGuide.getComponent('HomeGuide').curState == 'state24') { this.scrollView.vertical = true; this.scrollView.scrollToBottom(0.0); this.canScroll = false; } else { this.canScroll = true; this.scrollView.scrollToOffset(this.scrollPosition, 0.0); } } else { this.scrollView.content.removeAllChildren(); let top = cc.instantiate(this.topItem); this.topItemHeight = top.height; // get total content height this.content.addChild(top); top.setPosition(0, -(this.topItemHeight * 0.5)); let topItemScript = top.getComponent('LevelHomeTop'); topItemScript.updateItem(); this.listAdapter.updateItems(this.dataList, this.item, this.itemScriptName, this.topItemHeight); this.bottomItemNode = cc.instantiate(this.bottomItem); this.bottomItemHeight = this.bottomItemNode.height; // get total content height this.content.addChild(this.bottomItemNode); this.bottomItemNode.setPosition(0, -(this.bottomItemHeight * 0.5 + this.content.height)); let itemScript = this.bottomItemNode.getComponent('LevelHomeBottom'); itemScript.updateItem(); //上面的方法会重新计算cotent总高度,所以需要再加回底部的高度 this.bottomItemNode.setPosition(0, -(this.bottomItemHeight * 0.5 + this.content.height)); this.content.height += this.bottomItemHeight; this.scrollView.scrollToBottom(0.1); this.scrollPosition = this.scrollView.getContentPosition(); } }, configGuide() { if (GameModule.homeGuide.getComponent('HomeGuide').curState == 'state20') { this.canScroll = false; return; } if (GameModule.homeGuide.getComponent('HomeGuide').curState == 'state24') { this.canScroll = false; return; } if (!GameModule.homeGuide.getComponent('HomeGuide').isPassGuideState('state24')) { this.canScroll = false; } }, refreshRoomData(roomInfo) { this.dataList.forEach((item, index) => { if (item.roomId == roomInfo.roomId) { this.dataList[index] = roomInfo; } }); }, refreshStarEnterRoom(starId, roomId) { GameModule.userInfo.refreshSecondText(); GameEvent.fire(GameNotificationKey.UpBuildingLevel); if (roomId <= 0) { return; } for (let i = 0; i < this.dataList.length; ++i) { let room = this.dataList[i]; if (room.roomId == roomId) { if (room.roomStars == undefined) { room.roomStars = []; } room.roomStars.push(starId); } } this.listAdapter.updateDataList(this.dataList, roomId); }, refreshStarLeaveRoom(starId, roomId) { // GameModule.userInfo.refreshSecondText(); // GameEvent.fire(GameNotificationKey.UpBuildingLevel); if (roomId <= 0) { return; } for (let i = 0; i < this.dataList.length; ++i) { let room = this.dataList[i]; if (room.roomId == roomId) { if (room.roomStars != undefined) { for (let j = 0; j < room.roomStars.length; ++j) { let roomStarId = room.roomStars[j]; if (roomStarId == starId) { room.roomStars.splice(j, 1); } } } } } this.listAdapter.updateDataList(this.dataList, roomId); }, refreshAllStarLeaveRoom() { for (let i = 0; i < this.dataList.length; ++i) { let room = this.dataList[i]; if (room.roomStars != undefined && room.roomStars.length > 0) { room.roomStars = []; } } this.listAdapter.refreshItems(this.dataList); }, // update (dt) {}, onScrollEvent(sender, event) { this.listAdapter.update(); }, });