You are probably adding the event listener to the html element containing the player
Not really, I add it to document, even tried on window, still it only catches after player is clicked
here is the code
<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");
},
});
document.addEventListener("keydown", function (event) {
if (!player) return; // Ensure player is loaded
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>