Ejm gamepad

De wikiserver
Dreceres ràpides: navegació, cerca
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        #bola{
            width:100px;
            height: 100px;
            background-color:red;
            position: relative;
            top:0px;
            left: 0px;


        }
    </style>
    <script type="text/javascript">

var b=1;
var a=1;
var botones;
        function gameLoop() {
            if(navigator.webkitGetGamepads) {
                var gp = navigator.webkitGetGamepads()[0];
            } else {
                var gp = navigator.getGamepads()[0];
            }

            if(gp.axes[0] < -0.8) {
               a -= 1;
            } else if(gp.axes[0] > 0.8) {
               a += 1;
            } else if(gp.axes[1] < -0.8) {
                b -= 1;
            } else if(gp.axes[1] >0.8) {
                b +=1;
            }

            document.getElementById("bola").style.left = a*20 + "px";
            document.getElementById("bola").style.top = b*20 + "px";

            var botones=gp.buttons;
            if(botones[0].pressed){
                //alert("Boton A");
                document.getElementById("texto").innerHTML="A";

            }
            if(botones[1].pressed){
                //alert("Boton B");
                document.getElementById("texto").innerHTML="B";
            }
            if(botones[2].pressed){
                //alert("Boton X");
                document.getElementById("texto").innerHTML="X";
            }
            if(botones[3].pressed){
                //alert("Boton Y");
                document.getElementById("texto").innerHTML="Y";
            }



        };


   setInterval(gameLoop,100);

    </script>
</head>
<body>


<div id="bola"> </div>
<div id="texto"></div>
</body>
</html>