橡皮怪main.js

cc.Class({
    extends: cc.Component,  //继承属性类
    properties: 
    {
        _LEFT: -1,
        _RIGHT: 1,
        score:0,
        playTime:60,        //游戏时间,倒计时
        playTime:90,
        diciCount:0,        //地刺数量
        dc_duration:320,   //地刺的间隔距离
        dici:cc.Prefab,    //地刺预制件
       // dflasf:23,        //自定义后property中就会包含定义的属性
        //主角节点
        player:cc.Node,  
        //背景音乐
        bgAudio:
        {
           default:null, 
           url:cc.AudioClip                                       
        },
        //主角跳跃音效     
        jumpAudio:
        {
           default:null,
           url:cc.AudioClip  //音频组件
        },
        //进度条
        progressBar: 
        {
            default: null,
            type: cc.ProgressBar   //进度条组件
        },
        //粒子效果
        gameParticle: {
            default: null,
            type: cc.ParticleSystem  //粒子组件
        },

        timeLabel:cc.Label,  //倒计时文本
        scoreLabel:cc.Label, //得分文本
    },

    playerDown:function()
    {
        var action1 = cc.scaleTo(0.09,0.5,0.5);
        this.player.runAction(action1);
    },
    playerUp:function()
    {
        var action1 = cc.scaleTo(0.09,1,1);
        this.player.runAction(action1);
    },

    onLoad: function () 
    {
        //计时器
        this.schedule(this.NewDici,1);
       //设置音效的音量
       cc.audioEngine.setEffectsVolume(0.1);
       cc.audioEngine.playMusic(this.bgAudio,true);
       //给屏幕设置点击事件,控制主角动作
       this.node.on(cc.Node.EventType.TOUCH_START,this.setInputControl.bind(this),this);
       cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN,function(event){
        switch(event.keyCode)
        {
            case 38:
                this.playerMove();
                break;
            case 40:
                this.playerDown();
                cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP,function(event){
                this.playerUp();
                },this);
                break;
            default:
                break;
        }
    },this);

       this.player.setPosition(-100,-296);
       this.timeLabel.string = "倒计时: "+this.playTime;
       cc.director.preloadScene("over");  //预加载场景文件
       this.score=0;
       var time = this.playTime;
       //添加计时器实现场景定时切换
       this.schedule(function()
       {
            var ratio = this.playTime/time;
            this.progressBar.progress = ratio;
            this.playTime--;
            this.timeLabel.string = "last: "+this.playTime;
            // if(this.playTime<=0){
            //     cc.audioEngine.pauseMusic();
            //     cc.director.loadScene('over');
            // }
        },1);       
    },

    //得到新的地刺
    NewDici:function()
    {
        this.diciCount+=1;     
        //克隆指定的任意类型对象 
        var newDici = cc.instantiate(this.dici); 
        //node.addChild(newDici)创建了一个newDici节点
        this.node.addChild(newDici); 
        //范围0到1随机
        var randD = cc.random0To1();
        // newDici.rotationX = (randD >= 0.5) ? 0 : 180;
        //newDici.setPosition()等价于node.setPostion(x,y)需要两个参数
        newDici.setPosition(this.diciPosition(randD));
    },

    //地刺的出现位置
    diciPosition:function(randD)
    {
        var randX=0;
        var randY=0;     
        randX = 600;
        if(randD>0.5)
        {
            randY=-200;
        }
        else
        {
            randY=-260;
        }
        return cc.p(randX,randY);
    },


   //主角跳跃逻辑
    playerMove:function()
    {
        ////移动过程中暂停节点所有侦听事件
       cc.eventManager.pauseTarget (this.node,true);
       //////////////////////
        // var duration = bValue ? this._RIGHT : this._LEFT;
        // var _rotationY = bValue ? 0 : 0;
        // var goOtherSide= cc.moveTo(0.1,cc.p((this.node.width/2-80) * duration,
        //                                      this.player.getPositionY()));
        var goR1= cc.moveBy(0.25,cc.p(0,200));
        var goR2= cc.moveBy(0.25,cc.p(0,-200));
        var sque= cc.sequence(goR1,goR2);
        ///////////////////////////////////////////////////////////////
        //主角动作完成时恢复节点上的触摸侦听事件
        var callFunc = cc.callFunc(function()
        {
            cc.eventManager.resumeTarget(this.node,true);
        },this);

        var sque1=cc.sequence(goR1,goR2,callFunc);
        //cc.log(this.player.position);
        cc.log(this.player.position);
        //限定主角位置
        if(this.player.position.y<-295&&this.player.position.y>-297)
        {
            this.player.runAction(sque1);
        } 

    },

    //设置玩家控制
    setInputControl:function(touch,event)
    {
        cc.audioEngine.playEffect(this.jumpAudio,false);
        var locationInNode = touch.getLocation();
        this.playerMove(true); //怪物移动
        this.score += 1;  //把分数存储到本地
        cc.sys.localStorage.setItem("score",this.score);
        this.scoreLabel.string = "Score: " + this.score;
        //this.NewDici();
    },

});

你可能感兴趣的:(游戏,橡皮怪main-js)