Actionscript 3 使用Actionscript 3.0进行拖动,就像RTS游戏中移动相机一样

Actionscript 3 使用Actionscript 3.0进行拖动,就像RTS游戏中移动相机一样,actionscript-3,flash,draggable,Actionscript 3,Flash,Draggable,我正在尝试用Flash构建一个RTS游戏,并做一些基本的测试。我遇到教我拖动物体的人。我修改了代码,模拟在点击游戏时移动游戏世界。中心圆是相机的焦点/中心。矩形板代表游戏世界 我尝试将功能板移动更改为单击并根据mouseX和mouseY移动。但每次我点击鼠标时,鼠标和鼠标就成了电路板的中心,这不是我想要的。我想使它与鼠标位置相对,但我只能使电路板闪烁,或使其左上角移动 如有任何建议,将不胜感激 // Part 1 -- Setting up the objects var board:Spri

我正在尝试用Flash构建一个RTS游戏,并做一些基本的测试。我遇到教我拖动物体的人。我修改了代码,模拟在点击游戏时移动游戏世界。中心圆是相机的焦点/中心。矩形板代表游戏世界

我尝试将功能板移动更改为单击并根据mouseX和mouseY移动。但每次我点击鼠标时,鼠标和鼠标就成了电路板的中心,这不是我想要的。我想使它与鼠标位置相对,但我只能使电路板闪烁,或使其左上角移动

如有任何建议,将不胜感激

// Part 1 -- Setting up the objects

var board:Sprite = new Sprite();
var myPoint:Sprite = new Sprite();
var stageWidth = 550;
var stageHeight = 400;
var boardWidth = 400;
var boardHeight = 300;
var pointWidth = 10;

this.addChild(board);
this.addChild(myPoint);

board.graphics.lineStyle(1,0);
board.graphics.beginFill(0xCCCCCC);
board.graphics.drawRect(0,0,boardWidth,boardHeight);
board.graphics.endFill();
board.x = (stageWidth - boardWidth) / 2;
board.y = (stageHeight - boardHeight) / 2;

myPoint.graphics.lineStyle(1,0);
myPoint.graphics.beginFill(0x0000FF,0.7);
myPoint.graphics.drawCircle(0,0,pointWidth);
myPoint.graphics.endFill();
myPoint.x = (stageWidth - pointWidth) / 2;
myPoint.y = (stageHeight - pointWidth) / 2;


// Part 2 -- Add drag-and-drop functionality - Better Attempt

stage.addEventListener(MouseEvent.MOUSE_DOWN, startMove);

function startMove(evt:MouseEvent):void {
    stage.addEventListener(MouseEvent.MOUSE_MOVE, boardMove);
}

// Revised definition of pointMove in Part II of our script

function boardMove(e:MouseEvent):void {
    board.x = checkEdgeX(board.mouseX);
    board.y = checkEdgeY(board.mouseY);
    e.updateAfterEvent();
}

stage.addEventListener(MouseEvent.MOUSE_UP, stopMove);

function stopMove(e:MouseEvent):void {
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, boardMove);
}


// Part III -- Check for boundaries

function checkEdgeX(inX:Number):Number {
    var x = stageWidth / 2 - boardWidth;
    if (inX < x) {
        return x;
    }

    x = stageWidth / 2;
    if (inX > x) {
        return x;
    }

    return inX;
}

function checkEdgeY(inY:Number):Number {
    var y = stageHeight / 2 - boardHeight;
    if (inY < y) {
        return y;
    }

    y = stageHeight / 2;
    if (inY > y) {
        return y;
    }

    return inY;
}
//第1部分——设置对象
变量板:Sprite=新Sprite();
var myPoint:Sprite=新Sprite();
var阶段宽度=550;
var阶段权重=400;
var boardWidth=400;
var板高=300;
var pointWidth=10;
这是addChild(董事会);
这个.addChild(myPoint);
线路板.图形.线型(1,0);
board.graphics.beginll(0xCCCC);
board.graphics.drawRect(0,0,boardWidth,boardHeight);
board.graphics.endFill();
board.x=(分段宽度-板宽)/2;
board.y=(舞台高度-板高)/2;
myPoint.graphics.lineStyle(1,0);
myPoint.graphics.beginll(0x0000FF,0.7);
myPoint.graphics.drawCircle(0,0,点宽度);
myPoint.graphics.endFill();
myPoint.x=(stageWidth-pointWidth)/2;
myPoint.y=(stageHeight-pointWidth)/2;
//第2部分-添加拖放功能-更好的尝试
stage.addEventListener(MouseEvent.MOUSE_DOWN,startMove);
功能启动移动(evt:MouseeEvent):无效{
stage.addEventListener(MouseEvent.MOUSE_MOVE,boardMove);
}
//修改了脚本第二部分中的pointMove定义
功能板移动(e:MouseeEvent):无效{
board.x=checkEdgeX(board.mouseX);
board.y=checkEdgeY(board.mouseY);
e、 updateAfterEvent();
}
stage.addEventListener(MouseEvent.MOUSE_UP,stopMove);
函数stopMove(e:MouseEvent):void{
stage.removeEventListener(MouseEvent.MOUSE_MOVE,boardMove);
}
//第三部分——检查边界
函数checkedEx(inX:Number):编号{
var x=分段宽度/2-板宽;
if(inXx){
返回x;
}
返回inX;
}
功能检查键(inY:编号):编号{
变量y=舞台高度/2-板高;
if(inYy){
返回y;
}
返回inY;
}

一个选项是确定鼠标的相对移动并相应地移动板;比如:

private Point lastPosition;

function startMove(...) {
    lastPosition = null;
    ...
}

function boardMove(e:MouseEvent):void {
    Point position = new Point(stageX, stageY);
    if (lastPosition != null) {
        Point delta = position.subtract(lastPosition);
        board.x += delta.x; // NOTE: also try -= instead of +=
        board.y += delta.y; // NOTE: also try -= instead of +=
        e.updateAfterEvent();
    }
    lastPosition = position;
}

看看startDrag/stopDrag——它为您完成了大部分工作: