Skip to main content
Participating Frequently
February 21, 2011
Answered

Mouse Movement Navigation

  • February 21, 2011
  • 1 reply
  • 361 views

I am trying to make something in which where if the coursor is on the edges of the document, an object will move around. If the cursor is in the upper left corner, the object will both x and y values will be increased. This is what I have, but it does not work. I am not good at coding, and it has been some time. Also, if someone could help me simplify my code, that would be awesome.

//General Movement
     if (mouseX<5)
     {
          if (example_mc.x<0)
          {
               example_mc.x = example_mc.x + 1;
          }
     }
     if (mouseX>1020)
     {
          if (example_mc.x>-856)
          {
          example_mc.x = example_mc.x -1;
          }
     }
     if (mouseY<5)
     {
          if (example_mc.y<0)
          {
          example_mc.y = example_mc.y + 1;
          }
     }
     if (mouseY>760)
     {
          if (example_mc.y>-235)
          {
          example_mc.y = example_mc.y -1;
          }
     }

This topic has been closed for replies.
Correct answer Ned Murphy

You'll probably have to work out the details of what happens when and what mopves where, but try changing your code as shown below so that it is constantly checking to see if movement is needed...

stage.addEventListener(Event.ENTER_FRAME, moveObject);

function moveObject(evt:Event):void {

     if (mouseX<5)
     {
          if (example_mc.x<0)
          {
               example_mc.x = example_mc.x + 1;
          }
     }
     if (mouseX>1020)
     {
          if (example_mc.x>-856)
          {
          example_mc.x = example_mc.x -1;
          }
     }
     if (mouseY<5)
     {
          if (example_mc.y<0)
          {
          example_mc.y = example_mc.y + 1;
          }
     }
     if (mouseY>760)
     {
          if (example_mc.y>-235)
          {
          example_mc.y = example_mc.y -1;
          }
     }

}

1 reply

Ned Murphy
Legend
February 21, 2011

You'll probably need to provide a better explanation of what you are trying to create.  The title mentions mouse movement, but the description only mentions location.

Participating Frequently
February 21, 2011

By movement, I mean if the mouse is in a certain position, the position of another object will change accordingly. The object I want to move around will be the main focus of the document, so when the cursor is on the right side of the document, it will look as if the viewer is moving to the right (or the object to the left, depending on point of view). Does this help clarify what I am going for?

Ned Murphy
Ned MurphyCorrect answer
Legend
February 21, 2011

You'll probably have to work out the details of what happens when and what mopves where, but try changing your code as shown below so that it is constantly checking to see if movement is needed...

stage.addEventListener(Event.ENTER_FRAME, moveObject);

function moveObject(evt:Event):void {

     if (mouseX<5)
     {
          if (example_mc.x<0)
          {
               example_mc.x = example_mc.x + 1;
          }
     }
     if (mouseX>1020)
     {
          if (example_mc.x>-856)
          {
          example_mc.x = example_mc.x -1;
          }
     }
     if (mouseY<5)
     {
          if (example_mc.y<0)
          {
          example_mc.y = example_mc.y + 1;
          }
     }
     if (mouseY>760)
     {
          if (example_mc.y>-235)
          {
          example_mc.y = example_mc.y -1;
          }
     }

}