Domanda

i'm new into programming and I was trying to make an sort of race with a rocket. The rocket has to reach the top of the page and when it has reached the top of the page I want an alert to come out(?). What is the best of way doing this? Thanks in advance!

Ive edited my post, I used the "if" just under my function downpressed.. But it doesnt work.

    function leftArrowPressed() {
        var element = document.getElementById("image1");
        element.style.left = parseInt(element.style.left) - 5 + 'px';
        }

        function rightArrowPressed() {
        var element = document.getElementById("image1");
        element.style.left = parseInt(element.style.left) + 5 + 'px';

        }

        function upArrowPressed() {
        var element = document.getElementById("image1");
        element.style.top = parseInt(element.style.top) - 5 + 'px';
        }

        function downArrowPressed() {
        var element = document.getElementById("image1");
        element.style.top = parseInt(element.style.top) + 5 + 'px';
        }




        function moveSelection(evt) {

            if(parseInt(element.style.top)==0)
            {
                alert("some text there");
            }
            switch (evt.keyCode) {
                case 37:
                leftArrowPressed();
                break;
                case 39:
                rightArrowPressed();
                break;
                case 38:
                upArrowPressed();
                break;
                case 40:
                downArrowPressed();
                break;
                }
            };

    function docReady()
    {

      window.addEventListener('keydown', moveSelection);
    }
È stato utile?

Soluzione

if(parseInt(document.getElementById("image1").style.top)<=0)
{
alert("some text there");
}

Place it in moveSelection(), before switch.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top