Groups | Blog | Home
all groups > flash actionscript > june 2004 >

flash actionscript : arrays and attach movie


Paulrus
6/7/2004 9:09:52 PM
Hello everyone -

Thanks to everyone who have helped me thus far. I have another question...

I have the following code onRelease on a MC:

function(){

// this should construct the array
i = select.length;
select=seriesname + this._name;
trace(select+" was clicked");

// here is where the buttons should be created
_root.attachMovie("btn_listbutton", "listbutton", 0);
_root.listbutton.listText.text = seriesname + "/" + this._name;
_root.listbutton._x = 582.8;
_root.listbutton._y += 133.2;
_root.listbutton.onRelease = function(){
trace("this button is named " + this.listText.text);
}
}

Right now I'm simply generating 1 button onRelease - but what I want to do is
generate a vertical list of buttons from the array that's created each time a
button is clicked. I've searched everywhere and I can't quite seem to figure
out how to do this dynamically. The trace(select+" was clicked"); line shows
the array being constructed, but I don't know how to use that to build MCs.

Thanks again to all those who've helped me so far.

Paul

Paulrus
6/8/2004 1:25:44 PM
No ideas?

kglad
6/8/2004 2:12:20 PM
you have some buttons on stage and when they are pressed you want to create
another button? if that newly created button is pressed do you want to create
another button?

in any case, to use that function you should give it a name, attach it to a
frame and call it with each button press event handler. you should also
declare select() to be an array, have a movieclip with linkage id
"btn_listbutton" that contains a textfield with instance name "listText" and
you should give each instance of your attached movies unique names, unique
depths and unique positions. for example:

select=new Array();
ivar=0;
function makeButton(mc){
ivar++;
select[ivar]=seriesname+mc; // seriesname should be defined
trace(select[ivar]+" was clicked");
_root.attachMovie("btn_listbutton", "listbutton"+ivar, ivar);
_root[listbutton+ivar].myVar=ivar; // you're probably going to want this at
some point
_root[listbutton+ivar].listText.text = seriesname+"/"+mc;
_root[listbutton+ivar]._x = 582.8;
_root[listbutton+ivar]._y = 133.2+ivar;
_root[listbutton+ivar]..onRelease = function(){
trace("this button is named " + this.listText.text);
// if you wanted to retrieve ivar at some point use this.myVar
}
}

and attached to your (better be) movieclip buttons:

on(press){
makeButton(this._name); // this._name won't work for a non-movieclip button
}
Paulrus
6/8/2004 2:34:43 PM
Originally posted by: kglad
you have some buttons on stage and when they are pressed you want to create
another button? if that newly created button is pressed do you want to create
another button?

Close. I have some MC buttons on stage and when you click on them I want them
to generate a new set of buttons vertically as a sort-of playlist.

Thank you very much for your help! I'll take a look at what you've written
and see if I can sort it out. It's tough being an animator asked to write
actionscript :-)

Paul
Paulrus
6/8/2004 3:44:41 PM
Hmm it's semi-functional. What I'm getting is the attachmovie MC/button
appearing at 0,0 with the stand-in text in the dynamic text field. Here's all
of my code for this MC:

select=new Array();
ivar=0;
function makeButton(mc){
ivar++;
select[ivar]=seriesname+mc; // seriesname should be defined
trace(select[ivar]+" was clicked from the function");
_root.attachMovie("btn_listbutton", "listbutton"+ivar, ivar);
_root[listbutton+ivar].myVar=ivar; // you're probably going to want this at
some point
_root[listbutton+ivar].listText.text = //seriesname+"/"+mc;
_root[listbutton+ivar]._x = 582.8;
_root[listbutton+ivar]._y = 133.2+ivar;
_root[listbutton+ivar].onRelease = function(){
trace("this button is named " + this.listText.text);
// if you wanted to retrieve ivar at some point use this.myVar
}
}

compactsize.onRollOver = shaftspacing.onRollOver = performance.onRollOver =
comparison.onRollOver =
applications.onRollOver = summary.onRollOver = bearings.onRollOver =
stationaryports.onRollOver =
function(){
this.gotoAndStop("over");
}


compactsize.onRollOut = shaftspacing.onRollOut = performance.onRollOut =
bearings.onRollOut = stationaryports.onRollOut = comparison.onRollOut =
applications.onRollOut =
summary.onRollOut = function(){
this.gotoAndStop("off");
}

compactsize.onRelease = shaftspacing.onRelease = performance.onRelease =
bearings.onRelease =
stationaryports.onRelease = comparison.onRelease = applications.onRelease =
summary.onRelease =
function(){

makeButton(this._name); // this._name won't work for a non-movieclip button
}

stop();
kglad
6/9/2004 7:39:34 AM
this line: _root[listbutton+ivar]._y = 133.2+ivar;

