How to make an object follow the mouseX when clicked on
How to make an object follow the mouseX when clicked on? Please help.
How to make an object follow the mouseX when clicked on? Please help.
You should think it thru - your code doesn't match your very simple explanation of what you said it should do, and it should.... click something and have it follow the mouse.
A game loop (ENTER_FRAME) could work, but in this case it wastes unnecessary processing if the mouse is not moving. And in your code example it is not waiting for the click to occur.
Use a MOUSE_MOVE event listener instead of an ENTER_FRAME. Inside the clicking event handler function is where you should be defining the MOUSE_MOVE listener. The handler for the MOUSE_MOVE listener is where you assign rock1Cnt.x = mouseX.
public function Main():void {
rock1Cnt.addEventListener(MouseEvent.CLICK, mouseClickHandler);
}
private function mouseClickHandler(e:MouseEvent):void {
stage.addEventListener(Event.MOUSE_MOVE, followMouse);
}
private function followMouse(e:MouseEvent):void {
rock1Cnt.x = mouseX
}
Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.