import { Component, Node, NodeEventType, _decorator, instantiate, math, random } from 'cc'; import { GameManager } from '../../Manager/GameManager'; import { AudioManage } from '../../Manager/AudioManage'; import { Global } from '../../Common/Global'; const { ccclass, property } = _decorator; @ccclass('GameMain') export class GameMain extends Component { @property(Node) mainNode: Node = null; @property(Node) player: Node = null; @property(Node) guaishou: Node = null; @property(Node) zidan: Node = null; @property(Node) zidanNode: Node = null; @property(Node) guaishouNode: Node = null; isUpdate: boolean = true; isBull = false; bullInterval = 0.5; touchTime = 0; onLoad() { } start() { } startGame(): void { this.gameOver() this.unscheduleAllCallbacks(); //初始化 this.isUpdate = true; this.node.on(NodeEventType.TOUCH_START, this.onThouchStart, this); this.node.on(NodeEventType.TOUCH_END, this.onThouchEnd, this); this.node.on(NodeEventType.TOUCH_CANCEL, this.onThouchEnd, this); this.updateGuaishou(); } updateGuaishou() { if (!this.isUpdate) return let guaishou = instantiate(this.guaishou); guaishou.active = true; this.guaishouNode.addChild(guaishou); const ranY = math.randomRangeInt(0, 3); const posX = guaishou.position.x; guaishou.setPosition(posX, -380 + ranY * 380); let time = GameManager.getInstance().getRand(GameManager.getInstance().intervalTime[0], GameManager.getInstance().intervalTime[1]); if (this.isUpdate) { this.scheduleOnce( this.updateGuaishou.bind(this), time / 1000 ) // setTimeout(() => { // this.updateGuaishou() // }, time); } else { this.unscheduleAllCallbacks(); } } onThouchStart(e) { this.isBull = true; console.log('onThouchStart'); } onThouchEnd(e) { this.isBull = false; console.log('onThouchEnd'); } gameOver() { //游戏结束 this.zidanNode.removeAllChildren(); this.guaishouNode.removeAllChildren(); this.isUpdate = false; // AudioManage.instance.playSound(Global.Audio_gameOver); } update(deltaTime: number) { if (!GameManager.getInstance().isStartGame) return; if (this.isBull) { this.touchTime += deltaTime; if (this.touchTime >= this.bullInterval) { let zidan = instantiate(this.zidan); zidan.active = true; this.zidanNode.addChild(zidan); zidan.setPosition(-212, this.player.position.y - 15); AudioManage.instance.playSound(Global.Audio_biu); this.touchTime = 0; } } } }