View Full Version : Spacing out dynamic links horizontally
JLern
01-12-2005, 12:37 AM
I'm trying to set up a horizontal navigation with attachMovie and store the link names in an array. Is there a mathematical way to have them spaced out evenly since I don't know the width of each word ahead of time?
I've done this with a vertical nav where I just figure out the space I want between each line and multiply it by i, etc. Just wondering if theres a way to do it horizontally. Thanks.
Laguana
01-12-2005, 12:45 AM
Well, if you've put in the previous buttons ( 1 through x-1 when you're doing button x) then you could simply take button x-1._x + buttonx-1._width + spacing and there's your next spot.
tdoublea
01-12-2005, 02:14 AM
to back up laguana's suggestion, you might also want to check out textWidth instead of _width. _width will return the width you set the textfield to unless you autosize. textWidth will return the sum of the width of each character, as long as you specified the width and height of the textfield when you created it. that sounds jumble-mouth.... here's some code:
//no dimensions given, using autosize and _width
var nsArray:Array = new Array("one", "two");
for(var i = 0; i < nsArray.length; i++){
tb = createEmptyMovieClip("tb"+i, i);
tb.createTextField("ns_txt", 0, 0, 0, 0, 0);
tb.ns_txt.text = nsArray[i];
tb.ns_txt.autoSize = true;
tb._x = 10 + _root["tb"+ (i - 1)].ns_txt._width;
}
//dimensions given, no autosize and useing textWidth
var nsArray:Array = new Array("one", "two");
for(var i = 0; i < nsArray.length; i++){
tb = createEmptyMovieClip("tb"+i, i);
tb.createTextField("ns_txt", 0, 0, 0, 100, 22);
tb.ns_txt.text = nsArray[i];
var preTbWidth = _root["tb"+ (i - 1)].ns_txt.textWidth;
tb._x = 10 + preTbWidth;
}
hope it helps
-t
JLern
01-12-2005, 02:36 AM
Thanks. ya that was the problem I came across - getting the actual width of the textfield. So is there no way to autosize a dynamic textfield that is on the stage? Would you have to use createTextField for this?
tdoublea
01-12-2005, 01:39 PM
JLern,
no, it's possible. you might be getting hung up on where to access things from after attachMovie (judging from your first post). if you are , here's some code:
//my attachMovies, the linkage name in the library
//** inside these movieclips is a textfield with this instance name: the_txt
var dyArray = new Array("one_mc", "two_mc");
//my text to fill
var textArray = new Array("hello", "world");
//the x spacing
var space = 10;
for(var i = 0; i < dyArray.length; i++){
//use element of array as the link and new name
var mcName = dyArray[i].toString();
var mymc = attachMovie(mcName, mcName, i);
//previous attached movie
var lastmc = dyArray[i-1].toString();
//set text
mymc.the_txt.text = textArray[i];
//resize
mymc.the_txt.autoSize = true;
//
if(i > 0){
mymc._x = space + _root[lastmc]._x + _root[lastmc]._width;
}
}
hope it helps,
-t
JLern
01-12-2005, 03:58 PM
Thanks tdoublea that worked nicely. Here is what I was finally trying to achieve for anyone interested. (Setting the navigation to evenly space the links within a specified area)
//names of each link
linkNames = ["News", "About Us", "Information", "Media", "Downloads", "Contact Us", "Credits"];
//create instance names for each
linkSet = new Array();
for (i=0; i<linkNames.length; i++) {
linkSet[i] = ["link"+i];
}
//place text in each clip
for (i=0; i<linkNames.length; i++) {
mcName = linkSet[i].toString();
mymc = attachMovie("linkClip", mcName, i);
mymc.the_txt.text = linkNames[i];
mymc.the_txt.autoSize = true;
}
//total space for navigation
totalSpace = 500;
//calculate the amount of space taken up by all links
usedSpace = 0;
for (i=0; i<linkNames.length; i++) {
usedSpace += this["link"+i]._width;
}
//figure out even spacing accordingly
space = (totalSpace-usedSpace)/linkNames.length;
//set the x position of each link
for (i=0; i<linkNames.length; i++) {
this["link"+i]._x = space+this["link"+(i-1)]._x+this["link"+(i-1)]._width;
//center group of links within the space
this["link"+i]._x += -(space/linkNames.length);
}
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.