利用计时函数做汤姆猫小游戏

来源:https://www.sucaihuo.com/js/2694.html 2017-08-11 06:19浏览(1155) 收藏

利用JavaScript和setInterval完成一个简单的汤姆猫游戏,童年回忆-汤姆猫js小游戏代码
利用计时函数做汤姆猫小游戏
分类:游戏 > 益智 难易:入门级
查看演示 下载资源 下载积分: 20 积分
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;
	}
}
标签: 游戏
评论0
头像

系统已开启自动识别垃圾评论机制,识别到的自动封号,下载出错或者资源有问题请联系全栈客服QQ 1915635791

1 2