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

Move object while mouse is pressed

Guest
Mar 24, 2014 Mar 24, 2014

I want to move an object right or left while the mouse left button is held down. If the mouse is pressed and located on the left half of the screen, I want the object to move left (decrease X) and vice versa for right.

My current solution is to create two buttons covering the screen and adding MouseEvent.CLICK listeners , as so:

btn_clickleft.addEventListener(MouseEvent.CLICK, btn_clickleft_mouse);

function btn_clickleft_mouse(e:MouseEvent):void {

          object_to_move.x-=10;

}

btn_clickright.addEventListener(MouseEvent.CLICK, btn_clickright_mouse);

function btn_clickright_mouse(e:MouseEvent):void {

  object_to_move.x+=10;

}


But this causes two problems:

1. The object only moves 10 pixels once per mouse click, how do I make it continously move?

2. If the object I want to move passes below the mouse cursor, the events stop firing.

Is there a solution that solves these two problems?

TOPICS
ActionScript
2.3K
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
Community Beginner ,
Mar 24, 2014 Mar 24, 2014

Hey!

So, following your logic, to solve the issue number 1, I would do something like this:

import flash.events.MouseEvent;

import flash.events.Event;

leftbtn.addEventListener(MouseEvent.MOUSE_DOWN, startmoveleft);

rightbtn.addEventListener(MouseEvent.MOUSE_DOWN, startmoveright);

leftbtn.addEventListener(MouseEvent.MOUSE_UP, stopmoveleft);

rightbtn.addEventListener(MouseEvent.MOUSE_UP, stopmoveright);

function startmoveleft(evt:MouseEvent):void{

          addEventListener(Event.ENTER_FRAME, moveleft);

}

function startmoveright(evt:MouseEvent):void{

          addEventListener(Event.ENTER_FRAME, moveright);

}

function stopmoveleft(evt:MouseEvent):void{

          removeEventListener(Event.ENTER_FRAME, moveleft);

}

function stopmoveright(evt:MouseEvent):void{

          removeEventListener(Event.ENTER_FRAME, moveright);

}

function moveleft(evt:Event):void{

          objectmc.x -= 10;

}

function moveright(evt:Event):void{

          objectmc.x += 10;

}

Where leftbtn and rightbtn are the buttons you placed on each half of the stage and objectmc is the object you want to move.

As for issue number 2, just make sure to put the two buttons in a layer on top of the layer where you have the object to move.

I hope this works for you!

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
Community Beginner ,
Mar 24, 2014 Mar 24, 2014

Thanks!

If I put the buttons "above" the object, they cover it up completely since they stretch across the whole screen, can I make the buttons click-able while not being visible in the compiled .swf? I tried setting alpha to 0 and unchecking a "Visible" box, but that disables the events.

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
Community Beginner ,
Mar 24, 2014 Mar 24, 2014
LATEST

For sure, leave the "Visible" box checked and set the alpha to 0, it should work with that setting.

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