我不知道如何解决这个问题:当我按住左键+空格键时,动画可以工作,但是当我按住左键(不离开它)然后按空格键(一次,一直按住左键)时,动画会跳转,但不会向左移动...即使我按下其他键,动画也会停止(要重置它,我必须全部离开)。有人可以解释我为什么会这样并给我一些实用的解决方案吗?非常感谢你!这是代码:
<style>
#map{
position:relative;
height:220px;
width:800px;
}
#character{
height:100px;
width:100px;
position:absolute;
bottom: 10px;
left: 0px;
z-index:1;
}
</style>
<div id="map"><div id="character"><img src="images/bart.gif"></div></div>
<script>
enableJump = true;
jump = function(){
if(enableJump){ enableJump = false;
salta = function(){
if(flagY){
posY++;
if(posY >= 90) flagY = false;
jj = document.getElementById('character').style.bottom=posY;
}
else{
posY--;
jj = document.getElementById('character').style.bottom=posY;
if(posY < 10){
flagY = true;
clearInterval(j);
enableJump = true;
up = false;
}
}
}
j = setInterval(salta,8);
}
}
up = false; dx = false;
document.onkeydown = function(evt){
code = evt.keyCode;
if(code == 32) up=true;
if(code == 39) dx=true;
moveCharacter();
}
document.onkeyup = function(evt){
code = evt.keyCode;
if(code == 32) up=false;
if(code == 39) dx=false;
}
posX = 10; posY = 10; flagY = true; var j;
moveCharacter = function(){
if(dx == true && up == true){
jump();
posX+=10;
x1 = document.getElementById('character').style.backgroundImage='url(images/character_left.gif)';
temp = document.getElementById('character').style.left=posX;
}
else if(dx){
posX+=10;
x1 = document.getElementById('character').style.backgroundImage='url(images/character_left.gif)';
temp = document.getElementById('character').style.left=posX;
}
else if(up){ jump(); }
}
</script>
代码已清理(没有跳转但有同样的问题,按住左键并按下另一个键它会停止,我必须让它重置它)
<script>
var dx = false;
var posX = 10;
var posY = 10;
document.onkeydown = function(evt){
if(evt.keyCode == 39) dx=true;
moveCharacter();
}
document.onkeyup = function(evt){
if(evt.keyCode == 39) dx=false;
}
moveCharacter = function(){
if(dx){
posX+=10;
document.getElementById('character').style.left=posX;
}
}
</script>