Setting anchor point of sprite to middle

↘锁芯ラ 提交于 2019-12-11 07:37:38

问题


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?


回答1:


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.




回答2:


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

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



来源:https://stackoverflow.com/questions/9917950/setting-anchor-point-of-sprite-to-middle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!