切换箭头键时图像停止一秒钟

时间:2015-11-02 22:10:20

标签: javascript css html5

我有另一个问题。当我向左或向右移动主要播放器图像时,它移动得很好,除非你向右移动然后你快点并在右键仍然向下时按左键,图像停止一秒然后它决定向左移动。反之亦然。

Main.js

var frames = story.textContainers; // Where `story` is your story
for (var i=0; i<frames.length; i++) {
   var frame = frames[i];

   if (!frame.characters.length) continue; // Skip text frames without characters

   var para = frame.paragraphs[0];

   if (para.parentTextFrames.length > 1) {
      para = frame.paragraphs[1];
   }

   // Do work with `para` object
}

移动土地和玩家

var getPlatF1POSX = 0;
var getPlatF1POSY = 0;
var addVar = 0;
var addVar2 = 0;
var addVar3 = 0;
function loadGame() {
    document.getElementsByTagName("DIV")[4].style.visibility = "visible";
    addEventListener('mousemove', getData, false);
    addEventListener('keydown', movePlayer, false);
    addEventListener('keyup', stopPlayer, false);
    movePlat();
    moveP();

    document.getElementById("player").style.left = xPos + "px";
    document.getElementById("player").style.top = yPos + "px";  
}

function getData(gData) {

}

var thisThis = 1;
var moveBlock1 = 350;
var stLandT = 0;
var gPos = "";
var rightPos = false;
var leftPos = false;
function movePlat() {

}

function movePlayer(mPlayer) {
    switch (mPlayer.keyCode) {
        case 39: // RIGHT
            if (stLandT == 0 && gPos == "" && rightPos == false) {
                setThis = setTimeout(landT, 500);
                thisSet = setTimeout(moveLand, 30);
                stLandT = 1;
            }
            gPos = "RIGHT";
            rightPos = true;
            leftPos = false;
        break;

        case 37: // LEFT
            if (stLandT == 0 && gPos == "" && leftPos == false) {
                setThis = setTimeout(landT, 500);
                thisSet = setTimeout(moveLand, 30);
                stLandT = 1;
            }
            gPos = "LEFT";
            rightPos = false;
            leftPos = true;
        break;

        case 38: // UP

        break;

        case 40: // DOWN

        break;
    }
}

function stopPlayer(sPlayer) {
    switch (sPlayer.keyCode) {
        case 39:
            clearTimeout(setThis);
            clearTimeout(thisSet);
            stLandT = 0;
            gPos = "";
            rightPos = false;
            leftPos = false;
        break;
        case 37:
            clearTimeout(setThis);
            clearTimeout(thisSet);
            stLandT = 0;
            gPos = "";
            rightPos = false;
            leftPos = false;
        break;
    }
}

2 个答案:

答案 0 :(得分:0)

这是因为无论什么键出现,都会停止播放器。您应该存储上次按下的键,而不是键,您需要检查是否释放了最后一个键。

我很难调试你的代码,所以我在jQuery中创建它(并且遇到了与你相同的麻烦):

