mercredi 22 août 2018

Simple Basketball Motion Javascript

What happen : when I press space button the ball move up and stop , when press again the ball move down

What I need : When I press space button the ball move up then move down !

I want to some how repeat this function twice with one click ... Its look simple , I try to loop twice when press the space button , but it doesn't work as I expect ? any suggestions ?

var body = document.getElementById('body');
var basketball = document.getElementById('basketball');

var x = 0;
var y = 0;
var counter = 0;
var inc = Math.PI / 100  ;



body.addEventListener('keydown',function(e){
    var ek = e.which; 
    console.log('keydown');
    if(ek==32){
        
            for( var i = 0; i<=1 ;i+=0.01){
                x+=i;
                y+= Math.sin( counter );
                counter+=inc;

                basketball.style.left=x;
                basketball.style.bottom=y; 
            }    
    }
});
*
{
    transition: all 1s;
}

#basketball
{
    width: 75px;
    position: absolute;
    bottom: 0;
    left: 10;
}
<html>
    <head>                
        <link rel="stylesheet" href="css/index_style.css">        
        <title>Basket</title>
    </head>
    <body id="body">       
        <img src="https://thumbs.dreamstime.com/b/basketball-ball-transparent-checkered-background-realistic-vector-illustration-91566559.jpg" id="basketball">
        
        
        <script src="js/mine.js"></script>
    </body>
</html>



Aucun commentaire:

Enregistrer un commentaire