as3简单的滑块

时间:2011-01-21 00:42:24

标签: flash actionscript-3

我的目标是在as3中创建一个简单的滑翔机 这几乎可以..但唯一的麻烦是: 当您单击拖动滑块并从轨道中退出时,滑块仍在被拖动。

我想有一个简单的伎俩可以防止这种令人讨厌的效果

package  {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.geom.Rectangle; public class SliderH extends MovieClip {
    public function SliderH() {
        trace("creation");
        addEventListener(MouseEvent.MOUSE_DOWN, mousedown);
        addEventListener(MouseEvent.MOUSE_UP, mouseup);
        this.buttonMode = true;
    }
    private function mousedown(e:MouseEvent):void {
        trace("begin");
        var rect:Rectangle = new Rectangle(-this.parent.width/2,0,this.parent.width,0);
        startDrag(false , rect);

    }
    private function mouseup(e:MouseEvent):void {
        stopDrag();
        trace(this.x);
    }       
}

}

2 个答案:

答案 0 :(得分:2)

虽然我一直都在忙着学习和学习,但Keith Peters有一个非常简单易用的简单库。 (如果您正在尝试创建简单的控件)

http://www.bit-101.com/blog/?p=2979

但除此之外,您应该添加ROLL_OUT事件flash.events.MouseEvent.ROLL_OUT

addEventListener(MouseEvent.ROLL_OUT, mouseup);

答案 1 :(得分:0)

如果你的鼠标太远,你想阻止滑块移动,

我会通过为鼠标移动时添加一个事件监听器来做到这一点,如果距离滑块的距离变得太大,请尽早停止拖动,我不确定滑块是如何工作的,但这可能有所帮助。

package  {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.geom.Rectangle; public class SliderH extends MovieClip {
    public function SliderH() {
        trace("creation");
        addEventListener(MouseEvent.MOUSE_DOWN, mousedown);
        addEventListener(MouseEvent.MOUSE_UP, mouseup);
        addEventListener(MouseEvent.MOUSE_MOVE,mousemove);
        this.buttonMode = true;
    }
    private function mousedown(e:MouseEvent):void {
        trace("begin");
        var rect:Rectangle = new Rectangle(-this.parent.width/2,0,this.parent.width,0);
        startDrag(false , rect);

    }
    private function mouseup(e:MouseEvent):void {
        stopDrag();
        trace(this.x);
    }
    private function mousemove(e:MouseEvent):void{
        if (Math.abs(this.x)>this.parent.width/2){
            stopDrag();
        }
    }       
}