Move around the code portion cocoscreator

cc.Class({
extends: cc.Component,

the Properties: {
// protagonist jump height
jumpHeight: 0,
// protagonist jump duration
jumpDuration: 0,
// maximum speed
maxMoveSpeed: 0,
// acceleration
Accel: 0,
// jump sound resource
jumpAudio: {
default: null,
of the type: cc.AudioClip
},
},

setJumpAction: function () {
// jump-up
var jumpUp = cc.moveBy (this.jumpDuration, cc.v2 (0, this.jumpHeight)) that the easing (cc.easeCubicActionOut ());.
// drop
var jumpDown = cc. moveBy (this.jumpDuration, cc.v2 (0, -this.jumpHeight)) that the easing (cc.easeCubicActionIn ());.
// Add a callback function, other methods for calling at the end we define operation
var callback = cc.callFunc (this.playJumpSound, the this);
// repeat, and call a callback after completion of each floor action to play a sound
return cc.repeatForever (cc.sequence (jumpUp, jumpDown, callback));
},

playJumpSound: function () {
// call sound playback engine sound
cc.audioEngine.playEffect (this.jumpAudio, to 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;
}
},

the onLoad: function () {
// Initialization jump operation
this.jumpAction this.setJumpAction = ();
this.node.runAction (this.jumpAction);

// direction acceleration switch
this.accLeft = to false;
this.accRight = to false;
// current lead horizontal velocity
this.xSpeed = 0;

// Initialize keyboard input monitoring
cc.systemEvent.on (cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, the this);
cc.systemEvent.on (cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, the 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) {
// The current direction of the acceleration rate is updated every frame
IF (this.accLeft) {
this.xSpeed - = this.accel * dt;
} the else IF (this.accRight) {
this.xSpeed + = the this * dt .ACCEL;
}
// limits not exceed the maximum rate of lead
IF (the Math.abs (this.xSpeed)> this.maxMoveSpeed) {
// IF REACH speed limit, with use max speed Current direction
this.xSpeed = the this * this.xSpeed .maxMoveSpeed / the Math.abs (this.xSpeed);
}

// update the current position and speed of the lead
this.node.x this.xSpeed * dt + =;
},
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
37 [
38 is
39
40
41 is
42 is
43 is
44 is
45
46 is
47
48
49
50
51 is
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
});
--------------------- 

Guess you like

Origin www.cnblogs.com/hyhy904/p/10977604.html