Setting anchor point of sprite to middle

2019-08-03 09:44发布

I am generating sprites programatically like this:

this.food = new Sprite();
this.food.graphics.beginFill(0xFFFFFF);
this.food.graphics.drawRect(0, 0, 10, 10);
this.food.filters = [new GlowFilter(0xFF6699, .80, 5, 5, 2, 2, false, false)];
this.food.graphics.endFill();
this.food.x = this.x;
this.food.y = this.y;
this.stage.addChild(this.food);

And later on I'm doing this:

public function update():void
{
    // Spin the food
    this.food.rotation += 1;
}

I basically want the food sprite to spin slowly about its center, not its x and y value which is the top left corner of the sprite.

How can I make the anchor the center of the sprite?

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-03 10:12

I found another approach using the Matrix Object and the transform method. Maybe it's still helpful.

http://jamesvango.co.uk/blog/?p=136

查看更多
爷的心禁止访问
3楼-- · 2019-08-03 10:18

Use the first two parameters of drawRect() to offset the graphics:

drawRect(-5, -5, 10, 10);

Parameters

x:Number — A number indicating the horizontal position relative to the registration point of the parent display object (in pixels).
y:Number — A number indicating the vertical position relative to the registration point of the parent display object (in pixels).

width:Number — The width of the rectangle (in pixels).
height:Number — The height of the rectangle (in pixels).

The idea is to subtract half of the width and height from the x and y to centre.

查看更多
登录 后发表回答