开发者

Help with the coordinate space of a Rectangle

If I have a simple class that has a rectangle:

package 
{

import flash.display.Sprite;
import flash.geom.Rectangle;

public class Spot extends Sprite
{
    private var __rect:Rectangle;

    public function  Spot()
    {
        init();
    }

    private function init():void
    {
        __rect = this.getRect(this);

    }

    public function get rect():Rectangle{
            return __rect;
    }

}
开发者_开发问答
}

And I animate an instance of this class on the stage and try to trace it's coordinates:

package 
{

import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;

public class RectangleTest extends Sprite
{
    public var spot:Spot = new Spot();
    public function  RectangleTest()
    {
        init();
    }

    private function init():void
    {
        addEventListener(Event.ENTER_FRAME, dynamicSpotTrace, false, 0, true);      
    }

    private function dynamicSpotTrace(e:Event):void
    {
        trace(spot.rect.x, spot.rect.y, spot.rect.width, spot.rect.height);
    }

}

}

The output traces:

0 0 65 65

Over and over (since the Spot has a registration point of 0,0)...how can I rewrite this so I can get the new coordinates of the Spot instance at every frame (since the spot is actually moving across the screen??)


The parameter to getRect is the targetCoordinateSpace, that is in which display object's coordinate space the rectangle will be represented. So I guess instead of this you need to pass a reference to the display object on which coordinate system you want to get the rectangle, that is the parent display object of Spot. Passing this means Spot will return rectangle in it's own local coordinate system which always have (0, 0) as the origin. If you want the result in RectangleTest coordinate system then you can pass a reference of that in Spot constructor and use that as a parameter of getRect.

Note: I really have not tested this myself, but this is what I understand after reading the manual.


With taskinoor's answer in mind, you can do it a lot easier.

private function dynamicSpotTrace(e:Event):void
{
    var rect:Rectangle = spot.getRect(stage);
    trace(rect);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