Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to make an object follow the mouseX when clicked on

New Here ,
Oct 26, 2013 Oct 26, 2013

How to make an object follow the mouseX when clicked on? Please help.

TOPICS
ActionScript
657
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Oct 27, 2013 Oct 27, 2013

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

...
Translate
LEGEND ,
Oct 26, 2013 Oct 26, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 26, 2013 Oct 26, 2013

Thanks

Can you help me a bit more I still don't understand fully. Can you show me an example. Please.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 26, 2013 Oct 26, 2013

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

                    }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 27, 2013 Oct 27, 2013
LATEST

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

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines