Creator學(xué)習(xí)筆記1


1.除了方法, 其他東西都要扔到properties里面了, 并且要給出屬性的默認(rèn)值或者屬性的存取的方法

1) 首先呢? 是繼承自組件 (extends: cc.Component)

2)除了開放的extends接口, properties接口,

還有常用的onLoad接口和update接口

※onLoad會在組件加載的時候調(diào)用

※update會在每幀刷新的時候調(diào)用, 理論上是一秒執(zhí)行60次update??得到的參數(shù)是與上次刷新的時間間隔

3)我們有了一個想法,我想做一個游戲,很棒的游戲

ok,我們要用CCC做,

那我們究竟怎么做呢

很簡單,三步

1.把你所設(shè)想的游戲界面都布置好,有幾個場景布置幾個場景,不想看到的先在屬性檢查器把active關(guān)掉,想動態(tài)生成的就扔到資源管理器當(dāng)prefab

2.編寫游戲的邏輯,寫什么邏輯呢?兩個字,就是監(jiān)聽,各種聽,如果是邏輯的入口,就聽系統(tǒng)事件,如果不是,就聽自定義事件,然后該干嘛干嘛,用到的資源如果在資源管理器就require或者load,在層級管理器的就在properties開接口

3.關(guān)聯(lián)界面和邏輯,在屬性檢查器把整個監(jiān)聽的邏輯關(guān)系網(wǎng)織起來

以下引用Himi

大致內(nèi)容如下:

cc 屬性介紹

獲取組件的幾種形式

全局變量的訪問

模塊之間的訪問

在當(dāng)前節(jié)點下添加一個組件

復(fù)制節(jié)點/或者復(fù)制 prefab

銷毀節(jié)點(銷毀節(jié)點并不會立刻發(fā)生,而是在當(dāng)前 幀邏輯更新結(jié)束后,統(tǒng)一執(zhí)行)

事件監(jiān)聽 on 4種形式(包括坐標(biāo)獲取)

關(guān)閉監(jiān)聽

發(fā)射事件(事件手動觸發(fā))

動作示例,類似c2dx api 基本無變化

計時器 (component)schedule (cc.Node 不包含計時器相關(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:{//定義一個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)且不可修改[當(dāng)前有bug,設(shè)定只讀也能修改]

serializable:true,//設(shè)置false就是臨時變量

editorOnly:false//導(dǎo)出項目前剔除此屬性

},

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:{//定義一個數(shù)組

default:[],

type:[cc.Sprite]

}

},

// use this for initialization

onLoad: function () {

//--->>> 獲取組件的幾種形式:

//1. 通過屬性檢查器被賦值的label組件,直接拿到得到實例

//2. 通過屬性檢查器被賦值的label組件所在的node節(jié)點,然后通過getComponent獲取

// this.label.string = this.text;

//3. 獲取當(dāng)前this(node)節(jié)點上的label組件

// var _label = this.getComponent(cc.Label);

//4. 先獲取目標(biāo)組件所在的節(jié)點,然后通過getComponent獲取目標(biāo)組件

var _label = cc.find("Canvas/label").getComponent(cc.Label);

//5.也可以如下形式【注意此種方式,目前有BUG,無法正常使用 (0.7.1) 】

// var _label = cc.find("Canvas/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);

//--->>>模塊之間的訪問

/*任意腳本中定義如下 【注意關(guān)鍵字是module.exports】

module.exports= {

tme_pa1:"100",

tme_pa2:333221

};

*/

//--->>>用 require + 文件名(不含路徑) 來獲取到其他 模塊 的對象

var tModuleData = require("testJs");

tModuleData.tme_pa2 = 991;

console.log(tModuleData.tme_pa2);

//--->>>在當(dāng)前節(jié)點下添加一個組件

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é)點/或者復(fù)制 prefab

//復(fù)制節(jié)點

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é)點(銷毀節(jié)點并不會立刻發(fā)生,而是在當(dāng)前 幀邏輯更新結(jié)束后,統(tǒng)一執(zhí)行)

if (cc.isValid(this.label.node) ) {

console.log("有效存在,進行摧毀");

this.label.destroy();

}else{

console.log("已摧毀");

}

//--->>> 事件監(jiān)聽 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));

//--->>> 一次性的事件監(jiān)聽 once

// this.node.once("touchend",function (event){

//?? console.log("touchend event");

// });

//--->>> 關(guān)閉監(jiān)聽

this.node.off("touchend",tFun,this);

//--->>> 發(fā)射事件(事件手動觸發(fā))

this.node.on("tEmitFun",function (event){

console.log("tEmitFun event:"+event.detail.himi+"|"+event.detail.say);

//-->>> 事件中斷,如下函數(shù)阻止事件向當(dāng)前父級進行事件傳遞

// 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 不包含計時器相關(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秒進行

// 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("已摧毀");

// }

},

});

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容