as3 simple slider
My goal is tio buid a simpleslider in as3 This nearly works.. but the only trouble is: when you click drag the slider and you go out from the track, the slider is still being dragged.
I guess there is a simple trick to prevent that nasty effect
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:R开发者_StackOverflow社区ectangle = new Rectangle(-this.parent.width/2,0,this.parent.width,0);
startDrag(false , rect);
}
private function mouseup(e:MouseEvent):void {
stopDrag();
trace(this.x);
}
}
}
While I'm all for doing stuff and learning along the way, Keith Peters has a great simple library that's pretty easy to setup. (if you're trying to create simple controls)
http://www.bit-101.com/blog/?p=2979
but besides that, you should add a ROLL_OUT event flash.events.MouseEvent.ROLL_OUT
addEventListener(MouseEvent.ROLL_OUT, mouseup);
you want to prevent the slider from moving if you mouse is too far away,
i would do it by adding a event listener for when the mouse moves, if the distance from the slider becomes too much stop the drag early, I'm not sure exactly how your slider works but this may help.
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();
}
}
}
精彩评论