AS3 How to start only on the x axis?

I have a red square that I want to drag only along the x axis. I developed a simple script that should theoretically work, but it does not behave correctly. This is a little difficult to explain ... the square is held in the wrong position, and the position on the stage seems to be changing, so sometimes you cannot drag the square all the way to the right ...

red.buttonMode = true; red.addEventListener(MouseEvent.MOUSE_DOWN, dragHandler); function dragHandler(e:MouseEvent):void { var ypos:Number = e.currentTarget.y; var xpos:Number = e.currentTarget.x; e.currentTarget.startDrag(false,new Rectangle(-xpos,ypos,stage.stageWidth,0)); } red.addEventListener(MouseEvent.MOUSE_UP, dropHandler); function dropHandler(e:MouseEvent) { //trace("red up"); e.currentTarget.stopDrag(); } 
+6
source share
3 answers

You can try a different approach that includes MouseEvent.MOUSE_MOVE , since using a rectangle for a dynamic border will be difficult.

 // define lock on y-axis var LOCKY:Number = target.y; // MouseEvent.MOUSE_MOVE stage.addEventListener(MouseEvent.MOUSE_MOVE, _mouseMove); function _mouseMove(e:MouseEvent):void { if(target.y != LOCKY) target.y = LOCKY; } // dragging target.addEventListener(MouseEvent.MOUSE_DOWN, _mouseDown); function _mouseDown(e:MouseEvent):void { target.startDrag(); target.addEventListener(MouseEvent.MOUSE_UP, _mouseUp); } // dropping function _mouseUp(e:MouseEvent):void { target.stopDrag(); target.removeEventListener(MouseEvent.MOUSE_UP, _mouseUp); } 
+3
source

The Glad Marty solution worked for you, although it is not too efficient (the MouseEvent.MOUSE_MOVE listener is a killer). The problem with your source code was that the rectangle in which you are restricting the drag borders should be relative to the parent coordinates . In addition, depending on where you have the registration point of your square, you may need to consider its width if you do not want any part of it moving out of the scene.

For example, if your red square is directly on the stage, its registration point is located in the center, and you want to limit drag and drop to the entire x-axis of the scene, this will work:

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

IT WORKS ACTIONSCRIPT 3 (Semilla Sol Apps)

1- create regular MOUSE DOWN and MOUSE UP event listeners

2- discard your startDrag and stopDrag "elements in each of the functions, respectively

HERE HOW TO CLOSE THE DRYING:

1: create an event listener for the object you want to restrict ... in this example, this is a movieclip instance named " player ":

 player.addEventListener(Event.ENTER_FRAME, pLimiter); 

HERE 'pLimter' FUNCTION:

 function pLimiter(e:Event):void { player.y = stage.stageHeight; } 

So, in this case, we restrict the โ€œdrag and dropโ€ only to the x axis.

  • party at the dude! (Semilla Sol Apps)
0
source

Source: https://habr.com/ru/post/891068/


All Articles