movement of the object



  • There's an object in front of him, he moves forward (to the right) when pressed behind him, backwards (to the left).

    var car = document.querySelector(".car");
    var road = document.querySelector(".road");
    car.style.left = '1px';
    

    var go = new Object;
    go = {
    right : function(){
    car.style.left = parseInt(car.style.left) + 20 + 'px';
    },
    left : function(){
    car.style.left = parseInt(car.style.left) - 20 + 'px';
    }
    }

    road.addEventListener('click', function(e){
    var value = Math.round(e.pageX) + 'px';
    if(value > car.style.left){
    go.right();
    }
    if(value < car.style.left){
    go.left();
    }
    }, false)

    But whatever it takes to pass the courseor (from behind or at the front of the object) it only moves left. Can you tell me how to fix this?



  • if(value > car.style.left){
    
    if(value > parseInt(car.style.left, 10)) {
    

    And the other places are the same.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2