您正在尝试在您的 MovieClip 上创建一个函数原型。
要将此函数添加到 MovieClip,您可以扩展 MovieClip 类:
package
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
public class Example extends MovieClip
{
public function Example()
{
super();
}
public function centerContent(refItem:DisplayObject, xOffset:int, yOffset:int):void
{
this.x = (refItem.width - this.width) / 2 + refItem.x + xOffset;
this.y = (refItem.height - this.height) / 2 + refItem.y + yOffset;
}
}
}
然后你可以调用你的centerContent
函数:
var a_mc:Example = new Example();
a_mc.centerContent(obj, 0, 0);
否则,您可以将 any 传递DisplayObject
给函数,如下所示:
public function centerContent(object:DisplayObject, refItem:DisplayObject, xOffset:int, yOffset:int):void
{
object.x = (refItem.width - object.width) / 2 + refItem.x + xOffset;
object.y = (refItem.height - object.height) / 2 + refItem.y + yOffset;
}
然后你可以调用:
centerContent(a_mc, obj, 0, 0);