cocos相机控制

import { _decorator, Component, EventKeyboard, EventMouse, Input, input, KeyCode, Node, v2, v3 } from 'cc';

const { ccclass, property } = _decorator;

@ccclass('pc2')

export class pc2 extends Component {

    hu3:boolean = false

    hu4:boolean = false

    hu5:boolean = false

    hu6:boolean = false

    start() {

        input.on(Input.EventType.MOUSE_MOVE,this.onMouseMove,this)

        input.on(Input.EventType.KEY_DOWN,this.onKeyDown,this)

        input.on(Input.EventType.KEY_UP,this.onKeyUp,this)

    }

    onMouseMove(event:EventMouse){

        if(event.getButton() == 2){

            //获取本地坐标系下的旋转,用欧拉角表示

            let fe1 = this.node.eulerAngles.x

            let fe2 = this.node.eulerAngles.y

            //鼠标在UI坐标系下的移动距离

            let x1 = event.movementX

            let y1 = event.movementY

            //用欧拉角设置世界坐标系下的旋转

            this.node.setWorldRotationFromEuler(fe1+=y1*-1,fe2+=x1*-1,0)

        }

    }

    onKeyDown(event:EventKeyboard){

        if(event.keyCode == KeyCode.KEY_W){

            this.hu3 = true

        }

        if(event.keyCode == KeyCode.KEY_A){

            this.hu4 = true

        }

        if(event.keyCode == KeyCode.KEY_S){

            this.hu5 = true

        }

        if(event.keyCode == KeyCode.KEY_D){

            this.hu6 = true

        }

    }

    onKeyUp(event:EventKeyboard){

        if(event.keyCode == KeyCode.KEY_W){

            this.hu3 = false

        }

        if(event.keyCode == KeyCode.KEY_A){

            this.hu4 = false

        }

        if(event.keyCode == KeyCode.KEY_S){

            this.hu5 = false

        }

        if(event.keyCode == KeyCode.KEY_D){

            this.hu6 = false

        }

    }

    update(deltaTime: number) {

        //旋转角度转化为弧度

        let ag = this.node.eulerAngles.y * 2/360 * Math.PI

       

        //计算基于z轴的方向向量

        let j = v3( Math.sin(ag), 0, Math.cos(ag) )

        //计算基于x轴的方向向量

        let o = v3(Math.cos(ag),0,Math.sin(ag))

        //方向向量进行单位化

        j.normalize()

        //根据方向向量移动位置

       

        let h1 = this.node.position.x

        let h2 = this.node.position.y

        let h3 = this.node.position.z

        if(this.hu3){

            this.node.setPosition

            ( h1 += deltaTime * j.x * 5 * -1, h2 += deltaTime * j.y * 5 * -1, h3 += deltaTime * j.z * 5 * -1 )

        }

        if(this.hu4){

            this.node.setPosition

            ( h1 += deltaTime * o.x * 2 * -1, h2 += deltaTime * o.y * 2 * -1, h3 += deltaTime * -o.z * 2 * -1 )

        }

        if(this.hu5){

            this.node.setPosition

            ( h1 += deltaTime * j.x * 2 * 1, h2 += deltaTime * j.y * 2 * 1, h3 += deltaTime * j.z * 2 * 1 )

        }

        if(this.hu6){

            this.node.setPosition

            ( h1 += deltaTime * o.x * 2 * 1, h2 += deltaTime * o.y * 2 * 1, h3 += deltaTime * -o.z * 2 * 1 )

        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值