当前位置:网站首页>Cocos学习日记2——脚本和属性
Cocos学习日记2——脚本和属性
2022-06-23 03:49:00 【herb.dr】
目录
6.1 TypeScript,强类型的 JavaScript
一、给节点添加脚本


二、VScode编辑脚本
2.1 配置 vscode 环境

2.2 编写脚本

三、有关脚本的知识
3.1 脚本的运行原理
1、游戏引擎加载所有节点
2、加载节点的所有的组件
var comp = new PigScript();
node["PigScript"]= comp;
comp.onLoad();
comp.start();
3.2 生命期回调
生命期回调 Life-Cycle Callback
onLoad() 组件初始化时执行
start() 第一次激活前执行
update() 每帧执行
onEnable() 组件启用时调用
onDisable() 组件禁用时调用
onDestroy()组件销毁时调用

3.3 查看脚本执行情况
1、模拟器

2、控制台

四、事件响应处理
事件处理,和 HTML 里的 JavaScript 事件类似
点击事件 mousedown / mouseup / mousemove ...
键盘事件 keyup / keydown
触摸事件 touchstart / touchend / touchcancel ...

上面的效果实现了:点击一下,图片位置就会左移
五、调试 JS 代码
目前还不会,这个用来占位
六、TypeScript
6.1 TypeScript,强类型的 JavaScript
https://www.typescriptlang.org
6.2 JavaScript 是弱类型的语言
var str = "shaofa";
var a = 10;
var node = this.node;由于没有类型标识,所以编辑器无法精确的提示
6.3 对比写法
1、变量
JavaScript:
var str = "shaofa";
var a = 10;
var node = this.node;
TypeScript:
let str : string = "shaofa";
let a : number = 10;
let node : cc.Node = this.node;
2、方法的参数
JavaScript:
moveLeft(evt) {
}
TypeScript:
moveLeft(evt : cc.Event.EventMouse) {
}
3、方法的返回值
JavaScript:
sum(x, y) {
return x + y;
}
TypeScript:
sum(x:number, y:number):number {
returnx + y;
}
七、属性
7.1 在引擎中显示属性


7.2 属性的定义
1、在 TypeScript 添加一个类,类里可以添加属性
形式如下:
@property("string")
time : string="";
其中,@property 称为装饰器(同 Java 中的注解语法)
2、属性的定义规则
(1) 若无 @property 注解,则不会出现在 Cocos Creator 属性面板
(2) @property 中, 应该指明类型
@property("string")
@property(cc.Node)
(3) 基本类型,可以不指定类型
string,number,boolean,bigint
3、TypeScript 里有 number 和 Number 两种类型
number 算是基本类型
Number 算是引用类型(对象)
7.3 属性的应用
属体一般分为两种类型:
1、基本类型
string,number,boolean,bigint
2、引用类型
cc.Node 节点
cc.SpriteFrame 图片帧资源
cc.AudioClip 音频资源
3、应用

7.4 引用类型属性

边栏推荐
猜你喜欢

How node+express operates cookies

svg d3. JS generate tree tree view

制造型企业开发的SRM供应商管理系统特点是什么

理想汽车×OceanBase:当造车新势力遇上数据库新势力

【二叉树进阶】AVLTree - 平衡二叉搜索树

JD cloud distributed database stardb won the "stability practice pioneer" of China Academy of information technology

There is a problem with redis startup

Code refactoring Guide

JVM调优简要思想及简单案例-为什么需要JVM调优?

12 excellent practices of wireless network security
随机推荐
[pycharm] ide Eval resetter
Black horse PostgreSQL, why is it black in the end
Can MySQL be used in Linux
2022年起重机械安全管理考试题库及答案
PTA:7-58 图书音像出租管理
Please use the NLTK Downloader to obtain the resource
LabVIEW在同一表中同时显示十六进制字符和普通字符
Leetcode 1208. 尽可能使字符串相等
Analysis on the current situation of the Internet of things in 2022
How node+express operates cookies
虫子 日期类 上 太子语言
leetcode 91. Decode Ways 解码方法(中等)
P1347 sorting (TOPO)
leetcode 91. Decode ways (medium)
PTA:7-87 集合的模拟实现(类模板)
Audio and video development journey (63) - animation and rendering of Lottie source code analysis
自动化测试常见的面试题
Cocos学习日记1——节点
Halcon glue line detection - template matching, pose transformation, glue width, glue continuity detection
Redis启动有问题