Copy link to clipboard
Copied
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 MO
...Copy link to clipboard
Copied
You can assign a MOUSE_MOVE event listener when the object gets clicked and have the event handler for that listener match the object's x and y properties to the mouseX and mouseY properties.
Copy link to clipboard
Copied
Thanks
Can you help me a bit more I still don't understand fully. Can you show me an example. Please.
Copy link to clipboard
Copied
This is what I has so far and it does not work. I think it should be in a gameloop but it does not seem to go in a agmeloop.
public function Main():void {
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
rock1Cnt.addEventListener(MouseEvent.CLICK, mouseClickHandler);
}
private function mouseClickHandler(e:MouseEvent):void
{
rock1Cnt.x = mouseX
}
Copy link to clipboard
Copied
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
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now