AS3 How to startdrag only on x-axis?

前端 未结 3 1747
攒了一身酷
攒了一身酷 2020-12-19 10:33

I have a red square that I want to drag only on the x-axis. I\'ve worked out a simple script, which theoretically should work, but it\'s not behaving properly. It\'s a bit h

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-19 10:51

    Glad Marty's solution worked for you, although it is not too efficient (that MouseEvent.MOUSE_MOVE listener is a killer). The problem with your original code was that the rectangle in which you limit the dragging boundaries must be relative to the parent's coordinates. Also, depending on where you have the registration point of your square, you might have to take its width into account, if you don't want any part of it to move out of the stage.

    For example, if your red square is directly on the stage, its registration point is located on its centre, and you want to limit the dragging to the whole x-axis of the stage, this will work:

    e.currentTarget.startDrag(
           false,
           new Rectangle(
              e.currentTarget.width/2,
              e.currentTarget.y,
              stage.stageWidth-e.currentTarget.width,
              0
           )
    );
    

提交回复
热议问题