var game = {
    settings: {
    	moveSpeed: 5, // 5 milliseconds, 200fps  
        moveBy: 2 // 2 pixels
    },
    land: null,
    landWidth: null,
    landLeft: null,
    viewport: null,
    viewportWidth: null,
    landMinLeft: null,
    init: function() {
        game.land = $('.land');
        game.landWidth = game.land.width();
        game.landLeft = game.land.position().left;
        game.viewport = $('.viewport');
        game.viewportWidth = game.viewport.width();
        game.landMinLeft = -(game.landWidth-game.viewportWidth);
    },
    movingInterval: null,
    lastKey: null,
    keyDown: function (e) {
		switch (e.keyCode) {
        	case 39: // RIGHT
                game.lastKey = e.keyCode;
                clearInterval( game.movingInterval );
                game.movingInterval = setInterval( function() {
                    game.move('right');
                }, game.settings.moveSpeed);
                break;
            case 37: // LEFT
                game.lastKey = e.keyCode;
                clearInterval( game.movingInterval );
                game.movingInterval = setInterval( function() {
                    game.move('left');
                }, game.settings.moveSpeed);
                break;
        }
    },
    keyUp: function(e) {
        if( e.keyCode==game.lastKey ) {
        	game.stopMoving();
        };
    },
    move: function( direction ) {
    	switch( direction ) {
            case 'left':
                var newLeft = game.land.position().left+game.settings.moveBy;
                if( newLeft>0 ) newLeft=0;
                game.land.css({
                   'left': newLeft+'px' 
                });
                break;
            case 'right':
                var newLeft = game.land.position().left-game.settings.moveBy;
                if( newLeft<game.landMinLeft ) newLeft=game.landMinLeft;
                game.land.css({
                   'left': newLeft+'px' 
                });
                break;
        };
    },
    stopMoving: function() {
    	clearInterval( game.movingInterval );
    }
};
game.init();
$(window).on('keydown', game.keyDown);
$(window).on('keyup', game.keyUp);
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
body, html, .viewport {
    width: 100%;
    height: 100%;
}
.viewport {
    position: relative;
    overflow: hidden;
}
.land {
    position: absolute;
    left: 0;
    top: 0;
    width: 2300px;
    height: 200px;
    background: url('//dummyimage.com/2300x400/000/fff&text=Mario+is+great!+Mario+is+our+hero!+We+love+you+mario!') no-repeat center center;
    background-size: cover;
    will-change: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="viewport">
    <div class="land"></div>
</div>

同样在 Playground

答案 1 :(得分:0)

这是你在Javascript / HTML5中的方法

theGame.js

var getPlatF1POSX = 0;
var getPlatF1POSY = 0;
var addVar = 0;
var addVar2 = 0;
var addVar3 = 0;

var thisThis = 1;
var moveBlock1 = 350;
var stLandT = 0;

var moveRight = false;
var moveLeft = false;
var movePL = 0;
var movePR = 0;

//////////////////////////////////////////////////////////
//
// LOAD PLATFORMS/SET KEY UP AND DOWN/SET PLAYER POS 


function loadGame() {
    document.getElementsByTagName("DIV")[4].style.visibility = "visible";
    addEventListener('mousemove', getData, false);
    addEventListener('keydown', movePlayer, false);
    addEventListener('keyup', stopPlayer, false);
    moveP();

    document.getElementById("player").style.left = xPos + "px";
    document.getElementById("player").style.top = yPos + "px";  
}

function getData(gData) {

}

//////////////////////////////////////////////////////////
//
// KEY DOWN TO MOVE PLAYER 

function movePlayer(mPlayer) {
    switch (mPlayer.keyCode) {
        case 39: // RIGHT
            if (stLandT == 0) {
                setThis = setTimeout(landT, 500);
                thisSet = setTimeout(moveLand, 30);
                stLandT = 1;    
            }

                movePL = 0;
                movePR = 1;

        break;

        case 37: // LEFT
            if (stLandT == 0) {
                setThis = setTimeout(landT, 500);
                thisSet = setTimeout(moveLand, 30);
                stLandT = 1;    
            }

                movePL = 1;
                movePR = 0;

        break;

        case 38: // UP

        break;

        case 40: // DOWN

        break;
    }
}


//////////////////////////////////////////////////////////
//
// KEY UP TO STOP PLAYER/VOID STOP AND GO GLITCH

function stopPlayer(sPlayer) {
    if (sPlayer.keyCode == 39) {
        clearTimeout(setThis);
        clearTimeout(thisSet);
        stLandT = 0;
        movePR = 0;
    }

    if (sPlayer.keyCode == 37) {
        clearTimeout(setThis);
        clearTimeout(thisSet);
        stLandT = 0;
        movePL = 0;
    }
}

landThis.js / MOVE PLAYER AND PLATFORMS

var cTAdd = 0;
var setThis = 1;
var GAPlayer = 3;

//////////////////////////////////////////////////////////
//
// SHOW PLATFORMS TO MOVE

function landT() {
    setThis = setTimeout(landT, 500);

    if (xPos >= 500) {
        cTAdd = Math.floor(Math.random() * 100 + 1);

        var block00 = document.createElement("img");

        if (cTAdd > 0 && cTAdd < 25) {
            block00.src = "images/sep2.png";
        }
        if (cTAdd > 25 && cTAdd < 50) {
            block00.src = "images/sep1.png";
        }
        if (cTAdd > 50 && cTAdd < 100) {
            block00.src = "images/platform00.png";
        }

        document.getElementById("land01").appendChild(block00);

        var block01 = document.createElement("img");
        var getB = block01.getBoundingClientRect();


        if (cTAdd > 0 && cTAdd < 25) {
            block01.src = "images/platform00.png";
        }
        if (cTAdd > 25 && cTAdd < 50) {
            block01.src = "images/sep2.png";
        }
        if (cTAdd > 50 && cTAdd < 100) {
            block01.src = "images/sep1.png";
        }

        document.getElementById("land00").appendChild(block01);

        GAPlayer = GAPlayer + 2;
    }

}

//////////////////////////////////////////////////////////
//
// MOVE PLATFORMS 

var thisSet = 1;
var cPlayer = 0;
var moveSpeed = 5;
var xPos = 50;
var yPos = 300;
function moveLand() {

    thisSet = setTimeout(moveLand, 30);

    if (xPos >= 500) {
        moveBlock1 = moveBlock1 - 10;
        document.getElementById("land00").style.left = moveBlock1 + "px";
        document.getElementById("land01").style.left = moveBlock1 + "px";
    }

}


//////////////////////////////////////////////////////////
//
// MOVE PLAYER

var setP = 1;
function moveP() {
    setP = setTimeout(moveP, 10);


    if (movePR == 1) {
        xPos = xPos + moveSpeed;

        cPlayer++;
        if (cPlayer >= 4) 
            cPlayer = 0;
            document.images[GAPlayer].src = gPlayer[cPlayer].src;
    }


    if (movePL == 1) {
        xPos = xPos - moveSpeed;

        cPlayer++;
        if (cPlayer >= 4) 
            cPlayer = 0;
            document.images[GAPlayer].src = gPlayer[cPlayer].src;
    }

    document.getElementById("player").style.left = xPos + "px";
    document.getElementById("player").style.top = yPos + "px";  


    if (xPos >= 500) {
        xPos = 500; 
    }
    if (xPos <= 50) {
        xPos = 50;  
    }   
}