JavaScript - 在激光增量周期中停止激光

时间:2016-09-29 19:23:57

标签: javascript html css

要查看一个工作示例,只需将代码复制到notepad ++并在chrome中作为.html文件运行,我在使用代码段或代码笔获取工作示例时遇到问题,如果可以获得,我会给这些网站提供一个链接它在它们中工作。

问题是;一旦我按照我想要的方式完成激光就会激光。它随lzxR ++递增;直到它击中游戏竞技场的边界但是如果我击中空格键WHILST激光器正在移动代码再次迭代并试图在两个地方同时显示激光看起来很糟糕且非常不稳定,那么我怎样才能让它工作所以,如果我第二次击中空格键,即使激光是中间增量 - 它也会停止增加并简单地拍摄一个新的激光,而不是试图一次增加多个激光器???

下面是代码:

<html>

<head>
    <style>
        #blueCanvas {
            position: absolute;
            background-color: black;
            width: 932px;
            height: 512px;
            border: 1px solid black;
            top: 20px;
            left: 20px;
        }

        #blueBall {
            position: relative;
            background-color: white;
            border: 1px solid blue;
            width: 10px;
            height: 10px;
            border-radius: 100%;
            top: 0px;
            left: 0px;
        }

        #laser {
            position: absolute;
            background-color: white;
            border: 1px solid blue;
            width: 10px;
            height: 1px;
            top: 10px;
            left: 10px;
        }

        #pixelTrackerTop {
            position: absolute;
            top: 530px;
            left: 20px;
        }

        #pixelTrackerLeft {
            position: absolute;
            top: 550px;
            left: 20px;
        }
    </style>

    <title>Portfolio</title>
    <script src="https://ajax.googleapis.com/
    ajax/libs/jquery/1.12.4/jquery.min.js">
    </script>
    <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
        document.addEventListener("keydown", keyBoardInput);
        var topY = 0;
        var leftX = 0;
        var lzrY = 0;
        var lzrX = 0;

        function moveUp() {
            var Y = document.getElementById("blueBall");
            topY = topY -= 1;
            Y.style.top = topY;
            masterTrack();
            if (topY < 1) {
                topY = 0;
                Y.style.top = topY;
            };
            stopUp = setTimeout("moveUp()", 1)
                /**allows for progression of speed with additional key strokes**/
            topStop();
            stopConflictYup();
            console.log('moveUp');
        };

        function moveDown() {
            var Y = document.getElementById("blueBall");
            topY = topY += 1;
            Y.style.top = topY;
            masterTrack();
            if (topY > 500) {
                topY = 500;
                Y.style.top = topY;
            };
            stopDown = setTimeout("moveDown()", 1)
                /**allows for progression of speed     with additional key strokes**/
            topStop();
            stopConflictYdown();
            console.log('moveDown');
        };

        function moveLeft() {
            var X = document.getElementById("blueBall");
            leftX = leftX -= 1;
            X.style.left = leftX;
            masterTrack();
            if (leftX < 1) {
                leftX = 0;
                Y.style.leftX = leftX;
            };
            stopLeft = setTimeout("moveLeft()", 1)
                /**allows for progression of speed with additional key strokes**/
            leftStop();
            stopConflictXleft();
            console.log('moveLeft');
        };

        function moveRight() {
            var X = document.getElementById("blueBall");
            leftX = leftX += 1;
            X.style.left = leftX;
            masterTrack();
            if (leftX > 920) {
                leftX = 920;
                Y.style.leftX = leftX;
            };
            stopRight = setTimeout("moveRight()", 1)
                /**allows for progression of speed with additional key strokes**/
            leftStop();
            stopConflictXright();
            console.log('moveRight');
        };

        function masterTrack() {
            var pxY = topY;
            var pxX = leftX;
            document.getElementById('pixelTrackerTop').innerHTML =
                'Top position is ' + pxY;
            document.getElementById('pixelTrackerLeft').innerHTML =
                'Left position is ' + pxX;
        };

        function topStop() {
            if (topY <= 0) {
                clearTimeout(stopUp);
                console.log('stopUp activated');
            };
            if (topY >= 500) {
                clearTimeout(stopDown);
                console.log('stopDown activated');
            };
        };

        function leftStop() {
            if (leftX <= 0) {
                clearTimeout(stopLeft);
                console.log('stopLeft activated');
            };
            if (leftX >= 920) {
                clearTimeout(stopRight);
                console.log('stopRight activated');
            };
        };

        function stopConflictYup() {
            clearTimeout(stopDown);
        };

        function stopConflictYdown() {
            clearTimeout(stopUp);
        };

        function stopConflictXleft() {
            clearTimeout(stopRight);
        };

        function stopConflictXright() {
            clearTimeout(stopLeft);
        };

        function shootLaser() {
            var l = document.getElementById("laser");
            var lzrY = topY;
            var lzrX = leftX;
            fireLaser();

            function fireLaser() {
                l.style.left = lzrX; /**initial x pos **/
                l.style.top = topY; /**initial y pos **/
                var move = setInterval(moveLaser, 1);
                /**continue to increment laser unless IF is met**/
                function moveLaser() { /**CALL and start the interval**/
                    var bcrb = document.getElementById("blueCanvas").style.left;
                    if (lzrX > bcrb + 920) {
                        /**if the X axis of the laser goes beyond the  

                               blueCanvas 0 point by 920 then stop incrementing the laser on its X   

                               axis**/
                        clearInterval(move);
                        /**if statement was found true so stop increment of laser**/
                    } else {
                        lzrX++;
                        l.style.left = lzrX;
                    };
                };
            };
        };

        function keyBoardInput() {
            var i = event.keyCode;
            if (i == 32) {
                shootLaser();
            };
            if (i == 38) {
                if (topY > 0) {
                    moveUp();
                };
            };
            if (i == 40) {
                if (topY < 500) {
                    moveDown();
                };
            };
            if (i == 37) {
                if (leftX > 0) {
                    moveLeft();
                };
            };
            if (i == 39) {
                if (leftX < 920) {
                    moveRight();
                };
            };
        };
        /**

    !! gradual progression of opacity is overall
    !! being able to speed up element is best done with setTimout
    !! setInterval is constant regards to visual speed
    !! NEXT STEP IS ARRAYS OR CLASSES 
    IN ORDER TO SHOOT MULITPLE OF SAME     ELEMENT? MAYBEE?


    var l = document.getElementById("laser");
    lzrX = lzrX += 1;
    l.style.left = lzrX;
    lzrY = topY += 1;
    l.style.top = lzrY;
    **/
    </SCRIPT>

</head>

<div id="blueCanvas">
    <div id="laser"></div>
    <div id="blueBall">

    </div>
</div>

<p id="pixelTrackerTop">Top position is 0</p>
<br>
<p id="pixelTrackerLeft">Left position is 0</p>

</body>

</html>

1 个答案:

答案 0 :(得分:0)

解决了使用名为“g”的变量并在激光射击后递增的问题!

var g = 0;
function keyBoardInput() {
var i = event.keyCode;


  if (i == 32) {
  if (g < 1) {
  shootLaser();
  g++;
  };


  };