what I'm trying to achieve is making a "centerItem"-method that centers your object on the stage. I would like to add this method in my Misc class file, and use it everywhere in my application. I already tried it like below but this didn't work:
public static function centerItem(item:*):void
{
item.x = (stage.stageWidth - item.width) >> 1;
item.y = (stage.stageHeight - item.height) >> 1;
}
and calling it like this:
Misc.centerItem(myObjectToPlaceOnStage);
But when I try it out, it just gives me an error. I already looked on the web but I didn't find any constructive solutions. I hope someone might find a solution/hack
If your object is already added on the stage, you can access its stage property :
public static function centerItem(item:DisplayObject):void
{
item.x = (item.stage.stageWidth - item.width) >> 1;
item.y = (item.stage.stageHeight - item.height) >> 1;
}
By the way, I've typed your item parameter to DisplayObject
, so you are sure that it has width
, height
and stage
properties.