zoukankan      html  css  js  c++  java
  • 部分cocoscreator左右移动代码

    cc.Class({
    extends: cc.Component,

    properties: {
    // 主角跳跃高度
    jumpHeight: 0,
    // 主角跳跃持续时间
    jumpDuration: 0,
    // 最大移动速度
    maxMoveSpeed: 0,
    // 加速度
    accel: 0,
    // 跳跃音效资源
    jumpAudio: {
    default: null,
    type: cc.AudioClip
    },
    },

    setJumpAction: function () {
    // 跳跃上升
    var jumpUp = cc.moveBy(this.jumpDuration, cc.v2(0, this.jumpHeight)).easing(cc.easeCubicActionOut());
    // 下落
    var jumpDown = cc.moveBy(this.jumpDuration, cc.v2(0, -this.jumpHeight)).easing(cc.easeCubicActionIn());
    // 添加一个回调函数,用于在动作结束时调用我们定义的其他方法
    var callback = cc.callFunc(this.playJumpSound, this);
    // 不断重复,而且每次完成落地动作后调用回调来播放声音
    return cc.repeatForever(cc.sequence(jumpUp, jumpDown, callback));
    },

    playJumpSound: function () {
    // 调用声音引擎播放声音
    cc.audioEngine.playEffect(this.jumpAudio, false);
    },

    onKeyDown (event) {
    // set a flag when key pressed
    switch(event.keyCode) {
    case cc.macro.KEY.a:
    this.accLeft = true;
    break;
    case cc.macro.KEY.d:
    this.accRight = true;
    break;
    }
    },

    onKeyUp (event) {
    // unset a flag when key released
    switch(event.keyCode) {
    case cc.macro.KEY.a:
    this.accLeft = false;
    break;
    case cc.macro.KEY.d:
    this.accRight = false;
    break;
    }
    },

    onLoad: function() {
    // 初始化跳跃动作
    this.jumpAction = this.setJumpAction();
    this.node.runAction(this.jumpAction);

    // 加速度方向开关
    this.accLeft = false;
    this.accRight = false;
    // 主角当前水平方向速度
    this.xSpeed = 0;

    // 初始化键盘输入监听
    cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
    cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    },

    onDestroy () {
    // 取消键盘输入监听
    cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
    cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    },

    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;
    },
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    });
    --------------------- 

  • 相关阅读:
    二进制插入 牛客网 程序员面试金典 C++ Python java
    二进制小数 牛客网 程序员面试金典 C++ Python
    二叉树中和为某一值的路径 牛客网 程序员面试金典 C++ Python
    Python matplotlib pylab 画张图
    Python matplotlib pylot和pylab的区别
    Ubuntu 16.04 curl 安装 使用
    Ubuntu 16.04 菜单栏 换位置 挪到左边 挪到下边
    Python 模块feedparser安装使用
    Ubuntu 16.04 下 旋转显示器屏幕 竖屏显示
    从 callback 到 promise
  • 原文地址:https://www.cnblogs.com/hyhy904/p/11001417.html
Copyright © 2011-2022 走看看