123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- 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_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);
- });
- GameModule.homeGuide.on('Fire_state24', () => {
- this.scrollView.vertical = true;
- }, 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);
- setTimeout(() => {
- this.scrollView.vertical = false;
- }, 500);
- } else {
- 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') {
- setTimeout(() => {
- this.scrollView.vertical = false;
- }, 500);
- return;
- }
- if (GameModule.homeGuide.getComponent('HomeGuide').curState == 'state24') {
- setTimeout(() => {
- this.scrollView.vertical = false;
- }, 500);
- return;
- }
- if (!GameModule.homeGuide.getComponent('HomeGuide').isPassGuideState('state24')) {
- setTimeout(() => {
- this.scrollView.vertical = false;
- }, 500);
- }
- },
- 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);
- },
- // update (dt) {},
- onScrollEvent(sender, event) {
- this.listAdapter.update();
- },
- });
|