cocos3.8.6触碰检测方法在安卓端适配问题

二级会员 站长
发表于cocos分类
先看以下代码

protected _startTouchPos = null; protected _isMoved = false; protected onEnable(): void { this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this); this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this); this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this); this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this); } protected onDisable(): void { this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this); this.node.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this); this.node.off(Node.EventType.TOUCH_END, this.onTouchEnd, this); this.node.off(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this); } // 触摸监听处理 private onTouchStart(event: EventTouch) { if (this.node.children.length === 0) { return; } this._isMoved = false; const pos = event.getLocation(); this._startTouchPos = pos; this.touchSelectCardsArr(); } private onTouchMove(event: EventTouch) { if (!this._startTouchPos) return; this._isMoved = true; const pos = event.getLocation(); if (Math.abs(pos.x - this._startTouchPos.x) < 5) return; this.touchSelectCardsArr(); } private onTouchEnd(event: EventTouch) { if (!this._startTouchPos) return; this._startTouchPos = null; if (!this._isMoved) core.audioManager.playBtnClick(); for (const node of this.node.children) { const card = node.getComponent(GameSKBaseCard); if (!card.isSelected) continue; card.isSelected = false; if (card.isUpCard) { card.setCardDown(); } else { card.setCardUp(); } card.resetColor(); } const selectCardIds = []; for (const node of this.node.children) { const card = node.getComponent(GameSKBaseCard); if (card.isUpCard) { selectCardIds.push(card.id); } } core.message.dispatchEvent(GameEvent.GAME_SELECT_CARDS, selectCardIds); } onTouchCancel(event: EventTouch) { this._startTouchPos = null; for (const node of this.node.children) { const card = node.getComponent(GameSKBaseCard); if (!card.isCanTouch) continue; card.isSelected = false; card.setCardDown(); card.resetColor(); } core.message.dispatchEvent(GameEvent.GAME_SELECT_CARDS, []); } private touchSelectCardsArr(): void { for (let i = this.node.children.length - 1; i >= 0; --i) { const card = this.node.children[i].getComponent(GameSKBaseCard); if (!card.isCanTouch) continue; const uiTransform = card.node.getComponent(UITransform); if (uiTransform.hitTest(event.getLocation())) { card.isSelected = true; card.setSelectColor(); return; } } }

在网页端这样都没问题,正常判定两个预制体之间是否有接触
但是,在安卓或者IOS中,就失效了
需要在触摸事件中,传递触摸事件的 windowId,这样cocos底层代码在获取到windowId后会根据客户端类型进行相应的转化操作,从而使触碰检测功能正常运行

protected _startTouchPos = null; protected _isMoved = false; protected onEnable(): void { this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this); this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this); this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this); this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this); } protected onDisable(): void { this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this); this.node.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this); this.node.off(Node.EventType.TOUCH_END, this.onTouchEnd, this); this.node.off(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this); } // 触摸选牌监听处理 private onTouchStart(event: EventTouch) { if (this.node.children.length === 0) { return; } this._isMoved = false; const pos = event.getLocation(); this._startTouchPos = pos; this.touchSelectCardsArr(event); } private onTouchMove(event: EventTouch) { if (!this._startTouchPos) return; this._isMoved = true; const pos = event.getLocation(); if (Math.abs(pos.x - this._startTouchPos.x) < 5) return; this.touchSelectCardsArr(event); } private onTouchEnd(event: EventTouch) { if (!this._startTouchPos) return; this._startTouchPos = null; if (!this._isMoved) core.audioManager.playBtnClick(); for (const node of this.node.children) { const card = node.getComponent(GameSKBaseCard); if (!card.isSelected) continue; card.isSelected = false; if (card.isUpCard) { card.setCardDown(); } else { card.setCardUp(); } card.resetColor(); } const selectCardIds = []; for (const node of this.node.children) { const card = node.getComponent(GameSKBaseCard); if (card.isUpCard) { selectCardIds.push(card.id); } } core.message.dispatchEvent(GameEvent.GAME_SELECT_CARDS, selectCardIds); } onTouchCancel(event: EventTouch) { this._startTouchPos = null; for (const node of this.node.children) { const card = node.getComponent(GameSKBaseCard); if (!card.isCanTouch) continue; card.isSelected = false; card.setCardDown(); card.resetColor(); } core.message.dispatchEvent(GameEvent.GAME_SELECT_CARDS, []); } private touchSelectCardsArr(event: EventTouch): void { for (let i = this.node.children.length - 1; i >= 0; --i) { const card = this.node.children[i].getComponent(GameSKBaseCard); if (!card.isCanTouch) continue; const uiTransform = card.node.getComponent(UITransform); if (uiTransform.hitTest(event.getLocation(), event.windowId)) { card.isSelected = true; card.setSelectColor(); return; } } }


评论列表
加载更多
登录 分类