【COCOS CREATOR 系列教程之二】脚本开发篇&事件监听、常用函数等示例整合

简介:

上一篇,介绍了Himi在使用过cc所有组件后的一篇总结,没有具体介绍每个组件的原因在于官方文档很齐全,而且也有视频的介绍。

所以希望童鞋们可以把我这两篇博文当成对组件、脚本两部分开发的整理与总结。

后续的文章,Himi应该主要更新一些官方还未补充或者还没有的教程。避免无用功。

下面直接放出代码,因为不是很难理解。所以不再一一赘述,都是常用的函数、事件监听、动作回调、定时器等开发过程中必接触的。

大致内容如下:

  1. cc 属性介绍

  2. 获取组件的几种形式

  3. 全局变量的访问

  4. 模块之间的访问

  5. 在当前节点下添加一个组件

  6. 复制节点/或者复制 prefab

  7. 销毁节点(销毁节点并不会立刻发生,而是在当前 帧逻辑更新结束后,统一执行)

  8. 事件监听 on 4种形式(包括坐标获取)

  9. 关闭监听

  10. 发射事件(事件手动触发)

  11. 动作示例,类似c2dx api 基本无变化

  12. 计时器 (component)schedule (cc.Node 不包含计时器相关 API)

  13. url raw资源获取



CC版本:0.7.1

源码下载地址:   http://vdisk.weibo.com/s/yZxRoLm4Mnio3                     

主要两个js源码:

