日期:2014-05-16 浏览次数:20529 次
$(function() { $("body").append('<div class="map"></div>'); InitTank('me', {x:(long-50)/2, y:high-50}, 'up'); InitEnemy(); //键盘点击事件 $(document).keydown(function(evt) { evt = evt || window.event; var key = evt.which || evt.keyCode; if(key==32) { SendBullet("me"); } switch (key) { case 37: direction = "left"; break; case 38: direction = "up"; break; case 39: direction = "right"; break; case 40: direction = "down"; break; } if (key >= 37 && key <= 40) { ChangeDirection('me', direction); isMeMove = true; } }); $(document).keyup(function(evt) { evt = evt || window.event; var key = evt.which || evt.keyCode; if (key >= 37 && key <= 40) { isMeMove = false; } }); MyInterval=setInterval("Refresh()",timeSpan); }); var isMeMove = false; var moveLong = 10; var bulletmoveLong = 20; var timeSpan = 300; var long = 600; var high = 600; function Refresh() { var me = $("#me"); var mtop = $(me).position().top; var mleft = $(me).position().left; if (isMeMove) { direction = $(me).attr("direction"); var offset = GetOffset(direction); mtop += offset.y*moveLong; mleft += offset.x*moveLong; if(mtop<0){ mtop = 0; }else if(mtop>long-$(me).height()) { mtop = long-$(me).height(); } if(mleft<0){ mleft = 0; }else if(mleft>long-$(me).width()) { mleft = long-$(me).width(); } $(me).css({'top':(mtop + 'px'),'left':(mleft + 'px')}); } $(".tank:visible[enemy='enemy']").each(function(){ var etop = $(this).position().top; var eleft = $(this).position().left; var bullettime = parseInt($(this).attr("bullettime")); if(bullettime<=0) { bullettime = 10; var topSpan = etop-mtop; var leftSpan = eleft-mleft; ex = Math.abs(leftSpan)>Math.abs(topSpan)?leftSpan/Math.abs(leftSpan)*-1:0; ey = Math.abs(leftSpan)>Math.abs(topSpan)?0:topSpan/Math.abs(topSpan)*-1; etop = etop + ey*moveLong; eleft = eleft + ex*moveLong; var direction = GetDirection({x:ex,y:ey}); ChangeDirection($(this).attr("id"),direction); SendBullet($(this).attr("id")); }else { direction = $(this).attr("direction"); var offset = GetOffset(direction); etop = etop + offset.y*moveLong; eleft = eleft + offset.x*moveLong; bullettime--; }