部分cocoscreator左右移动代码

摘要:
Cc.Class({extend:Cc.Component,属性:{//主角jumpHeight的跳跃高度:0,//主角jump的跳跃持续时间:0,//maxMoveSpeed:0,//Acceleration:0,//跳跃音频资源jumpAudio:{default:null,type:Cc.AudioClip},},setJumpAction:function

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
});
--------------------- 

免责声明:文章转载自《部分cocoscreator左右移动代码》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇windows下redis报错redis-server.exe已停止工作,redis的rdb持久化异常EXCEPTION_ACCESS_VIOLATIONThreadLocal工具类 隔离思想下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C++ STL hash表用法

C++ STL unordered_map用法 在C++11中,unordered_map作为一种关联容器,替代了hash_map,unordered_map的底层实现是hash表,所以被称为无序关联容器。不管是map还是unordered_map都是一种 key-map(value) 映射的容器,提供非常高的查找效率,下面我们来了解unordered_m...

ro.boot.bootreason property设置(androidboot.xxxx bootargs)

ro.boot.bootreason property设置(androidboot.xxxx bootargs) system/core/init.cpp static void process_kernel_cmdline() { // The first pass does the common stuff, and finds if we a...

缓存击穿 解决方案

本文代码逻辑思想来自阿里的JetCache框架,这里只是自己的学习与理解,记录下;具体实现可以去查看JetCache源码:git地址:https://github.com/alibaba/jetcache实际应用中可以接JetCache框架,使用@CachePenetrationProtect注解即可实现 当缓存访问未命中的情况下,对并发进行的加载行为进行...

缓存三大问题的解决办法

1.缓存穿透  在大多数互联网应用中,缓存的使用方式如下图所示:       当业务系统发起某一个请求时:     首先判断缓存中是否有该数据。     如果缓存中存在,则直接返回数据。     如果缓存中不存在,则再查询数据库,然后返回数据。   了解了上述过程后,下面说说缓存穿透。   1.1 缓存穿透的危害   如果存在海量请求查询根本就不存在的数据...

Django前端获取后端数据之前端自定义函数

在写网站的时候遇到了一个问题: Django在后端向前端传数据时,多数会使用dict字典来传送多个数据,但前端只能遍历,没有一个用key取到value值的方法可以直接使用 如果作为一个list传递到前端,结构相同情况下,遍历确实够用。但是使用dict时多数会使用单个key取value放到不同的地方使用,Django提供的方法就不够用了。 所以这篇文章介绍一...

Linux下常用redis指令

set get 取值赋值,key值区分大小写 mset mget多个的取值和赋值 del 删除一个或多个key 查看key值是否存在,有就返回1,没有就是0 设置一个有存活时间的key值,设置成功返回1 显示key值剩余存活时间 keys显示符合指定条件的key 创建相关数据类型,并使用keys显示对应的数据类型 创建hash类型的数据...