1.除了方法运沦, 其他東西都要扔到properties里面了, 并且要給出屬性的默認(rèn)值或者屬性的存取的方法
1) 首先呢? 是繼承自組件 (extends: cc.Component)
2)除了開(kāi)放的extends接口, properties接口,
還有常用的onLoad接口和update接口
※onLoad會(huì)在組件加載的時(shí)候調(diào)用
※update會(huì)在每幀刷新的時(shí)候調(diào)用, 理論上是一秒執(zhí)行60次update??得到的參數(shù)是與上次刷新的時(shí)間間隔
3)我們有了一個(gè)想法,我想做一個(gè)游戲,很棒的游戲
ok蓬坡,我們要用CCC做,
那我們究竟怎么做呢
很簡(jiǎn)單卓舵,三步
1.把你所設(shè)想的游戲界面都布置好弊仪,有幾個(gè)場(chǎng)景布置幾個(gè)場(chǎng)景畔柔,不想看到的先在屬性檢查器把a(bǔ)ctive關(guān)掉氯夷,想動(dòng)態(tài)生成的就扔到資源管理器當(dāng)prefab
2.編寫游戲的邏輯,寫什么邏輯呢靶擦??jī)蓚€(gè)字腮考,就是監(jiān)聽(tīng)雇毫,各種聽(tīng),如果是邏輯的入口踩蔚,就聽(tīng)系統(tǒng)事件棚放,如果不是,就聽(tīng)自定義事件馅闽,然后該干嘛干嘛飘蚯,用到的資源如果在資源管理器就require或者load,在層級(jí)管理器的就在properties開(kāi)接口
3.關(guān)聯(lián)界面和邏輯福也,在屬性檢查器把整個(gè)監(jiān)聽(tīng)的邏輯關(guān)系網(wǎng)織起來(lái)
以下引用Himi
大致內(nèi)容如下:
cc 屬性介紹
獲取組件的幾種形式
全局變量的訪問(wèn)
模塊之間的訪問(wèn)
在當(dāng)前節(jié)點(diǎn)下添加一個(gè)組件
復(fù)制節(jié)點(diǎn)/或者復(fù)制 prefab
銷毀節(jié)點(diǎn)(銷毀節(jié)點(diǎn)并不會(huì)立刻發(fā)生局骤,而是在當(dāng)前 幀邏輯更新結(jié)束后,統(tǒng)一執(zhí)行)
事件監(jiān)聽(tīng) on 4種形式(包括坐標(biāo)獲缺┐铡)
關(guān)閉監(jiān)聽(tīng)
發(fā)射事件(事件手動(dòng)觸發(fā))
動(dòng)作示例峦甩,類似c2dx api 基本無(wú)變化
計(jì)時(shí)器 (component)schedule (cc.Node 不包含計(jì)時(shí)器相關(guān) API)
url raw資源獲取
cc.Class({
extends: cc.Component,
properties: {
label: {
default: null,
type: cc.Label
},
text: 'Hello, World!',
t_prefab:{
default:null,
type:cc.Prefab
},
t_sprite:{//定義一個(gè)cc的類型,并定義上常用屬性
default:null,
type:cc.SpriteFrame,//類型的定義
// url:cc.Texture2D, //Raw Asset(cc.Texture2D, cc.Font, cc.AudioClip)
visible:true,//屬性檢查器中是否可見(jiàn)
displayName:'himi',//屬性檢查器中屬性的名字
tooltip:"測(cè)試腳本",//屬性檢查器中停留此屬性名稱顯示的提示文字
readonly:false,//屬性檢查器中顯示(readonly)且不可修改[當(dāng)前有bug现喳,設(shè)定只讀也能修改]
serializable:true,//設(shè)置false就是臨時(shí)變量
editorOnly:false//導(dǎo)出項(xiàng)目前剔除此屬性
},
t_url:{
default:null,
url:cc.Texture2D
},
t_count_2:200,//基礎(chǔ)類型
//可以只定義 get 方法凯傲,這樣相當(dāng)于一份 readonly 的屬性。[當(dāng)前有bug嗦篱,只設(shè)定get也能修改]
t_getSet:{
default:12,
get:function(){return this.t_getSet},//get
set:function(value){this.t_getSet =value;}//set
},
t_array:{//定義一個(gè)數(shù)組
default:[],
type:[cc.Sprite]
}
},
// use this for initialization
onLoad: function () {
//--->>> 獲取組件的幾種形式:
//1. 通過(guò)屬性檢查器被賦值的label組件冰单,直接拿到得到實(shí)例
//2. 通過(guò)屬性檢查器被賦值的label組件所在的node節(jié)點(diǎn),然后通過(guò)getComponent獲取
// this.label.string = this.text;
//3. 獲取當(dāng)前this(node)節(jié)點(diǎn)上的label組件
// var _label = this.getComponent(cc.Label);
//4. 先獲取目標(biāo)組件所在的節(jié)點(diǎn)灸促,然后通過(guò)getComponent獲取目標(biāo)組件
var _label = cc.find("Canvas/label").getComponent(cc.Label);
//5.也可以如下形式【注意此種方式诫欠,目前有BUG,無(wú)法正常使用 (0.7.1) 】
// var _label = cc.find("Canvas/label");
console.log(_label.string);
console.log(this.t_getSet);
//--->>>全局變量的訪問(wèn)
/* 任意腳本中定義如下:【注意不要有var哦】
t_global = {
tw:100,
th:200
};
*/
t_global.th = 2000;
console.log(t_global.th);
//--->>>模塊之間的訪問(wèn)
/*任意腳本中定義如下 【注意關(guān)鍵字是module.exports】
module.exports= {
tme_pa1:"100",
tme_pa2:333221
};
*/
//--->>>用 require + 文件名(不含路徑) 來(lái)獲取到其他 模塊 的對(duì)象
var tModuleData = require("testJs");
tModuleData.tme_pa2 = 991;
console.log(tModuleData.tme_pa2);
//--->>>在當(dāng)前節(jié)點(diǎn)下添加一個(gè)組件
var mySprite = new cc.Node().addComponent(cc.Sprite);
mySprite.spriteFrame = this.t_sprite;
mySprite.node.parent = this.node;
mySprite.node.setPosition(300,200);
//--->>>復(fù)制節(jié)點(diǎn)/或者復(fù)制 prefab
//復(fù)制節(jié)點(diǎn)
var lLabel = cc.instantiate(this.label);
lLabel.node.parent = this.node;
lLabel.node.setPosition(-200,0);
//復(fù)制prefab
var tPrefab = cc.instantiate(this.t_prefab);
tPrefab.parent = this.node;
tPrefab.setPosition(-210,100);
//--->>>??銷毀節(jié)點(diǎn)(銷毀節(jié)點(diǎn)并不會(huì)立刻發(fā)生浴栽,而是在當(dāng)前 幀邏輯更新結(jié)束后呕诉,統(tǒng)一執(zhí)行)
if (cc.isValid(this.label.node) ) {
console.log("有效存在,進(jìn)行摧毀");
this.label.destroy();
}else{
console.log("已摧毀");
}
//--->>> 事件監(jiān)聽(tīng) on 4種形式
//枚舉類型注冊(cè)
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);
//事件名注冊(cè)
// 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));
//--->>> 一次性的事件監(jiān)聽(tīng) once
// this.node.once("touchend",function (event){
//?? console.log("touchend event");
// });
//--->>> 關(guān)閉監(jiān)聽(tīng)
this.node.off("touchend",tFun,this);
//--->>> 發(fā)射事件(事件手動(dòng)觸發(fā))
this.node.on("tEmitFun",function (event){
console.log("tEmitFun event:"+event.detail.himi+"|"+event.detail.say);
//-->>> 事件中斷,如下函數(shù)阻止事件向當(dāng)前父級(jí)進(jìn)行事件傳遞
// event.stopPropagation();
});
this.node.emit("tEmitFun",{himi:27,say:"hello,cc!"});
//--->>> 動(dòng)作吃度,類似c2dx api 基本無(wú)變化
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);
//暫停動(dòng)作
mySprite.node.stopAction(mAction);
//--->>> 計(jì)時(shí)器 (component)schedule (cc.Node 不包含計(jì)時(shí)器相關(guān) API)
//參數(shù): call funtion/interval/repeat times/delay time
//不延遲,永久重復(fù)
this.schedule(function(){
console.log("schedule log...");
},1);
//不延遲贴硫,有重復(fù)次數(shù)限定
// this.schedule(function(){
//???? console.log("schedule log...");
// },1,2);
//重復(fù)2次椿每,重復(fù)間隔為1秒,延遲1秒進(jìn)行
// this.schedule(function(){
//???? console.log("schedule log...");
// },1,2,1);
//一次性的計(jì)時(shí)器
var mySch =function(){ console.log("schedule Once log..."); }
this.scheduleOnce(mySch);
//取消定時(shí)器
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("有效存在英遭,進(jìn)行摧毀");
// }else{
//???? console.log("已摧毀");
// }
},
});