MainUIController:
//此方法为左滑,实现右滑则改成<
// if (MainUIControllers.instance.end_pos.x //< (MainUIControllers.instance.start_pos.x+15)) {
// console.log("进入战斗界面");
// MainUIControllers.instance.goBattle();
// }
start() {
this.onTouchBattle();
}
//注册滑动接收监听
onTouchBattle(){
this._singleTouchCtr = this.node.addComponent(TouchBatlleControllers);
this._singleTouchCtr.node.on(TouchBatlleControllers.SingleTouchDownEvent, this.onTouchDown);
this._singleTouchCtr.node.on(TouchBatlleControllers.SingleTouchMoveEvent, this.onTouchMove);
this._singleTouchCtr.node.on(TouchBatlleControllers.SingleTouchUpEvent, this.onTouchUp);
}
onTouchDown(pos:Vec2){
MainUIControllers.instance.start_pos = pos;
console.log("按下"+MainUIControllers.instance.start_pos);
}
onTouchMove(pos:Vec2){
MainUIControllers.instance.move_pos = pos;
//console.log("移动"+MainUIControllers.instance.move_pos);
}
onTouchUp(pos:Vec2){
MainUIControllers.instance.end_pos = pos;
console.log("抬起"+MainUIControllers.instance.end_pos);
if (MainUIControllers.instance.end_pos.x > (MainUIControllers.instance.start_pos.x+15)) {
console.log("进入战斗界面");
MainUIControllers.instance.goBattle();
}
}
goBattle(){
console.log("进入战斗界面");
director.loadScene("Battle");
}
TouchControllers:(放置到背景或画布上)
import { _decorator, Component, EventTouch, Input, input, Node, EventTarget } from 'cc';
const { ccclass, property } = _decorator;
//左滑进入战斗界面
@ccclass('TouchBatlleControllers')
export class TouchBatlleControllers extends Component {
private _touchId: number;
private static readonly defaultTouchId = -1;
gobattle: boolean;
public static readonly SingleTouchDownEvent: string = 'SingleTouchDownEvent';
public static readonly SingleTouchMoveEvent: string = 'SingleTouchMoveEvent';
public static readonly SingleTouchUpEvent: string = 'SingleTouchUpEvent';
start()
{
this.init();
}
private init(): void
{
input.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
input.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
this.resetTouchId();
}
private resetTouchId(): void
{
this._touchId = TouchBatlleControllers.defaultTouchId;
}
private hasTouchId(): boolean
{
return this._touchId != TouchBatlleControllers.defaultTouchId;
}
private onTouchStart(event: EventTouch):void
{
if (!this.hasTouchId())
{
this._touchId = event.touch.getID();
console.log(this._touchId);
this.node.emit(TouchBatlleControllers.SingleTouchDownEvent, event.touch.getLocation());
}
}
private onTouchMove(event: EventTouch):void
{
if (this._touchId == event.touch.getID())
{
console.log(this._touchId);
this.node.emit(TouchBatlleControllers.SingleTouchMoveEvent, event.touch.getLocation());
}
}
private onTouchEnd(event: EventTouch):void
{
if (this._touchId == event.touch.getID())
{
console.log(this._touchId);
this.node.emit(TouchBatlleControllers.SingleTouchUpEvent, event.touch.getLocation());
this.resetTouchId();
}
}
}