-
Add a symbol from the library
I have some buttons in my library that I would like to add to the stage using action script. Is this possible?
Flash MX
thanks!
-
hmmm y u want to do that?
where u can just drag it out from the library. ~ "scratch my head"
-
Senior Member
use attachMovie(); to do this.
-
ATTACHMOVIE!!!!!!!! Thanks whispers! You saved the day!
-
One more thing. I am able to attach the button but I have the code running in a loop. I am trying to get the code to attach multiples instances of the same button. It seems to be putting them right on top of each other. I am trying to get them to show next to each other. (Am I making any sense?)
-
Senior Member
perfect sense, I had the same exact question when I first used attachMovie();
here an example:
Code:
for (i=0; i<totalProducts; i++) {
var attachProduct = attachMovie("templateClip","product"+i,i);
//placement
attachProduct._x += i*220; //incremented for each button
attachProduct._y += 40; //static 40 pixels from top
}
-
Here is my code. It still seems to be stacking the buttons.
for(i=0;i<(myArray.length -1);i++) {
myOBJ="button" + i
var attachProduct = attachMovie("my.swf",myOBJ+i,i);
attachProduct._x += i*220; //incremented for each button
attachProduct._y += 40; //static 40 pixels from top
}
-
Flashmatics
Originally Posted by part0
hmmm y u want to do that?
where u can just drag it out from the library. ~ "scratch my head"
As you gain more experience with flash u will realise just how important it is to be able to attach movies to stage from the library at runtime...
e.g you have a "star" symbol and you want to fill the night sky with 1000 stars.. i dont think i would want to have the job of dragging a 1000 stars on stage!!
With attachMovie() and a loop, in a few lines of codes you can put these stars on stage!!
-
Senior Member
try this:
Code:
for(i=0; i<(myArray.length - 1); i++) {
var attachProduct = attachMovie("clipInLibrary","newClipName"+i,i); //my.swf should be replaced with the LINKAGE name of the clip in the library you are trying to attach it to. the second parameter is the name of the NEWLY placed clips instance name the 3rd parameter is the depth of the newly created clip.
attachProduct._x += i*220; //incremented for each button
attachProduct._y += 40; //static 40 pixels from top
}
what is the LINKAGE name of the clip you want to attch from the library...and what is the new Name you want them to be? they will be similar to newName1, newname2, newname3 with the [i] var being added.
-
OK, Now I have a bigger problem. I am attaching my code so you can see the whole thing. Here is what I have.
I have a text file that has a variable in it called "myVars". The info is separated by a "|". I then load the file, split the vars and load them into an array. I then have in my library, a button symbol with a linkage name of "button". I need to have a symbol placed for each value in the array. What I was getting before was just a symbol that I had dragged to the stage by mistake. Now that I have gotten rid of that, when I run my file, I get NOTHING! NADA! I can't figure it out! Help! Here is my code.
var myVarText = "C:\\Documents and Settings\\wallen\\Desktop\\myVars.txt"
var my_lv = new LoadVars();
my_lv.onLoad = function(success){
if (success){
myArray = [];
myArray = my_lv.myVars.split("|");
for(i=0;i<(myArray.length -1);i++) {
myArray[i] = myArray[i].split("|");
}
for(i=0;i<(myArray.length -1);i++) {
var attachProduct = attachMovie("button","button"+i,i);
attachProduct._x += i*220; //incremented for each button
attachProduct._y += 40; //static 40 pixels from top
}
}
my_lv.load(myVarText);
}
Your humble servant
O
-
Flashmatics
When u have split you string like above the "|" will not exist
e.g
Code:
var someString="hello there how | are you man | what are you doing?";
var someArray:Array = someString.split("|");
trace(someArray);
So just delete
Code:
for(i=0;i<(myArray.length -1);i++) {
myArray[i] = myArray[i].split("|");
}
and provied the rest of your code is ok..it shud work!!
Last edited by silentweed; 06-01-2006 at 11:43 AM.
-
Senior Member
hahaha.. (beat again).. yeah do what he said..
also why do you have the -1 in thre for the array.length? Just curious..
-
Tried it. No dice. I have the -1 in the length of the array, because the text file has a trailing "|" that has no data after it. I got this code from the net and that is the way that they had it. I will try removing it and adjusting the code. Here is my code as it currently stands and I am still getting nothing.
var myVarText = "C:\\Documents and Settings\\wallen\\Desktop\\myVars.txt"
var my_lv = new LoadVars();
my_lv.onLoad = function(success){
if (success){
myArray = [];
myArray = my_lv.myVars.split("|");
for(i=0;i<(myArray.length -1);i++) {
var attachProduct = attachMovie("button","button"+i,i);
attachProduct._x += i*220; //incremented for each button
attachProduct._y += 40; //static 40 pixels from top
}
}
my_lv.load(myVarText);
}
-
Here is something I noticed. In the my_lv.onLoad = function(success){, I put a trace("hi") and ran the file. the trace never came up. If I put the trace OUTSIDE of the function it will run. Does this mean that my file is not loading?
-
Got it! Thanks Gents! I had a "}" in the wrong place! Thanks for all the help! Newbies everywhere bow to your superior gifts!
O
-
Senior Member
yeah..need to move your } before the load action..
-
err....Care to help me put a label with each button? I can add one, but when I add the next one, it over writes the previous one.
O
-
Senior Member
hmm...there may be other ways to do this..but basically i just made another flashVar in your textFile....loaded that into Flash...split it up..and asigned those values as labels in the for loop.
(some things have been changed fomr your..so read it carefully) you didn supply a textFile to use...so I made one. he var inside is myvars= the textFile name is myVars.txt
here is the code:
Code:
on (press) {
myVarText = "myVars.txt"
my_lv = new LoadVars();
my_lv.onLoad = function(success){
if (success){
trace("Something: "+my_lv.myvars);
myLabels = [];
myLabels = my_lv.myLabels.split("|");
myArray = [];
myArray = my_lv.myvars.split("|");
for(i=0;i<(myArray.length -1);i++) {
var attachProduct = _root.attachMovie("button","button"+i,i);
attachProduct._x += i*220; //incremented for each button
attachProduct._y += 40; //static 40 pixels from top
attachProduct.label = myLabels[i];
}
}else {
trace("WTF??");
}
}
my_lv.load("myVars.txt");
}
here is the textFile I used: (myVars.txt)
Code:
&myvars=help|me|please|
&myLabels=1|2|3|
-
Senior Member
Im not ure what your using for your INITIAL array for attaching/populating the buttons to stage...but you could use ONE array...just an array of labels...you can use the length of the labelArray to see how many buttons you need to attach...and ten you can use the VALUES at each array INDEX to populate the labels
like this:
Code:
on (press) {
myVarText = "myVars.txt"
my_lv = new LoadVars();
my_lv.onLoad = function(success){
if (success){
myArray = [];
myArray = my_lv.myvars.split("|");
for(i=0;i<(myArray.length -1);i++) {
var attachProduct = _root.attachMovie("button","button"+i,i);
attachProduct._x += i*220; //incremented for each button
attachProduct._y += 40; //static 40 pixels from top
attachProduct.label = myArray[i];
}
}else {
trace("WTF??");
}
}
my_lv.load("myVars.txt");
}
asumming your text file had the labels set correctly:
Code:
myvars=label1|label2|label3|
Last edited by whispers; 06-01-2006 at 02:54 PM.
-
Whispers, I tried yours caused it looked the easiest, but I am getting nothing. do I have to create a dynamic field on the button?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|