2011-12-27 90 views
0

默認爲(0,0)當執行代碼:精靈(X,Y)在AS3

var cont:Sprite = new Sprite(); 
var a:Vector.<int > = Vector.<int > ([1,2]); 
var b:Vector.<Number > = Vector.<Number > ([0,0,40,40]); 
cont.graphics.lineStyle(5, 0x442299); 
cont.graphics.drawPath(a, b); 
addChild(cont); 
cont.x = 100; 
cont.y = 100; 
trace("X coordinate of purple line: ", cont.x); 

我得到的輸出 「紫色線的X座標:100」

然而,當我測試此代碼並用鼠標繪製從(100,100)中的線(140,140):

var line:Sprite = new Sprite(); 

stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler); 
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler); 

var startX:int = -1; 
var startY:int = -1; 

function mouseDownHandler(event:MouseEvent):void 
{ 
    startX = mouseX; 
    startY = mouseY; 
} 

function mouseUpHandler(event:MouseEvent):void 
{ 
    swype(Vector.<int> ([1,2]), Vector.<Number> ([startX,startY,mouseX,mouseY])); 
} 

function swype(commands:Vector.<int>, coords:Vector.<Number>):void 
{ 
    var container:Sprite = new Sprite(); 
    container.graphics.lineStyle(5, 0x0066CC); 
    container.graphics.drawPath(commands, coords); 
    addChild(container); 
    container.x = 100; 
    container.y = 100; 
    trace("X coordinate of blue line: ", container.x); 
} 

我得到的輸出: 「藍線的X座標:0」

爲什麼當我從屏幕上的鼠標位置獲取座標並將它們添加到矢量中時,Sprite容器的x和y座標默認爲0,0?

回答

1

不知道你想要做什麼,但你可能需要,如果你繼續走動是_container到使用localToGlobal方法;)

這與你在哪裏得到的mouseX的財產的情況下做和mouseY。 你必須更多地關注顯示堆棧,以及它如何與mouseX和Y的屬性會給你帶來什麼關係。

有一個這樣的嘗試,並射擊我任何問題,如果你不明白爲什麼它的作品。

package 
{ 

import flash.display.Sprite; 
import flash.display.StageQuality; 
import flash.display.StageScaleMode; 
import flash.events.MouseEvent; 

public class Testing extends Sprite 
{ 
    private var startX:Number; 
    private var startY:Number; 
    private var _container:Sprite = new Sprite(); 

    public function Testing() 
    { 
     stage.quality = StageQuality.BEST; 
     stage.align = "TL"; 
     stage.scaleMode = StageScaleMode.NO_SCALE; 

//  if you comment this it will stuff up the first swype 
     _container.x = _container.y = 100; 

     var cont:Sprite = new Sprite(); 
     var a:Vector.<int> = Vector.<int> ([1, 2]); 
     var b:Vector.<Number> = Vector.<Number> ([0, 0, 40, 40]); 
     cont.graphics.lineStyle (5 , 0x442299); 
     cont.graphics.drawPath (a , b); 
     addChild (cont); 
     cont.x = 100; 
     cont.y = 100; 

     trace ("X coordinate of purple line: " , cont.x); 
     var line:Sprite = new Sprite(); 

     addChild (_container); 
     stage.addEventListener (MouseEvent.MOUSE_DOWN , mouseDownHandler); 

     stage.addEventListener (MouseEvent.MOUSE_UP , mouseUpHandler); 
     var startX:int = - 1; 

     var startY:int = - 1; 
    } 

    function mouseDownHandler (event:MouseEvent):void 
    { 
     startX = _container.mouseX; 
     startY = _container.mouseY; 
    } 

    function mouseUpHandler (event:MouseEvent):void 
    { 
     swype (Vector.<int> ([1, 2]) , Vector.<Number> ([startX, startY, _container.mouseX, _container.mouseY])); 
    } 


    function swype (commands:Vector.<int> , coords:Vector.<Number>):void 
    { 

     _container.graphics.lineStyle (5 , 0x0066CC); 
     _container.graphics.drawPath (commands , coords); 
     //this moves it after the property was collected for the initial swype which you don't want 
     _container.x = 100; 
     _container.y = 100; 

     trace ("X coordinate of blue line: " , _container.x); 
    } 

} 

}