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!
:doughnut:
Printable View
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!
:doughnut:
hmmm y u want to do that?
where u can just drag it out from the library. ~ "scratch my head"
use attachMovie(); to do this.
;)
ATTACHMOVIE!!!!!!!! Thanks whispers! You saved the day!
:doughnut:
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?)
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
}
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...Quote:
Originally Posted by part0
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!!
try this:
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.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
}
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
When u have split you string like above the "|" will not exist
e.g
So just deleteCode:var someString="hello there how | are you man | what are you doing?";
var someArray:Array = someString.split("|");
trace(someArray);
and provied the rest of your code is ok..it shud work!!Code:for(i=0;i<(myArray.length -1);i++) {
myArray[i] = myArray[i].split("|");
}
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
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
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:
here is the textFile I used: (myVars.txt)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");
}
Code:&myvars=help|me|please|
&myLabels=1|2|3|
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:
asumming your text file had the labels set correctly: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");
}
Code:myvars=label1|label2|label3|
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?
I changed some of the VAR names in mine...the var in the textField too..
No difference. I still get nothing.
To be clear, the attachProduct.label is assigned the proper values, I just don't know how to show them on the stage.
o
post the current code you have.
which example are you trying?..the exampel with 1 ARRAY or with TWO ARRAYS??
I made an edit to my original post as well (had the wrong "attachProduct.label =" at first) but I corrected it before you had posted...so not sure if you saw new or old code?
also..post the contents of your .txt file...so i can see how you have it set up.
Here is the text file
myVars=Ceridian_Nonexempt_Employee_QRG|Computer_Se curity_QRG|Construction|betatesting|toocool|right| left|middle of the road.Why is sthat|
here is the code
var myVarText = "C:\\Documents and Settings\\wallen\\Desktop\\Flash buttons\\myVars.txt"
var my_lv = new LoadVars();
var my_X = 100 //horizontal spacing
var my_Y = 40 //vertical spacing(from top)
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);
//set position of button
attachProduct._x += i*my_X; //incremented for each button
attachProduct._y += my_Y; //pixels from top
attachProduct.label = myArray[i];
//if buttons reach a certain point, start a new row.
if(attachProduct._x > 600){
attachProduct._x += -700; //incremented for each button
attachProduct._y += my_Y +10; //pixels from top
attachProduct.label = myArray[i];
}
}
}
}
my_lv.load(myVarText);
I am using the one with 1 ARRAY
can you use {code} & {/code} tags? (but replace the { with brackets [ ] ;)
it makes the code easier to read..
but it looks as if you have added another "if" statement...and you have the attachProduct.label INSIDE that if statement...it should be outside of it.. just by a quick look atthe code in the 'email' I have tested it out yet...
I also have the attachProduct.label outside of the if statement. The code will run the first time then test to see if it is at the edge of the screen. I am able to assign the right value to the attachProduct.label, the problem is nothing shows on the screen. If I debug, I will see the proper values for each button. I am trying to find out how to show the labels on the screen.
Thanks!
Bill
your buttons show up..but they are blank? Are you using your OWN buttons? ones you made?..or the component buttons? If they are your OWN, then they do not HAVE labels.
you will have to create yoru OWN 'buttons' (they will really have to be movieClips) with a dynamic textField inside of it to get the "text" to show up.
www.dmstudios.net/demos/splitArrayHelp.zip
I was using my own buttons. (Did I say I was a newbie?) Well that explains it! I will try it using the componet buttons
Thanks!
0
LOL..its ok. (you learn something new everyday). basically you can create ANYTHING in the library and attach it as you are doing your 'buttons'. So you could create a movieClip that has dynamic textFields that you can populate with data..or blank/empty movieClips that you can populate with images or .swf's..possibilities are endless...
Could you send me that file again in flashMX format? I don't seem to be able to open the FLA.
Thanks
O
unfortunately..I can NOT...I am solely on FLASH 8 now... and can only save to FLASH 8 or MX 2004. I can however post it in MX 2004 format..and anyone with MX 2004 can save to MX. You would probably have to post and ask for someone with MX 2004 to save in MX format fo ryou.
up to you....let me know.
that would work. I think some one here has 2004
thanks!
Thanks! While I am waiting on someone to convert that file for me, can you tell me about this code. I decided to us my own buttons. I created a button with the three states (up,over,down). then on a level above the button but IN the same button symbol I added a layer and put a dynamic textbox on it with an instance name of "label" then I use this code,
<code>
//set position of button
attachProduct._x += i*my_X; //incremented for each button
attachProduct._y += my_Y; //pixels from top
attachProduct.label = myArray[i];
</code>
...from yesterday. everything loads but the text fields are still blank. any ideas?
O
you can NOT target omvieClips or even specific 'states' of a button. what you will need to do is make a movieClip that acts exactly as your button does.
example:
1.) make blank movieCip
2.) frame 1 / layer 1: red square (or image of button, up state of 'button')
3.) frame 1 / layer 2: dynamic textField (INSTANCE NAME not VAR name called label1)
4.) stop action on frame 1
5.) frame 2 / layer 1: blue square (or image for down state of 'button')
6.) frame 2 / layer 2: continue same frame (insert frame) from frame 1/ layer 2.
7.) stop action on frame 2
make a new layer (3) call it actions.
put this code in it:
test it....this should work just like a regular button now... now in the library, set the linkage like you did for your REAL buttons...Code:this.onRollOver = function() {
this.gotoAndStop(2);
}
this.onRollOut = function() {
this.gotoAndStop(1);
}
Too cool! Works like a charm!
Thanks!
what you have just learned is VERY VERY powerful code in my opinion...maybe not too fancy..but VERY practical. the objects you attach, dont have to be simple images or buttons, that can be comeplete movies, templates for a product even..that each attachedClip gets populated by that products image, price, color, size, description, and than placed on the stage.
I like to think of them as a .NET repeater panel equivelent. :)
code for GRID layout/positioning:
actionscript Code:var totalClips:Number = 25;
function createTemplates():Void {
//Define the grid columns
var columns:Number = 5;
//Define the spacing/padding
var hPadding:Number = 5;
var vPadding:Number = 5;
for (i = 0; i < totalClips; i++) {
var newClip:MovieClip = attachMovie("templateClip", "product" + i, i);
//initial positions
newClip._x = 10;
newClip._y = 10;
//grid positioning
newClip._x += (i % columns) * (hPadding + newClip._width);
newClip._y += Math.floor(i / columns) * (hPadding + newClip._width);
}
}
createTemplates();