ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

3|cocos

2020-12-04 21:01:49  阅读:232  来源: 互联网

标签:cocos creator cc docs manual default dt


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:

    onl oad () {
  //加速度开关
  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:

    // 加载脚本时自动调用
    onl oad () {
        // 子弹发射计时器
        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:

    // onl oad () {},

    start () {

    },

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

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

标签:cocos,creator,cc,docs,manual,default,dt
来源: https://blog.csdn.net/weixin_43846020/article/details/110672743

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有