开发者

Own drag function in AS3

开发者 https://www.devze.com 2023-04-07 16:37 出处:网络
I need to develop my own drag function in AS3 (instead of using startDrag) because I\'m resizing a MovieClip.

I need to develop my own drag function in AS3 (instead of using startDrag) because I'm resizing a MovieClip.

I'm doing this:

public class resizeBR extends MovieClip {

        var initialScaleX, initialScaleY;

        public function resizeBR() {            
            this.addEventListener(MouseEvent.MOUSE_DOWN, initResize);
            this.addEventListener(MouseEvent.MOUSE_UP, stopResize);
        }

        public function initResize(e:MouseEvent):void
        {
            initialScaleX = e.target.scaleX;
            initialScaleY = e.target.scaleY;
            e.target.addEventListener(MouseEvent.MOUSE_MOVE, startResize);
        }

  开发者_如何转开发      public function startResize(e:MouseEvent):void
        {
            e.target.x +=  e.localX;
            e.target.y +=  e.localY;
            e.target.parent.parent.width +=  mouseX;
            e.target.parent.parent.height +=  mouseY;
            // Keep its own scale
            e.target.scaleX = initialScaleX;
            e.target.scaleY = initialScaleY;

        }

        public function stopResize(e:MouseEvent):void
        {
            e.target.removeEventListener(MouseEvent.MOUSE_MOVE, startResize);
        }
    }

But the drag feature is not working fluently. I mean, when I drag a MovieClip from class resizeBR I need to move slowly my mouse cursor or it's not going to work propertly.

resizeBR is a MovieClip as a child of another MovieClip; the second one is which I have to resize.

What am I doing wrong?

Thanks!


Thanks all for your answers, but I found a great classes to do what I want.

http://www.senocular.com/index.php?id=1.372

http://www.quietless.com/kitchen/transform-tool-drag-scale-and-rotate-at-runtime/


I'm not really sure if I completely understand what you mean. But I think your problem lies with your MOUSE_MOVE handler.

In your current example you're resizing your target only when moving your mouse over the target. When you're moving your mouse fast enough it's possible your mouse leaves the target, casuing it to stop resizing. When I'm writing my own drag handlers I usually set the MOUSE_MOVE and MOUSE_UP listeners to the stage.

Your class would end up looking something like this:

public class resizeBR extends MovieClip 
{

    var initialScaleX, initialScaleY;

    public function resizeBR() 
    {            
        addEventListener(MouseEvent.MOUSE_DOWN, initResize);
        addEventListener(MouseEvent.MOUSE_UP, stopResize);
    }

    public function initResize(e:MouseEvent):void
    {
        initialScaleX = scaleX;
        initialScaleY = scaleY;
        stage.addEventListener(MouseEvent.MOUSE_MOVE, startResize);
    }

    public function startResize(e:MouseEvent):void
    {
        x +=  e.localX;
        y +=  e.localY;
        parent.parent.width +=  mouseX;
        parent.parent.height +=  mouseY;

        // Keep its own scale
        scaleX = initialScaleX;
        scaleY = initialScaleY;
    }

    public function stopResize(e:MouseEvent):void
    {
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, startResize);
    }
}


There are a couple reasons the resizing is jumpy. First, like rvmook points out, you'll need to make sure you support the mouse rolling off of the clip while its being resized. Since there is not an onReleaseOutside type of event in AS3, you have to set listeners to the stage, or some other parent clip. If you have access to the stage, that is best. If not, you can use the root property of your resizable clip, which will reference the highest level display object you have security access to. Setting mouse events to the root is a little wonky, because for them to fire, the mouse needs to be on one of the root's child assets - whereas the stage can fire mouse events when the mouse is over nothing but the stage itself.

Another reason you might be seeing some strange resizing behavior is because of using the localX/Y properties. These values reflect the mouseX/mouseY coordinates to the object being rolled over - which might not necessarily be your clip's direct parent.

I tend to avoid having classes access their parent chain. You might want to consider placing the resizing logic in the clip you want resized, and not in one of its children. Here is simple self resizing example:

package {

import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;

public class ResizableBox extends MovieClip {

    public function ResizableBox() {
        addEventListener(MouseEvent.MOUSE_DOWN, startResize);
    }

    private function startResize(evt:MouseEvent):void {
        stage.addEventListener(MouseEvent.MOUSE_MOVE, handleResize);
        stage.addEventListener(MouseEvent.MOUSE_UP, stopResize);
    }

    private function stopResize(evt:MouseEvent):void {
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, handleResize);
        stage.removeEventListener(MouseEvent.MOUSE_UP, stopResize);
    }

    private function handleResize(evt:MouseEvent):void {
        this.scaleX = this.scaleY = 1;
        this.width = this.mouseX;
        this.height = this.mouseY;
    }
}

}

ResizableBox is set as the base class of a MC in the library.

0

精彩评论

暂无评论...
验证码 换一张
取 消