Skip to main content
Inspiring
December 12, 2008
Question

Aligning an object to the center...

  • December 12, 2008
  • 3 replies
  • 326 views
I'm trying to load an image for displaying in a container which have been loaded from the Library and placed on the stage.

Loading the image for which to use is no problem but algning it to the center of the stage is a problem.

Now, keeping in mind that loaded images usually are placed at the top-left corner of the stage I tried to solve the problem like this...:

a = stage.stageWidth/2 (finding the center of the stage)
b = image.width/2
c = a-b (giving us the result of which to place the image)

image.x = c;

The formulas above make sence... in theory. In real life (at least on my computer) it doesn't work.

Now, there might be some factors to consider when doing simple math like this...:

- Can it be that the container is not the same size as the image loaded?
- Can it be that I have to keep the x and y of the container (although I have aligned it so it's placed at the center of the stage)?

Any advice are helpfull...
Thanks in advance...

AceOfFlash
This topic has been closed for replies.

3 replies

Inspiring
December 12, 2008
That should pretty much do it. So there are three things that jump to mind.

1. You've moved the zero point of the movie clip that contains the container. (of some parent of the parent, etc.)

2. You aren't waiting until the external content is completely loaded. So consequently you aren't getting the proper c.

3. You are loading into a container more than once and you need to set the scaleX and scaleY back to 1 before loading.

Okay I kind of lied a bit. There are more things that come to mind. You say "container" but since this is the AS3 forum you mean "Loader instance" right?

And yes NedWebs is right, some actual specific code would be more helpful.

PS: You can shorten the math by just doing this:

image.x=(stage.stageWidth-image.width)/2


kglad
Community Expert
Community Expert
December 12, 2008
you can't access image.width until loading is complete.
Ned Murphy
Legend
December 12, 2008
Can you show the actual code used relative to loading and placing the container/image. It's not really clear what you are loading, and may be a matter of timing.