Actionscript 3 ActionScript 3带矩形区域的startDrag

Actionscript 3 ActionScript 3带矩形区域的startDrag,actionscript-3,flash,Actionscript 3,Flash,我有一个名为“场地平面图”的电影剪辑(这是一个场地平面图的图像),我有放大手势,可以放大,效果很好。现在,我想将MouseEvents放置到位,以便在放大电影剪辑时进行拖动。我似乎在设置矩形区域x和y轴时遇到问题。我要做的是将电影剪辑拖到1080 x 1420区域内,并在放大时显示以下内容 图像的底部位于矩形区域的底部停止拖动 图像的顶部位于矩形区域的顶部停止拖动 图像的左侧位于矩形区域的左侧停止拖动 图像的右侧位于矩形区域的右侧停止拖动 我的代码如下。我的图像大小是1080 x 1420,

我有一个名为“场地平面图”的电影剪辑(这是一个场地平面图的图像),我有放大手势,可以放大,效果很好。现在,我想将MouseEvents放置到位,以便在放大电影剪辑时进行拖动。我似乎在设置矩形区域x和y轴时遇到问题。我要做的是将电影剪辑拖到1080 x 1420区域内,并在放大时显示以下内容

  • 图像的底部位于矩形区域的底部停止拖动
  • 图像的顶部位于矩形区域的顶部停止拖动
  • 图像的左侧位于矩形区域的左侧停止拖动
  • 图像的右侧位于矩形区域的右侧停止拖动
我的代码如下。我的图像大小是1080 x 1420,我的舞台是1080 x 1920

siteplan.addEventListener(MouseEvent.MOUSE_DOWN, dragStart);
siteplan.addEventListener(MouseEvent.MOUSE_UP, dragEnd);

function dragStart(e:MouseEvent):void
{
    var rect:Rectangle = new Rectangle(0 - 1080, 0 - 1920, 1080, 1420);
    siteplan.startDrag(false, rect); 
}

function dragEnd(e:MouseEvent):void
{
    siteplan.stopDrag();
}
我希望这是有意义的,请帮助

我也尝试过:

function dragStart(e:MouseEvent):void
{
    var rect:Rectangle = new Rectangle(0 - (1080 * e.currentTarget.x), 0 - (1920 * e.currentTarget.y), 1080, 1420);
    siteplan.startDrag(false, rect); 
}

您正在查看的事件侦听器将只触发一次,这(虽然是一个良好的开始)实际上不允许拖动。此外,在当前设计中,每次
mouseDown
时都会创建一个新的
Rectangle
对象

问题的关键在于将siteplan夹在屏幕上。我在下面编写了这样一个函数,并演示了您所描述的项目功能。请注意,您可以在一个干净的项目中编译它,并使用滚轮进行缩放,鼠标拖动进行平移,地图将保持在屏幕范围内

import flash.events.Event;
import flash.display.Loader;

var cursor:Object = { // tracks the starting coordinates
    "drag":false // whether we're currently dragging.
}

// Load our test "map"
var map:Loader = new Loader();
map.load(new URLRequest("http://nerdist.com/wp-content/uploads/2015/11/Fallout-4-Map.png"));
addChild(map)

// Event Listeners
map.addEventListener("mouseDown", drag);
map.addEventListener("mouseMove", drag);
map.addEventListener("mouseUp", drag);
map.addEventListener("releaseOutside", drag);
map.addEventListener("mouseWheel", zoom);

function zoom(e:MouseEvent):void {
    // Using the scrollwheel, we'll zoom in (up to 3x) or out (down to stage size)
    var increment:Number = 1.2;
    if (e.delta < 0) { increment = 0.8; }

    map.width = clamp(map.width * increment, this.loaderInfo.width, map.width/map.scaleX * 3);
    map.height = clamp(map.height * increment, this.loaderInfo.height, map.height/map.scaleY * 3);
    map.scaleY = map.scaleX;
    map.x =  - (mouseX/this.loaderInfo.width) * (map.width - this.loaderInfo.width);
    map.y =  - (mouseY/this.loaderInfo.height) * (map.height - this.loaderInfo.height);
}

function drag(e:Event):void {
    switch (e.type) {
        case "mouseDown":
            // Store the starting points for both cursor and map.
            cursor.x = map.x - mouseX;
            cursor.y = map.y - mouseY;
            cursor.drag = true;
            break;
        case "mouseMove":
            // When moving the mouse, if we're technically dragging...
            if (cursor.drag) {
                // Offset the current location by the delta of the cursor and the original position of the map.
                map.x = clamp(mouseX + cursor.x, -map.width + this.loaderInfo.width, 0);
                map.y = clamp(mouseY + cursor.y, -map.height + this.loaderInfo.height, 0);
            }           
            break;
        case "mouseUp":
        case "releaseOutside":
            cursor.drag = false;
            break;
    }
}