should be: _root[listbutton+ivar]._y = 133.2*ivar;

and why are you commenting out the right side of this assignment operator?:

_root[listbutton+ivar].listText.text = //seriesname+"/"+mc;
jolyon_russ
6/9/2004 11:37:29 AM
Hi,

I have a simular problem to Paulrus and would appreciate some guidance from
kglad.

I'm building a portfolio site for myself, I am trying to re-create a simular
thing to );
//returns big-green.html
};

but this same link applies to all the MC's I've made. The code is in frame 1
and within a for statement the cycles through the imageURLArray.

Thanks in advance.


Jolyon
Paulrus
6/9/2004 2:29:06 PM
Originally posted by: kglad
this line: _root[listbutton+ivar]._y = 133.2+ivar;

should be: _root[listbutton+ivar]._y = 133.2*ivar;

and why are you commenting out the right side of this assignment operator?:

_root[listbutton+ivar].listText.text = //seriesname+"/"+mc;


I was trying to figure out why the listText.text wasn't working. I've made
the changes, but what I'm still getting is my attachmovie MC ending up at 0, 0
with the default text in the button.

If anyone wants to take a look at the FLA I'm working on and make suggestions
- here's the URL:

www.fusion-films.com/projects/New_Interface.fla

the code in question is in Scene 1 > mc_series > mc_section > mc_animation

Thanks

Paul


kglad
6/9/2004 9:48:09 PM
jolyon_russ
6/9/2004 9:59:40 PM
Kglad,

I'd really appreciate it if you could take a look at my
http://www.jolyonruss.co.uk/portfolio3.zip.

I'm trying to get each of my dynamically created MC's to link to a unique URL
held in URLArray.

Thanks very much in advance.


Jolyon
kglad
6/9/2004 11:03:24 PM
the followingstatement can't work because when the movieclip button is released
there's no way for it to figure out what i is:

_root.holderMC["loader"+i].onRelease = function(i) {
trace(URLArray[projectIdArray[ i ]]);
};

to remedy you need to create a presistant variable that retains i's value.
so, within your for-loop add this statement after _root.holderMC["loader"+i]
has been instantiated (created):

_root.holderMC["loader"+i].persistantVar=i;

and then within your onRelease handler use:

_root.holderMC["loader"+i].onRelease = function(i) {
trace(URLArray[projectIdArray[this.persistantVar]]);
};
to remedy
jolyon_russ
6/10/2004 6:36:06 AM
Thanks very much kglad,

I knew it was going to be something as simple as that.

I have another dilema which you might be able to help me with, I don't know if
you noticed but my progressBars stay visible once they've reach 100%.

Is there a simple way to check through them all and if they at 100% either
removeMovieClip them or set their _visible to false?

Thanks again in advance.


Jolyon
Paulrus
6/10/2004 12:50:53 PM
Thanks anyway... as I have said, I'm an animator, not a coder. I try to get
things done the best way I know how.

However, in my defense I would like to note that even Macromedia's authorized
actionscripting for MX 2004 pro course doesn't have you coding everything in
one frame. So even if it may be the absolute best way to do things, it isn't
the way Macromedia is teaching us to do things.

Anyway, if someone else wants to take a crack at it I'd really appreciate it.
I can't believe it's that difficult to parse an array and create a verticle
series of buttons out of it.

Thanks

Paul

kglad
6/10/2004 5:32:04 PM
oh, it's not difficult to use an array to create a vertical column of buttons.
it's difficult to debug your movie. i know you are very familiar with it, but
for someone to find the coding problems they need to check various movieclips
that contain that code. this isn't great, but it's common coding practice.
what makes your movie more difficult to debug is that finding your problematic
code requires checking the layers and keyframes that contains code within each
movieclip.
kglad
6/10/2004 5:41:25 PM
Paulrus
6/10/2004 6:13:22 PM
Ok, rather than struggle with the main movie I'm working on, I'm breaking off
the construction of a vertical menu of MCbuttons using attach movie:

var instancecount = 0;
var myObj = new Object();
myObj._x = 411;
myObj._y = 50;

this.main_button.onRelease = function(){
_global.buttoninstance = "newbutton"+instancecount++;
var tempref = attachMovie("mc_attachmovie", buttoninstance, instancecount,
myObj);
myObj._y=(myObj._y+tempref._height);
}

So on each Release action a newbutton is placed at the bottom of the previous
newbutton. Each one is given an instance name newbutton1, 2, 3, and so on.

My question then is, how do I attach code to these new MCbuttons that were
created by using attachmovie?

Each one gets it's name from the var buttoninstance, but I've tried using
buttoninstance.onRelease.putsomecodeheretodosomeething and it doesn't work, but
doing a trace(buttoninstance) gives me the correct instance name.

Paul

AddThis Social Bookmark Button