gpt4 book ai didi

JavaScript/CSS 播放一次 Spritesheet 就变成 gif 了吗?

转载 作者:太空宇宙 更新时间:2023-11-04 12:17:34 27 4
gpt4 key购买 nike

所以我正在进行这个游戏。我要有,闲着玩this giffunction()被调用,我需要它遍历 this spritesheet ONCE 然后恢复到 gif .这是我的代码和 JavaScript,我不知道该怎么做,但我还是试过了。

CSS

.dragon  {
width: 120px;
height: 120px;
background: url('http://www.thegaminghideout.com/school/dragonFire.png') left center;
animation: play .8s steps(10) infinite;
}
@keyframes play {
100% {background-position: -1200px;}
}

JavaScript

  var weapon = [];
var weapons = ['Claymore', 'Dagger', 'Magic Staff', 'Sword', 'Bow', 'Crossbow'];
var armors = ['Helmet', 'Hood', 'Chestplate', 'Tunic', 'Robe', 'Legplates', 'Leggings', 'Undergarments', 'Boots', 'Armored Boots'];
var materials = ['Leather', 'Iron', 'Steel', 'Mythril', 'Dragonbone'];
var battleMusic = function() {
if(mute.checked == false) {
document.getElementsByTagName("AUDIO")[0].play();
}
if(mute.checked === true) {

}
}
var dragonHit = function() {
var damage = dragonad / dp * 100;
hp = hp - damage;
Math.round(hp);
alert("You were hit by the dragon! You currently have " + hp + " health!");
}
var hitDragon = function() {
var damage = ad / dragondp * 100;
dragonhp = dragonhp - damage;
Math.round(dragonhp);
alert("You hit the dragon! The dragon now has " + dragonhp + " health!");
if(weapon.hasOwnProperty("Magic Staff")) {
img.class = "dragon";
hitDragon();
}
}

HTML

<html>
<body>
<audio>
<source src="http://www.thegaminghideout.com/sound/Pokemon.mp3" type="audio/mpeg">
Your browser doesn't support the sound file for playback. The Dragon Battle will be silent!
</audio>
<div id="wrap">
<div class="box">
<div class="container">
<input type="checkbox" value="mute" id="mute">Mute</input>
<h2 class="header">Dragon Slayer - REBORN!</h2>
<p class="intro">You are a dragon-slayer veteran! You are retired, relaxed, and comfortable in your home, with no-one to boss you around... then you hear the town sirens.</p>
<a id="button" href="javascript:fight()"><br>BEGIN!</a>
<img id="scenario" class="" src="http://www.thegaminghideout.com/school/stage1.png">
<div class="battles">
</div>
</div>
</div>
</div>
</body>
</html>

应该发生什么

img从空白类(class)开始。默认情况下,在龙战之前,它会保持原样。一旦dragonFight()函数(包含在完整游戏的 jcodepen 中)被调用时,它会将图像的 src 更改为 dragon.gif .如果玩家(数组)拥有魔法杖的武器(属性),它应该播放 dragonFire.png当他们调用 ATTACK 时动画 ONCE功能,然后恢复到 dragon.gif .

带完整游戏的 CodePen

CodePen

最佳答案

这是我的做法: http://jsfiddle.net/gkj1ef8d/

我使用应用于特定类的关键帧动画来为设置为背景图像的 spritesheet 设置动画。使用这种方法,您可以拥有任意数量的动画。我建议您将它们全部放入一个大的 png spritesheet 中,其中包含您需要的所有动画,然后调整图像的水平和垂直位置。这样一来,您将只有一张图片需要预加载,从而缩短加载时间并简化代码。

var button = document.getElementsByTagName("button")[0];
var dragon = document.querySelectorAll(".dragon")[0];

button.onclick = function(e) {
e.preventDefault();
dragon.className+=" run";

window.st = window.setTimeout(function(p) {
dragon.className = dragon.className.replace(" run", "");
}, 1000)
}
@-webkit-keyframes run {
from {
background-position: top left;
}
to {
background-position: top right;
}
}

.dragon {
width: 120px;
height: 120px;
background: red;
}

img {
position: absolute;
top: -9999999px
}
.dragon.run {
background: url("http://www.thegaminghideout.com/school/dragonFire.png") top left no-repeat;
-webkit-animation: run 1s steps(9) infinite;
}
<img src="http://www.thegaminghideout.com/school/dragonFire.png" alt=""><!-- The image is in the html to force it to preload, a better approach would be to preload it with JS, but this is out of the scope of the question -->
<div class="dragon"></div>

<button>Run</button>

请注意,此示例没有所有 vendor 前缀,因此它只能在 Chrome 和 Safari 上运行。

关于JavaScript/CSS 播放一次 Spritesheet 就变成 gif 了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28584942/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com