flash as3 我想要做上下用鼠标拖动图片,同时把图片限定在某一个范围内。(不是跟随鼠标) 10
1个回答
展开全部
这里最好侦听ENTER_FRAME时间来自己写拖动,使用startDrag();不好控制
元件内容内容如图:
flash共一帧,输入如下代码:
import flash.events.MouseEvent;
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Rectangle;
var dis:Point = new Point();
var rectangle:Rectangle = new Rectangle(mc.x + 1, mc.y + 1, mc.width - 1, mc.height - 1);
rect.addEventListener(MouseEvent.MOUSE_DOWN, mousedownHandler);
function mousedownHandler(evt:MouseEvent):void{
dis.x = mouseX - rect.x;
dis.y = mouseY - rect.y;
rect.removeEventListener(MouseEvent.MOUSE_DOWN, mousedownHandler);
rect.addEventListener(MouseEvent.MOUSE_OUT, outHandler);
rect.addEventListener(MouseEvent.MOUSE_UP, outHandler);
rect.addEventListener(Event.ENTER_FRAME, enterframeHandler);
}
function outHandler(evt:MouseEvent):void{
rect.addEventListener(MouseEvent.MOUSE_DOWN, mousedownHandler);
rect.removeEventListener(MouseEvent.MOUSE_OUT, outHandler);
rect.removeEventListener(MouseEvent.MOUSE_UP, outHandler);
rect.removeEventListener(Event.ENTER_FRAME, enterframeHandler);
}
function enterframeHandler(evt:Event):void{
rect.x = mouseX - dis.x;
rect.y = mouseY - dis.y;
if(rect.x < rectangle.x){
rect.x = rectangle.x;
}
if(rect.y < rectangle.y){
rect.y = rectangle.y;
}
if(rect.x + rect.width > rectangle.x + rectangle.width){
rect.x = rectangle.x + rectangle.width - rect.width;
}
if(rect.y + rect.height > rectangle.y + rectangle.height){
rect.y = rectangle.y + rectangle.height - rect.height;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询