CocosCreator - 샘플

3870 단어

코드 예


1. 작은 공이 끊임없이 위아래로 뛴다

cc.Class({
    extends: cc.Component,
 
    properties: {
       jumpDuration:2,
       jumpHeight:300
    },
    ballJumpAction:function(){
        // 
        var jumpUp = cc.moveBy(this.jumpDuration,cc.p(0,this.jumpHeight)).easing(cc.easeCubicActionOut());
        // 
        var jumpDown = cc.moveBy(this.jumpDuration,cc.p(0,-this.jumpHeight)).easing(cc.easeCubicActionIn());
        // 
        return cc.repeatForever(cc.sequence(jumpUp ,jumpDown));
 
    },
    // use this for initialization
    onLoad: function () {
        this.jumpAction = this.ballJumpAction();
        this.node.runAction(this.jumpAction);
    },
 
    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {
 
    // },
});

2. 장면 전환, 카운트다운


장면 전환:
onLoad: function () {
        this.node.on('mousedown',function(){
            cc.director.loadScene('Scene2');
        })
    },

카운트다운 자동 장면 변환
// 
cc.Class({
    extends: cc.Component,
 
    properties: {
       timeLabel:{
           default:null,
           type:cc.Label
       }
    },
 
    // use this for initialization
    onLoad: function () {
        var timeIn=5;
        this.schedule(function(){
            timeIn--;
            this.time_Label.string=timeIn;
            if(timeIn===0){
                cc.director.loadScene('Scene3');
            }
        },1);
    },
 
    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {
 
    // },
});

3. 이벤트 감청 작업 입력

//  ASWD ;js ( ) 
cc.Class({
    extends: cc.Component,
    properties: {
        accl:0,
        plane:{
            default:null,
            type:cc.Node
        }
    },
    setInputControl:function(){
        var self = this;
        var listener= {
            event:cc.EventListener.KEYBOARD,
            onKeyPressed:function(keyCode,event){
                switch(keyCode){
                    case cc.KEY.a:
                        self.accLeft= true;
                        break;
                    case cc.KEY.d:
                        self.accRight= true;
                        break;
                    case cc.KEY.w:
                        self.accUp= true;
                        break;
                    case cc.KEY.s:
                        self.accDown= true;
                        break;
                }
            },
            onKeyReleased:function(keyCode,event){
                switch(keyCode){
                    case cc.KEY.a:
                        self.accLeft= false;
                        break;
                    case cc.KEY.d:
                        self.accRight= false;
                        break;
                    case cc.KEY.w:
                        self.accUp= false;
                        break;
                    case cc.KEY.s:
                        self.accDown= false;
                        break;
                }
            }
        }
        cc.eventManager.addListener(listener, self.node)
    },
 
    // use this for initialization
    onLoad: function () {
        this.accLeft = false;
        this.accRight = false;
        this.accUp = false;
        this.accDoen = false;
        this.setInputControl();
    },
 
    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        if(this.accLeft){
            this.plane.x -=this.accl;
        }
        if(this.accRight){
            this.plane.x +=this.accl;
        }
        if(this.accUp){
            this.plane.y +=this.accl;
        }
        if(this.accDown){
            this.plane.y -=this.accl;
        }
     },
});

orange.png

좋은 웹페이지 즐겨찾기