HelloWorld.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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
cc.Class({
     extends: cc.Component,
  
     properties: {
         label: {
             default null ,
             type: cc.Label
         },
         text:  'Hello, World!' ,
         
         t_prefab:{
             default : null ,
             type:cc.Prefab
         },
         
         t_sprite:{ //定义一个cc的类型,并定义上常用属性
             default : null ,
             type:cc.SpriteFrame, //类型的定义
             // url:cc.Texture2D, //Raw Asset(cc.Texture2D, cc.Font, cc.AudioClip) 
             visible: true , //属性检查器中是否可见
             displayName: 'himi' , //属性检查器中属性的名字
             tooltip: "测试脚本" , //属性检查器中停留此属性名称显示的提示文字
             readonly: false , //属性检查器中显示(readonly)且不可修改[当前有bug,设定只读也能修改]
             serializable: true , //设置false就是临时变量
             editorOnly: false //导出项目前剔除此属性
         },
         
         t_url:{
             default : null ,
             url:cc.Texture2D
         },
         
         t_count_2:200, //基础类型
         
         //可以只定义 get 方法,这样相当于一份 readonly 的属性。[当前有bug,只设定get也能修改]
         t_getSet:{
             default :12, 
             get: function (){ return  this .t_getSet}, //get
             set: function (value){ this .t_getSet =value;} //set
         },
             
         
         t_array:{ //定义一个数组
             default :[],
             type:[cc.Sprite]
         }
     },
  
     // use this for initialization
     onLoad:  function  () {
         
         //--->>> 获取组件的几种形式:
         //1. 通过属性检查器被赋值的label组件,直接拿到得到实例
         //2. 通过属性检查器被赋值的label组件所在的node节点,然后通过getComponent获取
         // this.label.string = this.text;
         
         //3. 获取当前this(node)节点上的label组件
         // var _label = this.getComponent(cc.Label);
         
         //4. 先获取目标组件所在的节点,然后通过getComponent获取目标组件
         var  _label = cc.find( "Canvas/label" ).getComponent(cc.Label);
         
         //5.也可以如下形式【注意此种方式,目前有BUG,无法正常使用 (0.7.1) 】
         // var _label = cc.find("Canvas/label<cc.Label>");
         
         console.log(_label.string);
         console.log( this .t_getSet);
         
         //--->>>全局变量的访问
         /* 任意脚本中定义如下:【注意不要有var哦】
         
             t_global = {
                 tw:100,
                 th:200
             };
         
         */
         t_global.th = 2000;
         console.log(t_global.th);
         
         //--->>>模块之间的访问
         /*任意脚本中定义如下 【注意关键字是module.exports】
         
             module.exports= {
                 tme_pa1:"100",
                 tme_pa2:333221
             };
             
         */
         //--->>>用 require + 文件名(不含路径) 来获取到其他 模块 的对象
         var  tModuleData = require( "testJs" );
         tModuleData.tme_pa2 = 991;
         console.log(tModuleData.tme_pa2); 
         
         
         //--->>>在当前节点下添加一个组件
         var  mySprite =  new  cc.Node().addComponent(cc.Sprite); 
         mySprite.spriteFrame =  this .t_sprite;
         mySprite.node.parent =  this .node;
         mySprite.node.setPosition(300,200);
         
         
         //--->>>复制节点/或者复制 prefab
         //复制节点
         var  lLabel = cc.instantiate( this .label);
         lLabel.node.parent =  this .node;
         lLabel.node.setPosition(-200,0);
         //复制prefab
         var  tPrefab = cc.instantiate( this .t_prefab);
         tPrefab.parent =  this .node;
         tPrefab.setPosition(-210,100);
         
         
         //--->>>  销毁节点(销毁节点并不会立刻发生,而是在当前 帧逻辑更新结束后,统一执行)
         if  (cc.isValid( this .label.node) ) {
             console.log( "有效存在,进行摧毁" );
             this .label.destroy();
         } else {
             console.log( "已摧毁" );
         }
         
         //--->>> 事件监听 on 4种形式
         //枚举类型注册
         var  tFun = function  (event){
           console.log( "touchend event:" +event.touch.getLocation().x + "|" +event.touch.getLocation().y); 
         };
         this .node.on(cc.Node.EventType.TOUCH_END,tFun, this ); 
         
         //事件名注册
         // var tFun =function (event){
         //   console.log("touchend event"); 
         // };
         // this.node.on("touchend",tFun);
         
         // this.node.on("touchend",function (event){
         //   console.log("touchend event"); 
         // });
         
         // this.node.on("touchend",function (event){
         //   console.log("touchend event"); 
         // },this);
         
         // this.node.on("touchend",function (event){
         //   console.log("touchend event"); 
         // }.bind(this));
         
         //--->>> 一次性的事件监听 once
         // this.node.once("touchend",function (event){
         //   console.log("touchend event"); 
         // });
         
         
         //--->>> 关闭监听
         this .node.off( "touchend" ,tFun, this );
         
         
         //--->>> 发射事件(事件手动触发)
         this .node.on( "tEmitFun" , function  (event){ 
             console.log( "tEmitFun event:" +event.detail.himi+ "|" +event.detail.say); 
             
             //-->>> 事件中断,如下函数阻止事件向当前父级进行事件传递
             // event.stopPropagation();
         });
         this .node.emit( "tEmitFun" ,{himi:27,say: "hello,cc!" });
         
         
         //--->>> 动作,类似c2dx api 基本无变化
         var  mTo = cc.moveBy(1,-100, -200);
         var  mAction = cc.repeatForever(cc.sequence(cc.moveBy(1,-100, -200),mTo.reverse(),cc.delayTime(0.5),cc.callFunc( function (action,data){
             console.log( "action callback:" +data.himi);
         }, this ,{tx:100,himi: "i'm action callback and bring data" })));
         mySprite.node.runAction(mAction);
         //暂停动作
         mySprite.node.stopAction(mAction);
         
         
         //--->>> 计时器 (component)schedule (cc.Node 不包含计时器相关 API)
         //参数: call funtion/interval/repeat times/delay time
         //不延迟,永久重复
         this .schedule( function (){
             console.log( "schedule log..." );
         },1);
         
         //不延迟,有重复次数限定
         // this.schedule(function(){
         //     console.log("schedule log...");
         // },1,2);
         
         //重复2次,重复间隔为1秒,延迟1秒进行
         // this.schedule(function(){
         //     console.log("schedule log...");
         // },1,2,1);
         
         //一次性的计时器
         var  mySch = function (){ console.log( "schedule Once log..." ); }
         this .scheduleOnce(mySch);
         
         //取消定时器
         this .unschedule(mySch);
         
         
         //--->>> url raw资源获取
         var  mSf =  new  cc.Node().addComponent(cc.Sprite);
         mSf.spriteFrame =  this .t_sprite;
         mSf.spriteFrame.setTexture( this .t_url);
         mSf.node.setPosition(400,0);
         mSf.node.parent =  this .node;
         mSf.node.setScale(0.5);
         
         //获得 Raw Asset 的 url
         var  mUrl = cc.textureCache.addImage(cc.url.raw( "himi.png" ));
         console.log( "raw asset url:" +mUrl);
         
       
     },
  
     // called every frame
     update:  function  (dt) {
         // if (cc.isValid(this.label.node) ) {
         //     console.log("有效存在,进行摧毁");
         // }else{
         //     console.log("已摧毁");
         // }
     },
});

