大佬教程收集整理的这篇文章主要介绍了【COCOS CREATOR 系列教程之二】脚本开发篇&事件监听、常用函数等示例整合,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
【Cocos Creator 】(千人群): 432818031
上一篇,介绍了Himi在使用过cc所有组件后的一篇总结,没有具体介绍每个组件的原因在于官方文档很齐全,而且也有视频的介绍。
所以希望童鞋们可以把我这两篇博文当成对组件、脚本两部分开发的整理与总结。
后续的文章,Himi应该主要更新一些官方还未补充或者还没有的教程。避免无用功。
下面直接放出代码,因为不是很难理解。所以不再一一赘述,都是常用的函数、事件监听、动作回调、定时器等开发过程中必接触的。
大致内容如下:
CC版本:0.7.1
源码下载地址: http://vdisk.weibo.com/s/yZxRoLm4Mnio3
主要两个js源码:
HelloWorld.js
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
@H_618_244@
81
82
83
84
85
86
87
88
89
@H_556_262@
90
91
92
93
94
95
@H_675_274@
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
@H_450_304@
111
112
113
114
115
116
117
118
@H_262_320@
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
@H_562_450@
184
185
186
187
188
189
190
191
192
@H_696_468@
193
194
195
196
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
extends
:
.
Component
,
label
{
default
null
type
.
Label
}
607@
text
'Hello,World!'
616@
:
:
.
Prefab
Striped-line" id="crayon-56af296a31784678474750-14" style="margin: 0px; padding: 0px 5px; border: 0px; font-style: inherit; font-variant: inherit; height: inherit; font-size: inherit !important; line-height: inherit !important; BACkground-color: rgb(247,
t_sprite
{
//定义一个cc的类型,并定义上常用属性
SpriteFrame
//类型的定义
// url:cc.Texture2D,//Raw Asset(cc.Texture2D,cc.Font,cc.AudioClip)
visible
true
//属性检查器中是否可见
@H_426_674@
displayName
:
'himi'
//属性检查器中属性的名字
tooltip
"测试脚本"
//属性检查器中停留此属性名称显示的提示文字
Striped-line" id="crayon-56af296a31784678474750-26" style="margin: 0px; padding: 0px 5px; border: 0px; font-style: inherit; font-variant: inherit; height: inherit; font-size: inherit !important; line-height: inherit !important; BACkground-color: rgb(247,
t_url
{
url
.
Texture2D
t_count_2
:
200
//基础类型
//可以只定义 get 方法,这样相当于一份 readonly 的属性。[当前有bug,只设定get也能修改]
t_getSet
{
12
Striped-line" id="crayon-56af296a31784678474750-40" style="margin: 0px; padding: 0px 5px; border: 0px; font-style: inherit; font-variant: inherit; height: inherit; font-size: inherit !important; line-height: inherit !important; BACkground-color: rgb(247,
t_array
//定义一个数组
[
]
[
Sprite
]
}
// use this for initialization
//--->>> 获取组件的几种形式:
//1. 通过属性检查器被赋值的label组件,直接拿到得到实例
//2. 通过属性检查器被赋值的label组件所在的node节点,然后通过getComponent获取
//3. 获取当前this(nodE)节点上的label组件
//4. 先获取目标组件所在的节点,然后通过getComponent获取目标组件
var
_label
=
.
find
(
"Canvas/label"
getComponent
Label
;
//5.也可以如下形式【注意此种方式,目前有BUG,无法正常使用 (0.7.1) 】
// var _label = cc.find("Canvas/label<cc.Label>");
(
;
//--->>>全局变量的访问
/* 任意脚本中定义如下:【注意不要有var哦】
t_global = {
tw:100,
th:200
};
*/
t_global
th
2000
;
th
;
//--->>>模块之间的访问
/*任意脚本中定义如下 【注意关键字是module.exports】
tme_pa1:"100",
tme_pa2:333221
};
*/
//--->>>用 @R_944_10613@ire + 文件名(不含路径) 来获取到其他 模块 的对象
tModuleData
tme_pa2
991
;
tme_pa2
;
//--->>>在当前节点下添加一个组件
@H_607_1091@mySprite
spriteFrame
t_sprite
;
node
parent
;
setPosition
(
300
;
//--->>>复制节点/或者复制 prefab
//复制节点
lLabel
instantiate
label
;
lLabel
;
(
-
0
;
//复制prefab
tPrefab
t_prefab
;
tPrefab
;
210
100
;
//--->>>销毁节点(销毁节点并不会立刻发生,而是在当前 帧逻辑更新结束后,统一执行)
"有效存在,进行摧毁"
;
destroy
;
}
else
{
"已摧毁"
;
}
//--->>> 事件监听 on 4种形式
//枚举类型注册
;
on
Node
EventType
TOUCH_END
tFun
197_1282@
//事件名注册
// var tFun =function (event){
// console.log("touchend event");
// };
{
// console.log("touchend event");
// });
{
// console.log("touchend event");
// },this);
{
// console.log("touchend event");
//--->>> 一次性的事件监听 once
// console.log("touchend event");
// });
//--->>> 关闭监听
off
"touchend"
;
"tEmitFun"
{
"tEmitFun event:"
detail
himi
say
//-->>> 事件中断,如下函数阻止事件向当前父级进行事件传递
// event.stopPropagation();
;
//--->>> 动作,类似c2dx api 基本无变化
@H_919_1450@mTo
@H_146_1084@moveBy
1
;
@H_777_578@mAction
repeatForever
sequence
@H_415_782@mTo
reverse
delayTime
0.5
callFunc
(
action
data
{
runAction
@H_415_782@mAction
;
//暂停动作
stopAction
;
//--->>> 计时器 (component)schedule (cc.Node 不包含计时器相关 API)
//参数: call funtion/interval/repeat times/delay time
//不延迟,永久重复
"schedule log..."
;
;
//不延迟,有重复次数限定
);
//重复2次,重复间隔为1秒,延迟1秒进行
Striped-line" id="crayon-56af296a31784678474750-192" style="margin: 0px; padding: 0px 5px; border: 0px; font-style: inherit; font-variant: inherit; height: inherit; font-size: inherit !important; line-height: inherit !important; BACkground-color: rgb(247,2,1);
//一次性的计时器
//取消定时器
unschedule
;
//--->>> url raw资源获取
@H_777_578@mSf
;
@H_131_1633@mSf
;
spriteFrame
setTexture
t_url
;
400
;
;
setScale
;
//获得 Raw Asset 的 url
"raw asset url:"
@H_245_1261@mUrl
;
// console.log("有效存在,进行摧毁");
// }else{
// console.log("已摧毁");
// }
Striped-line" id="crayon-56af296a31784678474750-224" style="margin: 0px; padding: 0px 5px; border: 0px; font-style: inherit; font-variant: inherit; height: inherit; font-size: inherit !important; line-height: inherit !important; BACkground-color: rgb(247,
;
|
testJs.js
以上是大佬教程为你收集整理的【COCOS CREATOR 系列教程之二】脚本开发篇&事件监听、常用函数等示例整合全部内容,希望文章能够帮你解决【COCOS CREATOR 系列教程之二】脚本开发篇&事件监听、常用函数等示例整合所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。