some problem use html5 to make game

time shaft

calculate fram count every seconds

var fps = 60,
	interval = 1000 / fps,
	now = Date.now(),
	lastSecond = Math.floor(now / 1000),
	frameCount = Math.ceil((now % 1000) / fps) ;
window.setInterval(function(){
	var now = Date.now(),
		second = Math.floor(now / 1000),
		milliSecond = now % 1000;
	if(second != lastSecond){
		frameCount = 0;
		lastSecond = second;
	}
	if(frameCount < fps && milliSecond >= frameCount * interval){
		frameCount ++;
		//do render
	}
},1);


 

keyboard press status

var specialKeys = {},
	keyboardHandler = function (e){
		var isDown = e.type === 'keyDown';
		e.stopPropagation();
		e.preventDefault();
		if(!specialKeys[e.keyCode]){
			keyStates[e.keyCode] = isDown;
		}
	},
	blurHandler = function (){
		keyStates = {};
	};
specialKeys[16] = specialKeys[17] = specialkeys[18] = true;
canvas.addEventListener('keydown',keyboardHandler,false);
canvas.addEventListener('keyup',keyboardHandler,false);
canvas.addEventListener('blur',blurHandler,false);


mouse event

cache every mouse event , deal cache every render . and clear dealed event in the cache.

var MAX = 30,head=0,tail=0,
	history = [],
	mousemoveHandler = function (e){
		var last = history[head];
		if(e.screenX == last.screenX && e.screenY == last.screenY){
			//ignore duplication
			return;
		}
		
		head = (head + 1) % MAX;
		history[head] = {
			//store attr in event..	
		};
		if(tail == head){
			tail = (tail + 1) % MAX;
		}
	},
	traverseHistory = function (cb){
		var len = (head - tail + MAX) % MAX ,i;
		for(i = 0,i<len;i++){
			cb(history[(i + tail) % MAX]);
		}
	};
canvas.addEventListener('mousemove',mousemoveHandler,false);


touch device support

same to up

var MAX = 30,head = 0,tail = 0,
	history = [],
	touchHandler = function (){
		var last = history[head],
			touch = e.touches[0];
		e.preventDefault();
		if(touch.screenX == last.screenX && touch.screenY == last.screenY){
			//ignore duplication
			return;
		}
		head = (head + 1) % MAX;
		history[head] = {
				//store attr in event..
		};
		if(tail == head){
			tail = (tail + 1) % MAX;
		}
	};
canvas.addEventListener('touchstart',touchHandler,false);
canvas.addEventListener('touchmove',touchHandler,false);


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值