testJs.js

1
2
3
4
5
6
7
8
9
t_global = {
     tw:100,
     th:200
};
  
module.exports= {
     tme_pa1: "100" ,
     tme_pa2:333221
};





本文转自 xiaominghimi 51CTO博客,原文链接:http://blog.51cto.com/xiaominghimi/1740512,如需转载请自行联系原作者

目录
相关文章
|
4月前
CocosCreator 面试题(十六)Cocos Creator 节点池的基本原理是什么?如何使用?
CocosCreator 面试题(十六)Cocos Creator 节点池的基本原理是什么?如何使用?
131 0
|
开发者 Python
Python Qt GUI设计:信号与槽的使用方法(基础篇—7)
Python Qt GUI设计:信号与槽的使用方法(基础篇—7)
Python Qt GUI设计:信号与槽的使用方法(基础篇—7)
|
6月前
|
Dart C++
带你读《深入浅出Dart》二十四、编写第一个Flutter应用
带你读《深入浅出Dart》二十四、编写第一个Flutter应用
|
7月前
|
前端开发 开发者
SAP UI5 初学者教程之二十 - SAP UI5 的表达式绑定用法讲解
SAP UI5 初学者教程之二十 - SAP UI5 的表达式绑定用法讲解
43 0
|
8月前
|
异构计算 索引 容器
creator源码阅读系列第二篇之渲染
creator源码阅读系列第二篇之渲染
|
8月前
|
缓存 JavaScript iOS开发
iOS 逆向编程(十五)Cycript 语法进阶(封装 .cy 脚本文件)
iOS 逆向编程(十五)Cycript 语法进阶(封装 .cy 脚本文件)
118 0
Qt实用技巧:VS2017编写纯C库以及使用Qt调用C库方法
Qt实用技巧:VS2017编写纯C库以及使用Qt调用C库方法
Qt实用技巧:VS2017编写纯C库以及使用Qt调用C库方法
|
IDE 测试技术 API
1篇文章带你了解poco的所有基本功能
1篇文章带你了解poco的所有基本功能
1338 0
|
Python
【PyQt】记录各种组件的使用方法【上】
使用Python开发GUI界面工具,通常可用选择PyQt框架,非常易使用,而且网上也有很多相关的文档分享。我也是在写了多个GUI界面后,自己整理了一些常用的使用文档,在此分享给大家(PyQt4~PyQt5适用)。
234 0
【PyQt】记录各种组件的使用方法【上】
|
Python
【PyQt】记录各种组件的使用方法【下】
使用Python开发GUI界面工具,通常可用选择PyQt框架,非常易使用,而且网上也有很多相关的文档分享。我也是在写了多个GUI界面后,自己整理了一些常用的使用文档,在此分享给大家(PyQt4~PyQt5适用)。
265 0
【PyQt】记录各种组件的使用方法【下】