function clamp(original:Number, low:Number, high:Number):Number {
    return (original > high) ? high : (original < low) ? low : original;
}
导入flash.events.Event;
导入flash.display.Loader;
var cursor:Object={//跟踪起始坐标
“拖动”:false//当前是否正在拖动。
}
//加载我们的测试“地图”
变量映射:Loader=newloader();
加载(新的URL请求(“http://nerdist.com/wp-content/uploads/2015/11/Fallout-4-Map.png"));
addChild(地图)
//事件侦听器
map.addEventListener(“鼠标向下”,拖动);
addEventListener(“mouseMove”,拖动);
map.addEventListener(“mouseUp”,拖动);
map.addEventListener(“releaseOutside”,拖动);
map.addEventListener(“鼠标滚轮”,缩放);
函数缩放(e:MouseeEvent):无效{
//使用滚轮,我们将放大(最多3倍)或缩小(缩小到舞台大小)
var增量:数值=1.2;
如果(e.delta<0){increment=0.8;}
map.width=climp(map.width*增量,this.loaderInfo.width,map.width/map.scaleX*3);
map.height=夹具(map.height*增量,this.loaderInfo.height,map.height/map.scaleY*3);
map.scaleY=map.scaleX;
map.x=-(mouseX/this.loaderInfo.width)*(map.width-this.loaderInfo.width);
map.y=-(mouseY/this.loaderInfo.height)*(map.height-this.loaderInfo.height);
}
函数拖动(e:事件):无效{
开关(e型){
案例“mouseDown”:
//存储光标和贴图的起点。
cursor.x=map.x-mouseX;
cursor.y=map.y-mouseY;
cursor.drag=true;
打破
案例“mouseMove”:
//移动鼠标时,如果我们严格地拖动。。。
如果(光标拖动){
//按光标的增量和地图的原始位置偏移当前位置。
map.x=钳制(mouseX+cursor.x,-map.width+this.loaderInfo.width,0);
map.y=夹具(mouseY+cursor.y,-map.height+this.loaderInfo.height,0);
}           
打破
案例“mouseUp”:
案例“外部释放”:
cursor.drag=false;
打破
}
}
功能夹(原:编号、低:编号、高:编号):编号{
返回(原始>高)?高:(原始<低)?低:原始;
}

您正在查看的事件侦听器只会触发一次,这(虽然是一个良好的开端)实际上不允许拖动。此外,在当前设计中,每次
mouseDown
时都会创建一个新的
Rectangle
对象

问题的关键在于将siteplan夹在屏幕上。我在下面编写了这样一个函数,并演示了您所描述的项目功能。请注意,您可以在一个干净的项目中编译它,并使用滚轮进行缩放,鼠标拖动进行平移,地图将保持在屏幕范围内

import flash.events.Event;
import flash.display.Loader;

var cursor:Object = { // tracks the starting coordinates
    "drag":false // whether we're currently dragging.
}

// Load our test "map"
var map:Loader = new Loader();
map.load(new URLRequest("http://nerdist.com/wp-content/uploads/2015/11/Fallout-4-Map.png"));
addChild(map)

// Event Listeners
map.addEventListener("mouseDown", drag);
map.addEventListener("mouseMove", drag);
map.addEventListener("mouseUp", drag);
map.addEventListener("releaseOutside", drag);
map.addEventListener("mouseWheel", zoom);

function zoom(e:MouseEvent):void {
    // Using the scrollwheel, we'll zoom in (up to 3x) or out (down to stage size)
    var increment:Number = 1.2;
    if (e.delta < 0) { increment = 0.8; }

    map.width = clamp(map.width * increment, this.loaderInfo.width, map.width/map.scaleX * 3);
    map.height = clamp(map.height * increment, this.loaderInfo.height, map.height/map.scaleY * 3);
    map.scaleY = map.scaleX;
    map.x =  - (mouseX/this.loaderInfo.width) * (map.width - this.loaderInfo.width);
    map.y =  - (mouseY/this.loaderInfo.height) * (map.height - this.loaderInfo.height);
}

function drag(e:Event):void {
    switch (e.type) {
        case "mouseDown":
            // Store the starting points for both cursor and map.
            cursor.x = map.x - mouseX;
            cursor.y = map.y - mouseY;
            cursor.drag = true;
            break;
        case "mouseMove":
            // When moving the mouse, if we're technically dragging...
            if (cursor.drag) {
                // Offset the current location by the delta of the cursor and the original position of the map.
                map.x = clamp(mouseX + cursor.x, -map.width + this.loaderInfo.width, 0);
                map.y = clamp(mouseY + cursor.y, -map.height + this.loaderInfo.height, 0);
            }           
            break;
        case "mouseUp":
        case "releaseOutside":
            cursor.drag = false;
            break;
    }
}

function clamp(original:Number, low:Number, high:Number):Number {
    return (original > high) ? high : (original < low) ? low : original;
}
导入flash.events.Event;
导入flash.display.Loader;
var cursor:Object={//跟踪起始坐标
“拖动”:false//当前是否正在拖动。
}
//加载我们的测试“地图”
变量映射:Loader=newloader();
加载(新的URL请求(“http://nerdist.com/wp-content/uploads/2015/11/Fallout-4-Map.png"));
addChild(地图)
//事件侦听器
map.addEventListener(“鼠标向下”,拖动);
addEventListener(“mouseMove”,拖动);
map.addEventListener(“mouseUp”,拖动);
map.addEventListener(“releaseOutside”,拖动);
map.addEventListener(“鼠标滚轮”,缩放);
函数缩放(e:MouseeEvent):无效{
//使用滚轮,我们将放大(最多3倍)或缩小(缩小到舞台大小)
var增量:数值=1.2;
如果(e.delta<0){increment=0.8;}
map.width=climp(map.width*增量,this.loaderInfo.width,map.width/map.scaleX*3);
map.height=夹具(map.height*增量,this.loaderInfo.height,map.height/map.scaleY*3);
map.scaleY=map.scaleX;
map.x=-(mouseX/this.loaderInfo.width)*(map.width-this.loaderInfo.width);
map.y=-(mouseY/this.loaderInfo.height)*(map.height-this.loaderInfo.height);
}
函数拖动(e:事件):无效{
开关(e型){
案例“mouseDown”:
//存储光标和贴图的起点。
cursor.x=map.x-mouseX;
cursor.y=map.y-mouseY;
cursor.drag=true;
打破
案例“mouseMove”:
//移动鼠标时,如果我们严格地拖动。。。
如果(光标拖动){
//按光标的增量和地图的原始位置偏移当前位置。