我很难找到一个包含 92 个图像的 PNG 文件进行测试,所以我不得不使用一个较短的文件。这是一个工作示例:http: //jsfiddle.net/Yhrbb/
示例中的代码是这样的:
(function() {
var total = 92;
var play_on_click = 72;
var played = 0;
$('#fly').click(function() {
if (played >= total) {
return;
}
var current_play;
if (play_on_click > (total - played)) {
current_play = total - played;
}
else {
current_play = play_on_click;
}
played += current_play;
$('#bird').sprite({fps: 12, no_of_frames: 3, play_frames: current_play});
});
})();
我们可以将其调整为适合您,如下所示:
(function() {
var total = 92;
var play_on_click = 72;
var played = 0;
$('#stacheguy2').click(function() {
if (played >= total) {
return;
}
var current_play;
if (play_on_click > (total - played)) {
current_play = total - played;
}
else {
current_play = play_on_click;
}
played += current_play;
$(this).sprite({fps: 30, no_of_frames: 92, play_frames: current_play});
});
})();
请注意,一旦我们达到总帧数,我们只需使用return
on忽略额外的点击if (played >= total)
。你可以在那个时候重置played
或做任何你想做的事情。如果这不起作用,您是否介意发布您的 PNG 文件或类似长的文件以在 jsfiddle 中使用。
使用数组配置帧数
如果您想在每次单击时播放配置数量的帧,您可以这样做:http: //jsfiddle.net/dNYks/
(function() {
var play_on_click = [32, 21, 10, 20];
var play_index = 0;
$('#fly').click(function() {
var current_play = play_on_click[play_index];
play_index++;
if (play_index > play_on_click.length-1) {
play_index = 0;
}
$('#bird').sprite({fps: 12, no_of_frames: 3, play_frames: current_play});
});
})();
我们可以调整此代码以匹配您的标记,如下所示:
(function() {
var play_on_click = [32, 21, 10, 20];
var play_index = 0;
$('#stacheguy2').click(function() {
var current_play = play_on_click[play_index];
play_index++;
if (play_index > play_on_click.length-1) {
play_index = 0;
}
$(this).sprite(fps: 30, no_of_frames: 92, play_frames: current_play});
});
})();
</p>
</p>