View Full Version : trouble centering loaded swf
Navarone
10-24-2006, 02:12 PM
I am having trouble centering my loaded swf. I have a movieClip on the stage called pieceContainer and I can't seem to get my loaded swf to be equally centered around the x,y of the pieceConatiner. Can someone help?
function newPiece() {
// create a movieclip to hold the functionality of the peice of furniture
// keep all the furniture inside 1 clip (called 'pieceContainer') for organizational purposes
var count:Number = pieceContainer.getNextHighestDepth();
var item_mc:MovieClip = pieceContainer.createEmptyMovieClip("item_mc"+count, count);
// create a container to load the furniture swf into
var container:MovieClip = item_mc.createEmptyMovieClip("container_mc", 0);
container._x = 0;
container._y = 0;
//
// setup a MCL to handle the swf's loading, when its loaded, apply the mdar methods to it.
item_mc.mcl = new MovieClipLoader();
item_mc.mcl.addListener(mclListener_2);
item_mc.mcl.loadClip("decals/"+this.piece+".swf", container);
//
}
//
var mclListener_2:Object = new Object();
mclListener_2.onLoadInit = function(mc:MovieClip) {
//
var pxw = pieceContainer._x
var pxh = pieceContainer._y
trace(pxw)
trace(pxh)
// set variables to remember the original dimensions
var axw = mc._width;
var axh = mc._height;
trace(axw);
trace(axh);
trace(mc._x)
trace(mc._y)
// center piece
mc._y = -axw/2;
mc._x = -axh/2;
mcmcom
10-24-2006, 02:32 PM
did you try?
// center piece
mc._y = (pxw/2) -(axw/2);
mc._x = (phx/2)-(axh/2);
hth,
mcm
Navarone
10-24-2006, 02:56 PM
no I didn't try that. I did make a few changes to make better sense
var mclListener_2:Object = new Object();
mclListener_2.onLoadInit = function(mc:MovieClip) {
//
var pcx= pieceContainer._x;
var pcy = pieceContainer._y;
trace(pcx);
trace(pcy);
// set variables to remember the original dimensions
var axw = mc._width;
var axh = mc._height;
trace(axw);
trace(axh);
trace(mc._x);
trace(mc._y);
// center piece
mc._y = (pcy/2)-(axw/2);
mc._x = (pcx/2)-(axh/2);
trace(mc._x);
trace(mc._y);
now my out put window shows this:
300
200
130
168
0
0
66
35
the pieceContainer is located at 300x200, the incomming swf measures 130x168 and the mc.x and mc.y before loading is 0,0 and after is 66 and 35.
so what you suggested didn't work.
Navarone
10-24-2006, 03:12 PM
I fixed it, I had the axh and axw inverted
// center piece
mc._y = -(axh/2);
mc._x = -(axw/2);
It's working now.:)
thanks for your help.:D
mcmcom
10-24-2006, 03:15 PM
ok so lets just work with width for my understanding (its still early here) ;)
i cant see the WIDTH of the holding container (i am assuming it is pieceContainer) forget x and y about this get the WIDTH and height.
so the AS to make it work should be.
mc._x = (pieceContainer._width/2)-(mc._width/2);
i think in your code you were assigning piceContainers_x and y to variables, but who cares about its x and y just get its width. you may need to ADD the x values to the final result to offset the location of the holding container, but that AS above will center the clip within the container.
hth,
mcm
Kraken
10-24-2006, 03:17 PM
Let me write out what you're trying to do:
When the clip is finally loaded
find the width and height of the loaded clip
set its x position to the center point of the container mc minus half the width of the loaded mc
set its y position to the centerpoint of the container mc minus half the height of the loaded mc
So, all you need to do is this:
mclListener_2.onLoadInit = function(mc:MovieClip) {
//find center point of the container.
var pcx:Number = pieceContainer._width * .5;
var pcy:Number = pieceContainer._height * .5;
mc._x = pcx - (mc._width * .5);
mc._y = pcy - (mc._height * .5);
}
How goes the furniture planner? I still haven't had time to even touch flash in the past week, but I might be able to help this weekend...
mcmcom
10-24-2006, 03:18 PM
ok our ideas are too late ! HAW HAW! Glad its working
mcm
Kraken
10-24-2006, 03:21 PM
haha! Curse my slow fingers... :)
Navarone
10-24-2006, 03:51 PM
I actually started working on a prototype of my own from scratch. This little centering thing was a small pest but with your guy's help I got it working.
I can create the menu from an XML file and load into a scrollPane the menu items. I can click on those items and add them to the floor (sorta) and drag and rotate them anywhere on the stage.
I have several steps to complete. First is to learn how to restrict the furniture items to the floor. Delete items. Figure out how to turn off senoculars freeTransformtool.... It's coming along.:)
Navarone
10-24-2006, 04:30 PM
Hey guys I just noticed something with my count that I didn't expect to work the way that it is.
var count:Number = pieceContainer.getNextHighestDepth();
var item_mc:MovieClip = pieceContainer.createEmptyMovieClip("item_mc"+count, count);
trace(count);
If I click on an piece say sofa, the count starts at zero, if I add a new sofa piece the count goes to 2, then 4, 5, 11, 15
If I start over and add a chair, the count goes 0, 2, 4, 6, 8 10.
Why is this happening?
mcmcom
10-24-2006, 05:33 PM
could it be because count is really
var count:Number = pieceContainer.getNextHighestDepth();
the next highest depth may not ALWAYS be 1 + the current depth, i think its the next highest AVAILABLE depth.
hth,
mcm
Navarone
10-24-2006, 05:35 PM
yeh that might be the reason. I never thought of that!
mcmcom
10-24-2006, 05:44 PM
just instantiate a variable called count. Use that and increment that, you can probably still slap all the clips on those depths without any issues, and this way count will increment by 1 each time, im assuming your using it to determine how many objects are placed on the stage, so separating it from the Depths is logical.
hth,
mcm
Navarone
10-24-2006, 05:46 PM
so instead of
var count:Number = pieceContainer.getNextHighestDepth();
// do something like
var count:Number = 0
mcmcom
10-24-2006, 05:48 PM
ya and you can still assign clips to the getNextHighestDepth when you construct them, you will just have to add a line once the MC is created. something like
//the mc was just created here
count++; // increment count by one.
and if they can remove them make sure to decrease count with count--
hth
mcm
Navarone
10-24-2006, 06:02 PM
ok here is what I have but it doesn't seem to work correctly
function newPiece() {
// create a movieclip to hold the functionality of the peice of furniture
// keep all the furniture inside 1 clip (called 'pieceContainer') for organizational purposes
//var count:Number = pieceContainer.getNextHighestDepth();
var count:Number = 0
var item_mc:MovieClip = pieceContainer.createEmptyMovieClip("item_mc"+count, count);
trace(count);
// create a container to load the furniture swf into
var container:MovieClip = item_mc.createEmptyMovieClip("container_mc", 0);
container._x = 0;
container._y = 0;
//
// setup a MCL to handle the swf's loading, when its loaded, apply the freeTransform methods to it.
item_mc.mcl = new MovieClipLoader();
item_mc.mcl.addListener(mclListener_2);
item_mc.mcl.loadClip("decals/"+this.piece+".swf", container);
count++
//
}
mcmcom
10-24-2006, 06:10 PM
since var count; is created in that function it will DIE with that function
create it on the _root outside of the function like
count:Number = 0;
//my function here
myFunction = function(){
count++;
}
myFunction2 = function(){
count--;
}
both functions should be changing the same variable now. and it will hold scope for the duration of the _root timeline.
hth,
mcm
Navarone
10-24-2006, 06:17 PM
ok, I got you, it is counting correctly but each time I add a new piece of furniture it removes the one already on the stage. They should be at different levels with the count?
var count = 0
function newPiece() {
// create a movieclip to hold the functionality of the peice of furniture
// keep all the furniture inside 1 clip (called 'pieceContainer') for organizational purposes
//var count:Number = pieceContainer.getNextHighestDepth();
var item_mc:MovieClip = pieceContainer.createEmptyMovieClip("item_mc"+count, count);
trace(count);
count++;
// create a container to load the furniture swf into
var container:MovieClip = item_mc.createEmptyMovieClip("container_mc", 0);
container._x = 0;
container._y = 0;
//
// setup a MCL to handle the swf's loading, when its loaded, apply the freeTransform methods to it.
item_mc.mcl = new MovieClipLoader();
item_mc.mcl.addListener(mclListener_2);
item_mc.mcl.loadClip("decals/"+this.piece+".swf", container);
//
}
mcmcom
10-25-2006, 03:24 PM
if its removing them from levels just add them to the nextHighestDepth, who cares if it doesn't match the count. try something like this.
//we are incrementing count by one but adding the clip to the next available depth
var item_mc:MovieClip = pieceContainer.createEmptyMovieClip("item_mc"+count, this.getNextHighestDepth());
trace(count);
count++;
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.