if (!player) return;
This isn't doing anything. You set the player before even adding the listener, so it's always set.
As Davide said, you aren't setting an animation in success
, so the player isn't playing. Call play()
. Also don't indent your code inside a script like a psychopath.
<script>
var player = new spine.SpinePlayer("player-container", {
jsonUrl: "gifts.json",
atlasUrl: "gifts.atlas",
showControls: true,
skin: "bluff",
backgroundColor: "000000",
defaultMix: 0,
premultipliedAlpha: true,
success: function (player) {
player.setViewport("base");
player.play();
},
});
document.addEventListener("keydown", function (event) {
let currentAnimation = player.animationState.getCurrent(0)?.animation?.name;
if (currentAnimation !== "base") {
player.animationState.setAnimation(0, "base", false);
} else {
let randomIndex = Math.floor(Math.random() * 6) + 1;
player.animationState.setAnimation(randomIndex, "meta_" + randomIndex, false);
}
});
</script>