Skip to content

Commit e1f8f87

Browse files
yaustarsteveny-sc
andauthored
Keyboard poll functions tutorial update (#451)
* Updated legacy animation tutorial * Updated anim graph tutorial Co-authored-by: Steven Yau <[email protected]>
1 parent e330923 commit e1f8f87

File tree

2 files changed

+12
-20
lines changed

2 files changed

+12
-20
lines changed

content/en/tutorials/anim-blending.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,12 @@ KeyboardControls.prototype.initialize = function() {
7979
this.app.keyboard.on(pc.EVENT_KEYUP, this.keyUp, this);
8080
};
8181

82-
83-
KeyboardControls.prototype.keyDown = function (e) {
84-
if ((e.key === pc.KEY_P) && (this.entity.anim.baseLayer.activeState !== 'Punch')) {
82+
KeyboardControls.prototype.update = function(dt) {
83+
if (this.app.keyboard.wasPressed(pc.KEY_P) && (this.entity.anim.baseLayer.activeState !== 'Punch')) {
8584
this.entity.anim.setBoolean('punch', true);
8685
}
87-
};
8886

89-
KeyboardControls.prototype.keyUp = function (e) {
90-
if ((e.key === pc.KEY_P) && (this.entity.anim.baseLayer.activeState === 'Punch')) {
87+
if (this.app.keyboard.wasReleased(pc.KEY_P) && (this.entity.anim.baseLayer.activeState === 'Punch')) {
9188
this.entity.anim.setBoolean('punch', false);
9289
}
9390
};

content/en/tutorials/animation-blending.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,16 @@ AnimationBlending.prototype.initialize = function() {
4848
this.blendTime = 0.2;
4949

5050
this.setState('idle');
51+
};
52+
53+
AnimationBlending.prototype.update = function(dt) {
54+
if (this.app.keyboard.wasPressed(pc.KEY_P)) {
55+
this.setState('punch');
56+
}
5157

52-
this.app.keyboard.on(pc.EVENT_KEYDOWN, this.keyDown, this);
53-
this.app.keyboard.on(pc.EVENT_KEYUP, this.keyUp, this);
58+
if (this.app.keyboard.wasReleased(pc.KEY_P)) {
59+
this.setState('idle');
60+
}
5461
};
5562

5663
AnimationBlending.prototype.setState = function (state) {
@@ -61,18 +68,6 @@ AnimationBlending.prototype.setState = function (state) {
6168
// the current animation state to the start of the target animation.
6269
this.entity.animation.play(states[state].animation, this.blendTime);
6370
};
64-
65-
AnimationBlending.prototype.keyDown = function (e) {
66-
if ((e.key === pc.KEY_P) && (this.state !== 'punch')) {
67-
this.setState('punch');
68-
}
69-
};
70-
71-
AnimationBlending.prototype.keyUp = function (e) {
72-
if ((e.key === pc.KEY_P) && (this.state === 'punch')) {
73-
this.setState('idle');
74-
}
75-
};
7671
```
7772

7873
From this point, you are able to add more and more animations to the animation component and start scripting much more complex animation state charts.

0 commit comments

Comments
 (0)