cocos creator隨筆

接觸一個月cocos creator作個筆記。。。html

一、socket.io 封裝了websocket , Ajax等,故任何瀏覽器均可以使用socket.io創建異步鏈接。websocket 雙向須要握手創建鏈接,http 單向。node

短鏈接:一次性交換數據,如登入,建立角色等  長鏈接:推送web

(一) socket.emit('tell one person'); socket.broadcast.emit('tell to everyone'); socket.on('I heard',function(){...});跨域

二、 scrollview組件,只有content節點比scrollview大時,才能夠滾動。瀏覽器

三、mask屬於渲染組件,不能和其餘渲染組件共同存在於同一節點。(如sprite,label)性能優化

四、全局定義,a.使用window申明如:window.global = {...} ; b.使用cc 如cc.gv = {} ; cc.gv.voiceMgr = new voiceMgr();服務器

c.使用常駐節點:cc.game.addpersistRootNode(this.node);微信

d.module.exports = {...}; 每一個腳本都能用 require +文件名 獲取exports對象;websocket

(function(){
    var demo = function(options){
        ......
    }
    window.demo = demo;//把這個函數暴露給外部,以便全局調用
}())

五、事件派發架構

a.  target.on(window.myTypeA,this.callback,this);  

    this.node.emit(window.myTypeA,{...});

b.冒泡:(派發)let aEvent= new cc.Event.EventCustom("...", false)

                      aEvent.setUserData({...})

                aNode..dispatchEvent(testEvent);

            (監聽)this.node.on("...", function (event) { var data = event.getUserData();cc.log(JSON.stringify(data) });

6 、繼承:js原型鏈繼承prototype和基於類的繼承extends;

如:Player--> Actor--->cc.Commponent(extends); (注意:this._super()的使用);

組件基類:cc.Commponent(this.node  this.enable  update(dt)  onLoad()  start()

=>p.s. 當組件從disable回到enable,start會再次被調用, enable=>onEnable()  ; destroy=> onDestroy();

這裏值得說的就是構造函數:如

var Node = cc.Class({ctor:function(){this.name = 'node'}});

var Sprite = cc.Class({extends:Node,   ctor:function(){this.name = 'sprite'}});

不論子類是否認義構造函數,子類實例化前父類的構造函數都會自動被調用,寫個this._super()就多此一舉了。

還有一點,在構造函數被調用前,屬性已被賦值爲默認值的能夠在構造函數裏被訪問到。

七、獲取節點:

a.層次淺:this.node.getCommponent(...);   this.node.getChildByName(...);

b.層次深: cc.find('../../..',this.baseNode);從baseNode開始查找

              cc.find('../../..');從場景根節點開始查找

c.腳本屬性聲明 testNode:{default:null, type:cc.Node},編輯器(層級管理器)拖拽 , this.testNode調用

屬性聲明:簡單:height:10 ,pos:cc.Vec2 ,pos:new cc.Vec2(10,20),

              完整:score:{default:0, displayName:'score',tooltip:'this is score', visible: true, serializable: false}

另外:_width:0,width:{get: function(){return this._width;} set:function(value){this._width = value;}}

可在編輯器的屬性檢查器裏進行設置

八、this.node.children 只返回直接節點的子節點。   另:this.node.childrenCount;

九、一些常見的屬性  :mysprite.node.color = cc.Color.RED;(opacity)

this.node.position = cc.p(0,0); this.node.setPoition(0,0)||(cc.p(0,0));

node.setLocalZOrder();

(rotation setRotation,scale setScale, width height setContentSize , anchor setAnchorPoint等)

另分組: this.node.group = "itemGroup"

十、動態建立模式:

 let node = new cc.Node('...');

 let sp =node.addComponent(cc.Sprite);

 sp.spriteFrame = ...;

 node.parent = ...; (等同於 ...addChild(node))

 node.setPosition(...);

十一、 關於觸摸:上層節點註冊touch/mouse事件,下面節點是收不到的,除非父節點經過冒泡。(固然上層節點是下層節點的子節點,冒泡纔有效)

由於事件是沿着節點的parent向上冒泡的(觸摸事件只能在節點上添加)

十二、一些不經常使用的方法:(雞肋)

let children = cc.directoer.getScene().getChildren();

cc.game.setFrameRate(30);

(GL verts :顯卡繪製的頂點數。GL calls :渲染次數。表示表明每一幀中 OpenGL 指令被調用的次數,相同的GL calls 下 , GL verts 越少 , 繪製效率越高 , FPS越高 .)

1三、一句值得思考的話:js裏的函數和變量本質是同樣的(把函數看成參數);

let plus = function(a ,b){ return a+b};

let dd = function(f,a,b){return f(a,b)};

dd(plus,2,3);

1四、關於靜態變量: 可直接調用,不須要new一個對象,且子類變化父類不變,父類變化子類也一樣不會變化。

1五、 隱形回調:(我這麼定義了)

this.node.active = false; 組件上有onDisable()將被執行 同理 = true ;onEnable()將被執行。

1六、克隆已有節點與建立預製節點 :cc.instantitate;

1七、在svn同步上,更新後報錯,統一項目creator版本,把.meta加入svn。

1八、關於tiledMap:

加載:cc.loader.loadRes('map/'+name,function(err,map){ this._tiledMap.tmxAsset = map});

釋放:var deps = cc.loader.getDependsRecursively(tmxAsset);

        cc.loader,release(deps);

1九、一句關於creator的精髓:組件都是存粹的數據,只有系統纔可以操做他們。 

20、creator 跨域訪問資源服務器的問題 詳見:http://www.cnblogs.com/billyrun/articles/7029221.html

2一、 creator 使用自定義shader 詳見:http://blog.csdn.net/xufeng0991/article/details/72973664

2二、關於正在播放的動畫不重置:

this.animPlay("Jump");

animPlay:function (name) {

  var anim = this._player.getChildByName("anim").getComponent(cc.Animation);

  if(anim.currentClip && anim.currentClip.name == name){

    return;

  }

  anim.play(name);

}

 2三、曾遇到這樣一個坑: 建立一個newNode 在其上面添加了監聽事件,再添加到父節點,當不須要newNode的時候,調用了removeFromParent 方法,其默認爲true ,再次調用newNode的時候其上面的監聽已被移除。

官網移除推薦用: node.parent = null;

另附一些連接:

一、4個知識幫你輕鬆上手Cocos Creator

二、cocos2d-JS 性能優化 - 對象緩衝池 ( cc.pool )

三、anysdk打包微信登陸、微信分享

四、手機截屏分享朋友圈,電池電量,WIFI狀態

五、WebSocket中關於使用ProtoBuf傳輸數據介紹js部分

六、棋牌遊戲服務器架構設計

相關文章
相關標籤/搜索