________________________________________________
var AnimationBlending = pc.createScript('animationBlending');
AnimationBlending.states = {
idle: {
animation: 'Idle.json'
},
Jump: {
animation: 'Jump.json'
},
Left: {
animation: 'Left.json'
},
Right: {
animation: 'Right.json'
},
Back: {
animation: 'Back.json'
},
Walk: {
animation: 'Walk.json'
}
};
// initialize code called once per entity
AnimationBlending.prototype.initialize = function() {
this.blendTime = 0.2;
this.setState('idle');
this.app.keyboard.on(pc.EVENT_KEYDOWN, this.keyDown, this);
this.app.keyboard.on(pc.EVENT_KEYUP, this.keyUp, this);
};
AnimationBlending.prototype.setState = function (state) {
var states = AnimationBlending.states;
this.state = state;
// Set the current animation, taking 0.2 seconds to blend from
// the current animation state to the start of the target animation.
this.entity.animation.play(states[state].animation, this.blendTime);
};
AnimationBlending.prototype.keyDown = function (e) {
if ((e.key === pc.KEY_W) && (this.state !== 'Walk')) {
this.setState('Walk');
}
// e.g. jumping works with space key
if ((e.key === pc.KEY_SPACE) && (this.state !== 'Jump')) {
this.setState('Jump');
}
if ((e.key === pc.KEY_S) && (this.state !== 'Back')) {
this.setState('Back');
}
if ((e.key === pc.KEY_D) && (this.state !== 'Right')) {
this.setState('Right');
}
if ((e.key === pc.KEY_A) && (this.state !== 'Left')) {
this.setState('Left');
}
};
AnimationBlending.prototype.keyUp = function (e) {
if ((e.key === pc.KEY_W) && (this.state === 'Walk')) {
this.setState('idle');
}
if ((e.key === pc.KEY_S) && (this.state === 'Back')) {
this.setState('idle');
}
if ((e.key === pc.KEY_SACE) && (this.state === 'Jump')) {
this.setState('idle');
}
if ((e.key === pc.KEY_D) && (this.state === 'Right')) {
this.setState('idle');
}
if ((e.key === pc.KEY_A) && (this.state === 'Left')) {
this.setState('idle');
}
};
Try to set the animation loop on false.
if (this.state === 'Jump') {
this.entity.animation.loop = false;
}
else {
this.entity.animation.loop = true;
}
2 Likes