Cocos Creator TS代码智能提示

Cocos Creator支持JS和TS两种脚本,在这里我强烈建议使用TS来做脚本开发。有几个比较重要的原因:

  • TS本身是JS的超集,完全兼容js
  • TS有静态语言特性,代码易于阅读和维护
  • 近乎完美的智能提示,而JS的代码提示几乎没有

在Creator里面写TS,要实现代码智能提示,需要手动添加申明文件,步骤如下:
点击菜单 开发者->VS Code 工作流->更新VS Code智能提示数据

点这个菜单,实际上就是把引擎目录下的模板文件creator.d.ts拷贝到了你的项目目录下,这个模板文件是引擎开发组写好了放在引擎目录下的,每个版本都有对应的实现。

在Creator的新版本预发布的阶段,通常这个声明文件很不完善,这就导致很多接口提示没有,写代码就会爆红,对于有强迫症的同学来说,可能总是会想办法把爆红的提示去掉。

找到引擎安装目录,打开文件夹resources\utils\api,可以看到有一个creator.d.ts文件,这个文件就是操作Creator编辑器的时候拷贝到项目目录下的。我们只要修改了这个文件,所有项目都可以更新。

修改智能提示有两种方式,一种是修改引擎的creator.d.ts,另一种是自己新建一个自己的声明文件,例如我的捕鱼里面就是自己定义了一个global.d.ts

在1.93版本的时候,我把shader相关的隐藏接口全部写在这个声明文件里面,代码如下

declare module cc {
    /**
     * 实现WebGL program的类,用来做shader编程
     */
    export class GLProgram{
        /**
         * 实现WebGL program的类,用来做shader编程
         */
        constructor();
        /**
         * 使用顶点shader字符串和片段shader字符串初始化cc.GLProgram
         * @param vertShaderStr 顶点shader字符串
         * @param fragShaderStr 片段shader字符串
         */
        initWithString(vertShaderStr: string, fragShaderStr: string): boolean;

        /**
         * It will add a new attribute to the shader
         * @param attributeName 
         * @param index 
         */
        addAttribute(attributeName: number, index: number);
        /**
         * Initializes the cc.GLProgram with a vertex and fragment with string
         * @param vertShaderStr 
         * @param fragShaderStr 
         */
        initWithVertexShaderByteArray(vertShaderStr: string, fragShaderStr: string):boolean;

        /**
         * 连接 glprogram
         */
        link(): boolean;
        
        /**
         * 这个函数创建4个统一变量即uniform 
         *  cc.macro.UNIFORM_PMATRIX
         *  cc.macro.UNIFORM_MVMATRIX
         *  cc.macro.UNIFORM_MVPMATRIX
         *  cc.macro.UNIFORM_SAMPLER
        */
        updateUniforms();

        /**
         * it will call gl.useProgram()
         */
        use();

        /**
         * calls retrieves the named uniform location for this shader program.
         * @param name 
         */
        getUniformLocationForName(name: string): number;

        /**
         * calls glUniform1f only if the values are different than the previous call for this same shader program.
         * @param location 
         * @param f1 
         */
        setUniformLocationWith1f(location, f1);

        /**
         * calls glUniform2f only if the values are different than the previous call for this same shader program.
         * @param location 
         * @param f1 
         * @param f2 
         */
        setUniformLocationWith2f(location,f1,f2)
    }

    export class GLProgramState{

        static getOrCreateWithGLProgram(program:GLProgram):any;
    }
}

写法也很简单,在Cocos Creator中,基本上都是基于模块和命名空间的,基本没有单独的变量声明。使用关键字declare来声明即可。

同名的变量会被无冲突合并,这样是为什么我在项目里面定义一个global.d.ts,在里面声明了cc这个命名空间,没有和creator.d.ts里面的声明冲突的原因。

新版的Cocos Creator有很多声明缺失,比如2.1.1里面关于3d和材质的代码,基本上没有提示。
后面我将会更新一版2.1.1的智能提示文件,方便大家开发。

捕鱼代码

https://github.com/fylz1125/CCFish

关注我的微信

扫码关注我的微信【Cocos Creator研究笔记】,每天更新。
扫码关注微信

以下是使用 TypeScript 编写的 Cocos Creator 代码示例: ```typescript // 引入 Cocos Creator 引擎模块 import { Component, Node, Vec2 } from 'cc'; // 自定义组件类 export default class MyComponent extends Component { // 节点引用 private _node: Node | null = null; // 生命周期:组件被添加到节点上时调用 public onEnable() { // 获取节点引用 this._node = this.node.getChildByName('MyNode'); // 注册触摸事件 this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this); } // 生命周期:组件被移除时调用 public onDisable() { // 取消注册触摸事件 this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this); // 释放节点引用 this._node = null; } // 触摸事件处理函数 private onTouchStart(event: any, touch: any) { // 获取触摸点在全局坐标系下的位置 const touchPos: Vec2 = touch.getLocation(); // 将触摸点位置转换为相对于 MyNode 节点的本地坐标系下的位置 const localPos: Vec2 = this._node!.convertToNodeSpaceAR(touchPos); // 输出本地坐标系下的位置 console.log(`Local position: (${localPos.x}, ${localPos.y})`); } } ``` 该代码示例是一个自定义组件类,包含了组件的生命周期函数和触摸事件处理函数。在 `onEnable` 生命周期函数中获取了节点引用,并注册了触摸事件。在 `onDisable` 生命周期函数中取消了触摸事件的注册,并释放了节点引用。在触摸事件处理函数中,通过 `convertToNodeSpaceAR` 方法将触摸点位置转换为相对于 MyNode 节点的本地坐标系下的位置,并输出该位置信息。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值