Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialnick88p
5,716 PointsMake 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
6,387 PointsWhen 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
nick88p
5,716 Pointsnick88p
5,716 PointsNo, I actually want the rocket to point to where the mouse is, wherever that may be. :)