当前位置:网站首页>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 引用类型属性

边栏推荐
- Imitation 360 desktop suspended ball plug-in
- PTA:7-67 友元很简单2016final
- Idea import module
- JVM调优简要思想及简单案例-为什么需要JVM调优?
- 靜態查找錶和靜態查找錶
- 深度学习 TensorFlow入门
- Pytorch---Pytorch进行自定义Dataset
- 2022年起重机械安全管理考试题库及答案
- 8 key indicators to measure technology debt in 2022
- Review the SQL row column conversion, and the performance has been improved
猜你喜欢

Pytoch --- pytoch customizes the dataset

12 excellent practices of wireless network security

Particle animation background login page particles js

摆烂LuoGu刷题记

Please use the NLTK Downloader to obtain the resource

Review the SQL row column conversion, and the performance has been improved

Inscription of lougu brush

LabVIEW displays both hexadecimal and normal characters in the same table

Avltree - arbre de recherche binaire équilibré

IDEA-导入模块
随机推荐
Ms-fsrvp forced abuse of POC
静态查找表和静态查找表
[deep learning] deep learning reasoning framework tensorrt MNN openvino onnxruntime
A summary of PostgreSQL data types. All the people are here
Leetcode 1208. Try to make the strings equal as much as possible (finally solved, good night)
解决使用Exception抛出后,@Transactional不生效
【二叉樹進階】AVLTree - 平衡二叉搜索樹
Static lookup tables and static lookup tables
PTA:6-30 时间相加
Bug STM32 interrupt (everyone knows)
Pta:7-85 data spacing (overload + function template)
Does the network disk also roll inside?
P1363 幻象迷宫(dfs)
After the exception is thrown, the @transactional does not take effect
Zhongang Mining: the demand for fluorite in the new energy and new material industry chain has increased greatly
photoshop PS 查看像素坐标、像素颜色、像素HSB颜色
Halcon胶线检测—模板匹配、位姿变换、胶宽,胶连续性检测
Lighthouse locally deployed TCA code analysis tool
What are the characteristics of SRM supplier management system developed by manufacturing enterprises
Monitoring artifact ZABBIX, from deployment to application, goes deep layer by layer