Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Make Object Face Mouse Pointer

Good day,

I am currently designing a game, for which I need an object to face the mouse pointer. Can anyone tell me where I went wrong? Thanks a lot.

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" style="text/css" href="game.css">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="game.js"></script>
    <title>Rocket Game</title>
  </head>
  <body>
    <img id="rocket" src="rocket.jpg">
  </body>
</html>
#rocket {
  position: absolute;
  left: 250px;
  top: 200px;
}
var rocket = $("#rocket");
var rocketCenter=[rocket.offset().left+rocket.width()/2,
                  rocket.offset().top+rocket.height()/2];
$(document).ready(function(){
  $(document).mousemove(function(e){
    var angle = Math.atan2(e.pageX - rocketCenter[0], - (e.pageY - rocketCenter[1]) )*(180/Math.PI);        

      rocket.css({ "-webkit-transform": 'rotate(' + angle + 'deg)'});    
      rocket.css({ '-moz-transform': 'rotate(' + angle + 'deg)'});
      rocket.css({ 'transform': 'rotate(' + angle + 'deg)'});
  });
});

1 Answer

liktid
liktid
6,387 Points

When i get you right you need the mouse showing the pointer when hover over the #rocket ? Then all you need to do is to add cursor:pointer; to your css

#rocket {
  position: absolute;
  left: 250px;
  top: 200px;
  cursor:pointer;
}

Hope this helps

No, I actually want the rocket to point to where the mouse is, wherever that may be. :)