【cocos creator】键盘控制人物平滑八方位行走

摇杆封装:

https://download.csdn.net/download/K86338236/85912640


cc.Class({
    extends: cc.Component,

    properties: {
        player: {
            default: null,
            type: cc.Node,
            displayName: '控制角色',
        },
    },

    onLoad() {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
        this.index = 0;
    },

    start() {

    },

    onKeyDown(event) {
        if (event.keyCode == cc.macro.KEY.left) {
            if (this.index == 0) this.index = 5;
            if (this.index == 3) this.index = 4;
            if (this.index == 7) this.index = 6;
        }
        else if (event.keyCode == cc.macro.KEY.right) {
            if (this.index == 0) this.index = 1;
            if (this.index == 3) this.index = 2;
            if (this.index == 7) this.index = 8;
        }
        else if (event.keyCode == cc.macro.KEY.up) {
            if (this.index == 0) this.index = 3;
            if (this.index == 1) this.index = 2;
            if (this.index == 5) this.index = 4;
        }
        else if (event.keyCode == cc.macro.KEY.down) {
            if (this.index == 0) this.index = 7;
            if (this.index == 1) this.index = 8;
            if (this.index == 5) this.index = 6;
        }
    },

    onKeyUp(event) {
        if (event.keyCode == cc.macro.KEY.left) {
            if (this.index == 4) { this.index = 3; return; }
            if (this.index == 6) { this.index = 7; return; }
            if (this.index != (1 || 2 || 3)) this.index = 0;
        }
        else if (event.keyCode == cc.macro.KEY.right) {
            if (this.index == 2) { this.index = 3; return; }
            if (this.index == 8) { this.index = 7; return }
            if (this.index != (5 || 4 || 6)) this.index = 0;
        }
        else if (event.keyCode == cc.macro.KEY.up) {
            if (this.index == 2) { this.index = 1; return; }
            if (this.index == 4) { this.index = 5; return; }
            if (this.index != (7 || 8 || 6)) this.index = 0;

        } else if (event.keyCode == cc.macro.KEY.down) {
            if (this.index == 8) { this.index = 1; return; }
            if (this.index == 6) { this.index = 5; return; }
            if (this.index != (3 || 2 || 1)) this.index = 0;
        }
    },
    update(dt) {
        if (this.index) {
            this.onMove(this.index, false);
        }
    },

    onMove(index, isFour) {
        var speed = 1;
        var speedxy = speed * Math.cos(45 * Math.PI / 180);
        //四方位行走
        if (isFour) {
            switch (index) {
                case 1:
                case 5:
                    this.player.x += speed;
                    break;
                case 2:
                    this.player.y += speed;
                    break;
                case 3: this.player.x -= speed;
                    break;
                case 4:
                    this.player.y -= speed;
                    break;
            }
            return;
        }
        //八分向行走
        switch (index) {
            case 1:
            case 9:
                this.player.x += speed;
                break;
            case 2:
                this.player.x += speedxy;
                this.player.y += speedxy;
                break;
            case 3: this.player.y += speed;
                break;
            case 4:
                this.player.x -= speedxy;
                this.player.y += speedxy;
                break;
            case 5: this.player.x -= speed;
                break;
            case 6:
                this.player.x -= speedxy;
                this.player.y -= speedxy;
                break;
            case 7: this.player.y -= speed;
                break;
            case 8:
                this.player.x += speedxy;
                this.player.y -= speedxy;
                break;
        }
    },

    onDestroy() {
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    },
});

摇杆八方位控制:

                //this.dir:摇杆角度
                var angle = this.dir % 360;
                var count = 8;//行走方位,4/8
                //计算角色移动象限
                var index = Math.round((angle + 360 / count) / (360 / count));
                //cc.log(angle, index);
                this.onMove(index, count == 4);
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Cocos Creator中使用TypeScript来创建弹窗控制器,你可以按照以下步骤进行操作: 1. 创建一个名为PopupManager的类,该类将负责管理所有弹窗的打开和关闭操作。 ```typescript export class PopupManager { private static _instance: PopupManager = null; private _popupStack: cc.Node[] = []; private _popupLayer: cc.Node = null; public static getInstance(): PopupManager { if (PopupManager._instance == null) { PopupManager._instance = new PopupManager(); } return PopupManager._instance; } private constructor() { this._popupLayer = new cc.Node(); this._popupLayer.name = "PopupLayer"; cc.director.getScene().addChild(this._popupLayer); } public pushPopup(popup: cc.Node) { if (popup == null) { return; } this._popupLayer.addChild(popup); this._popupStack.push(popup); } public popPopup() { if (this._popupStack.length > 0) { let popup = this._popupStack.pop(); popup.removeFromParent(true); } } } ``` 2. 创建一个名为PopupBase的类,该类将作为所有弹窗的基类,提供弹窗的打开和关闭方法。你可以在该类的onLoad方法中进行初始化操作。 ```typescript export class PopupBase extends cc.Component { public onLoad() { // 初始化弹窗界面 this.initView(); } public initView() { // 子类需要实现该方法,用于初始化弹窗界面 } public show() { // 弹出弹窗 PopupManager.getInstance().pushPopup(this.node); } public hide() { // 隐藏弹窗 PopupManager.getInstance().popPopup(); } } ``` 3. 创建一个名为MyPopup的类,该类将作为你的具体弹窗控制器,在该类中继承PopupBase类。 ```typescript export class MyPopup extends PopupBase { private _closeBtn: cc.Node = null; public initView() { // 初始化弹窗界面 this._closeBtn = this.node.getChildByName("CloseBtn"); this._closeBtn.on(cc.Node.EventType.TOUCH_END, this.onCloseBtnClick, this); } private onCloseBtnClick() { // 关闭弹窗 this.hide(); } } ``` 4. 在场景中添加一个空节点作为弹窗容器,并将MyPopup预制体添加到该节点下。 ```typescript const {ccclass, property} = cc._decorator; @ccclass export class MainScene extends cc.Component { @property(cc.Prefab) private _myPopupPrefab: cc.Prefab = null; private _myPopup: cc.Node = null; public onLoad() { // 加载弹窗预制体 this._myPopup = cc.instantiate(this._myPopupPrefab); } public onBtnShowPopupClick() { // 显示弹窗 this._myPopup.getComponent(MyPopup).show(); } } ``` 现在你已经创建了一个弹窗控制器,可以通过该控制器来方便地管理多个弹窗,并在需要的时候打开或关闭它们。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烧仙草奶茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值