在AS3中Sprite(x,y)默认为(0,0)

时间:2011-12-27 03:34:49

标签: actionscript-3 sprite shape

执行代码时:

    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 个答案:

答案 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 );
    }

}

}