2013-02-03 2 views
0

Flash를 사용하여 RTS 게임을 만들고 기본적인 테스트를 수행하려고합니다. 나는 물체를 끌고 가르치면서 this site을 만난다. 게임의 게임 세계를 클릭하면서 시뮬레이션하는 코드를 수정했습니다. 중앙 원은 카메라의 초점/중심입니다. 직사각형 보드는 게임 세계를 나타냅니다.RTS 게임에서 카메라를 움직이는 것과 같이 Actionscript 3.0으로 끌기

function boardMove를 mouseX 및 mouseY에 따라 클릭하여 이동하려고 변경했습니다. 그러나 클릭 할 때마다 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; 
} 
+0

이되는 startDrag /되면 stopDrag 한 번 봐 - 당신을 위해 대부분의 작업을 수행합니다 http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/ Sprite.html # startDrag() –

답변

1

하나의 옵션은 마우스의 상대적 이동을 결정하고 이에 따라 보드를 이동하는 것입니다. 뭔가 같은 :

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; 
} 
관련 문제