0
votes

I am trying to build a RTS game with Flash and doing some basic testing. I come across this site teaching me dragging objects. I am modified the code to simulate moving the game world of the game while clicking on it. The center circle is the focus point / center of camera. The rectangle board represents the game world.

I tried to change the function boardMove to click and move according to mouseX and mouseY. But every time I click, the mouseX and mouseY becomes the center of the board, which is not what I wanted. I want to make it relative to the mouse position, but I could only make the board flickering, or moves with its top left corner.

Any suggestions would be appreciated.

// 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
Take a look at startDrag/stopDrag - it does most of the work for you: help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/…user1901867

1 Answers

1
votes

One option is to determine the relative movement of the mouse and move the board accordingly; something like:

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;
}