利用JavaScript和setInterval完成一个简单的汤姆猫游戏,童年回忆-汤姆猫js小游戏代码
var timer;
window.onload = function() {
document.getElementById("cymbal").onclick = function() {
startAnimation("cymbal", 13);
}
document.getElementById("drink").onclick = function(){
startAnimation("drink",80);
}
document.getElementById("eat").onclick = function(){
startAnimation("eat",39);
}
document.getElementById("fart").onclick = function(){
startAnimation("fart",27);
}
document.getElementById("pie").onclick = function(){
startAnimation("pie",23);
}
document.getElementById("scratch").onclick = function(){
startAnimation("scratch",55);
}
}
function startAnimation(name, count) {
clearInterval(timer);
var index = 0;
var img = document.getElementById("cat");
timer = setInterval(function() {
if(++index < count) {
img.src = getImageName(name, index);
} else {
clearInterval(timer);
}
}, 80)
}
function getImageName(name, index) {
return "img/Animations/" + name + "/" + name + "_" + getIndex(index) + ".jpg";
}
function getIndex(index) {
if(index < 10) {
return "0" + index;
} else {
return index;
}
}
系统已开启自动识别垃圾评论机制,识别到的自动封号,下载出错或者资源有问题请联系全栈客服QQ 1915635791