cocoscreator实现滑动进入某场景

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();

       }

    }

}

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值