cocos2D-x-新知识

来源:互联网 发布:万能摄像头软件下载 编辑:程序博客网 时间:2024/06/16 19:29
1.你会经常需要获得同一个节点上的其它组件,这就要用到 getComponent 这个 API,它会帮你查找你要的组件。
start: function () {
        var label = this.getComponent(cc.Label);
        var text = this.name + ' started';


        // Change the text in Label Component
        label.string = text;
    }




2.上下左右移动事件模版
var Left = cc.moveTo(1,cc.p(-0,0));
var rigth = cc.moveTo(1,cc.p(0,0));
return cc.repeatForever(cc.sequence( Left , rigth ));


3.自定义事件和以前学的小区别

window.GAMEDATA={
    USER_CLICK_SHRED_FIWEN:"USER_CLICK_SHRED_FIWEN"
};


cc.eventManager.dispatchCustomEvent(GAMEDATA.USER_CLICK_SHRED_FIWEN,this.node)



cc.eventManager.addCustomListener(GAMEDATA.USER_CLICK_SHRED_FIWEN, this.fiwe.bind(this)) 

fiwe:function(event){
        var data = event.getUserData();




4.画布移动事件 

update:function(dt){
        this.node.x -= this._bug*this._bugs
        this.node.y -= this._bugt*this._bugs
        var bugPosition = this.node.getPosition()
        bugPosition = this.node.parent.convertToWorldSpace(bugPosition)     
        if(bugPosition.x < 0||bugPosition.x > cc.winSize.width){
            this._bug *= -1; 
        }
        if(bugPosition.y < 0||bugPosition.y > cc.winSize.height){
            this._bugt *= -1;           
        }
    },



5.计时器

cc.eventManager.addListener(listener, this.node)

0 0