3|cocos

fly.js

// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
      
        maxMoveSpeed:400,
        accel:200,
           //玩家移动速度
        MoveSpeed:100
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
  //加速度开关
  this.accleft=false;
  this.accright=false;
  this.accup=false;
  this.accdown=false;
  //水平方向速度
  this.xspeed=3;
  //竖直方向速度
  this.yspeed=2;
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN,this.onKeyDown,this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP,this.onKeyUp,this);
             
    },

    start () {
              
    },
    onKeyDown(event){
        switch(event.keyCode){
        case cc.macro.KEY.a:this.accleft=true;
        case cc.macro.KEY.d:this.accright=true;
        case cc.macro.KEY.w:this.accup=true;
        case cc.macro.KEY.s:this.accdown=true;
        }
    },
    onKeyUp(event){
        switch(event.keyCode){
            case cc.macro.KEY.a:this.accleft=false;this.xspeed=3;
            case cc.macro.KEY.d:this.accright=false;this.xspeed=3;
            case cc.macro.KEY.w:this.accup=false;this.yspeed=0;
            case cc.macro.KEY.s:this.accdown=false;this.yspeed=0;
            }
    },
 
    update (dt) {//v=at
     if(this.accleft){
         this.xspeed-=this.accel*dt;
     }else if(this.accright){
         this.xspeed+=this.accel*dt;
     }else if(this.accup){
        this.yspeed+=this.accel*dt;
    }else if(this.accdown){
        this.yspeed-=this.accel*dt;
    }

    

    if(Math.abs(this.xspeed)>this.maxMoveSpeed)
     this.xspeed=this.maxMoveSpeed*(this.xspeed/Math.abs(this.xspeed));
 
     this.node.x+=this.xspeed*dt;

     if(Math.abs(this.yspeed)>this.maxMoveSpeed)
     this.yspeed=this.maxMoveSpeed*(this.yspeed/Math.abs(this.yspeed));
  

     this.node.y+=this.yspeed*dt;

     
    },
});

game.js

// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
           
        // 背景移动速度
        bgMoveSpeed:0,
        // 背景
        bg:{
            default:null,
            type:cc.Node
        },
        bg2:{
            default:null,
            type:cc.Node
        },

        // 子弹预制体资源
        bulletPrefab:{
            default:null,
            type:cc.Prefab
        },
        // 玩家节点
        player:{
            default:null,
            type:cc.Node
        },


        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    // 加载脚本时自动调用
    onLoad () {
        // 子弹发射计时器
        this.bulletTimer = 0;
        // 子弹发射频率
        this.shootDuration = 0.5;
    },

    start () {

    },

    // 发射子弹
    shoot(dt){

        // 累计时间
        this.bulletTimer += dt;
        // 是否达到发射时间
        if(this.bulletTimer >= this.shootDuration)
        {
            // 计时器归零
            this.bulletTimer = 0;
            // 实例化一个预制体对象
            var bullet = cc.instantiate(this.bulletPrefab);
            // 添加到画布上
            this.node.addChild(bullet);
            // 获取英雄的位置设置给子弹
            var pos = cc.v2(this.player.x + this.player.width/2,this.player.y);
            bullet.setPosition(pos);
        }
    },

    // 帧调度器。dt:两帧之间的时间间隔  speed  s=vt
    update (dt) {
        this.bg.x = this.bg.x - this.bgMoveSpeed*dt;
        this.bg2.x = this.bg2.x - this.bgMoveSpeed*dt;

        if(this.bg.x <= -this.bg.width)
        {
            this.bg.x = this.bg2.x + this.bg2.width;
        }
        if(this.bg2.x <= -this.bg2.width)
        {
            this.bg2.x = this.bg.x + this.bg.width;
        }

        // 发射子弹
        this.shoot(dt);
    },
});

bullet.js

// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {

        // 子弹移动速度
        moveSpeed:0

        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

    },

    update (dt) {
        // 每一帧移动的距离
        var s = this.moveSpeed * dt;
        // 修改子弹的位置
        this.node.x = this.node.x + s;
    },
});

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值