Cocos official website example

Take a look at the official website and the documentation. When I saw cocos-js, I thought I might be able to copy an example to play with, which is quite interesting. Although the bottom layer does not know how to do it, at best it feels like writing a configuration file.

http://docs.cocos.com/creator/manual/zh/getting-started/quick-start.html

Complete the first game according to the example given on the official website

First download cocos creater and install it

The initial files given by the download tutorial are the music and image components. Create a new canvas and drag images such as background onto it

http://docs.cocos.com/creator/manual/zh/getting-started/quick-start.html

Complete the first game according to the example given on the official website

 

 

The initial files given by the download tutorial are the music and image components. Create a new canvas and drag images such as background onto it

 

Add components to pictures, usually js scripts. The attributes of js cannot be imported as plugins, otherwise you will not be able to choose to add user script components

play.js in the config file

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

cc.Class({
    extends: cc.Component,

    properties: {
       //主角跳跃高度
       jumpHeight: 0,
        // 主角跳跃持续时间
        jumpDuration: 0,
        // 最大移动速度
        maxMoveSpeed: 0,
        // 加速度
        accel: 0,
    },

    // LIFE-CYCLE CALLBACKS:
    //初始事件
    onLoad () {
        //初始化跳跃动作
        this.jumpAction = this.setJumpAction();
        this.node.runAction(this.jumpAction);
        //加速度方向开关
        this.accLeft = false;
        this.accRight = false;
        //主角当前水平方向速度
        this.xSpeed = 0;
        //初始化键盘输入监听
        this.setInputControl();

    },
    //跳跃事件 详情看cocos2d-js
    setJumpAction: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));
    },
    //重写输入控制事件
      setInputControl: function () {
        var self = this;
        // 添加键盘事件监听
        // 有按键按下时,判断是否是我们指定的方向控制键,并设置向对应方向加速
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, function (event){
            switch(event.keyCode) {
                case cc.KEY.a:
                    self.accLeft = true;
                    break;
                case cc.KEY.d:
                    self.accRight = true;
                    break;
            }
        });

        // 松开按键时,停止向该方向的加速
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, function (event){
            switch(event.keyCode) {
                case cc.KEY.a:
                    self.accLeft = false;
                    break;
                case cc.KEY.d:
                    self.accRight = false;
                    break;
            }
        });
    },
    //start () {},

     update: function (dt) {
        // 根据当前加速度方向每帧更新速度
        if (this.accLeft) {
            this.xSpeed -= this.accel * dt;
        } else if (this.accRight) {
            this.xSpeed += this.accel * dt;
        }
        // 限制主角的速度不能超过最大值
        if ( Math.abs(this.xSpeed) > this.maxMoveSpeed ) {
            // if speed reach limit, use max speed with current direction
            this.xSpeed = this.maxMoveSpeed * this.xSpeed / Math.abs(this.xSpeed);
        }

        // 根据当前速度更新主角的位置
        this.node.x += this.xSpeed * dt;
    },
});

Results map:

Summary: The component is the property function. After creating the object and property name, add it as a component of the game element to further set its properties

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324533778&siteId=291194637