Skip to main content
kevino4736006
Participant
April 3, 2018
Question

Hover left and right

  • April 3, 2018
  • 1 reply
  • 221 views

I want it so everytime I hover over a specific object I can move it just left or right. I have attached a photo of the scene. Basically, I want it so when you hover your mouse over the knife you can move it left or right (not up or down).

This topic has been closed for replies.

1 reply

Colin Holgate
Inspiring
April 3, 2018

If you move your mouse quickly, and get outside of the width of the knife, would it stop following the mouse?

You could make it just follow the mouse all the time, and not insist that the user has to initially point at the knife. You could have code like this ('knife' is the instance name you gave the knife movieclip in the Properties panel):

addEventListener(Event.ENTER_FRAME,moveknife);

function moveknife(e:MouseEvent){

knife.x = stage.mouseX;
}

If you want to make sure the mouse is within the knife, you could do this:

addEventListener(Event.ENTER_FRAME, moveknife);

function moveknife(e: Event) {

  if (knife.hitTestPoint(stage.mouseX, stage.mouseY, true)) {

      knife.x = stage.mouseX;

  }

}

You'll see though that it's easy to move away from the knife and lose control of it.

kevino4736006
Participant
April 3, 2018

The second one is perfect! Thank you so much!