PDA

View Full Version : Dynamically loading movies with buttons


Ranjit
06-13-2005, 08:26 PM
Hello Group,

I have been grappling with the following problem. I'll highly appreciate any help on this.

I have a movie clip, which is essentially a button component. and then i have a parent movie clip, which will contain these button movieclips.

this is the code am using to dynamically load the button movie clips on the parent movie clip. The problems is that am unable to set the labels of the buttons after dynamically loading them.

---------
qs_mc is the parent movie clip (place holder)
btn_movie is the movie clip, with a button component inside of it
ans_btn is the button component within btn_movie

for(i=1; i<=3; i++)
{
var handle:MovieClip = qs_mc.attachMovie("btn_movie", "ans"+ i + "_mc", i);
handle._x = 0;
handle._y = 35 * i;

handle.ans_btn.onPress = function()
{
trace("Clicked");
}
handle.ans_btn.label = "Button " + i;
}
---------
The statement where i set the label of the button does not seem to work..and it shows the default label "Button" on all the buttons

Kindly provide some suggestions.

Thanks in advance,
Regards
Ranjit

ManinBlaq
06-13-2005, 08:44 PM
You create the handle variable within your 'for' loop ..(I assume this is taking place within a function ..) and it holds the reference to your new movie button.

Then within your 'for' loop .. you are effectively creating / initializing another function and assigning it to the onPress handler .. no problems there.

~But~ the problem is, within the onPress function you're declaring -- you're using your 'handle' variable .. which you have to keep in mind -- when this function gets initialized, and when it actually gets called are two separate events in time.

Basically, by the time you want to set the label of your button .. it would have no concept of what the value of 'handler' is.. or who it's supposed to be pointing too. (Go ahead and put 'trace (handler); within your onPress function. When your button get's clicked, it'll show 'handler' as undefined).

You'll need to refer to your button through a more literal path ..such as:
_root.qs_mc."ans" + i.label = "Button " + i;


The other thing to keep in mind is .. in your code:
var handle:MovieClip = qs_mc.attachMovie("btn_movie", "ans"+ i + "_mc", i);
btn_movie is the name of your 'resource' within your library. "ans"+i ..is this objects 'instance name' which is what matters in actionscript.

Ranjit
06-13-2005, 09:24 PM
Hey Friend,

Thanks for your suggestion.. As you rightly pointed out, there seems to be some scoping problem here..

I made some modifications to my code..but still no luck :(

--------------------------------------------------

for(i=1; i<=3; i++)
{
var handle:MovieClip = qs_mc.attachMovie("btn_movie", "ans"+ i + "_mc", i);
handle._x = 0;
handle._y = 35 * i;

handle.ans_btn.onPress = function()
{
trace("Clicked " + this);
this.label = " New Label";
}

trace(qs_mc["ans"+i+"_mc"].ans_btn);
_root.qs_mc["ans"+i+"_mc"].ans_btn.label = "New Name";
}
-------------

Even the direct path, as you suggested does not change the label..

HOWEVER..what is surprising is, that when i click on any of the buttons, control is passed on to its respective event handler, the "handler.ans_btn.onPress" function, and the label gets changed, when i do it from within the handler. This is a little confusing.

Regards,
Ranjit

ManinBlaq
06-13-2005, 09:50 PM
1: ---------------------------------------------------

Ok ..we need to get these two things to match ... in terms of how they are organized.

qs_mc is the parent movie clip (place holder)
btn_movie is the movie clip, with a button component inside of it
ans_btn is the button component within btn_movie

var handle:MovieClip = qs_mc.attachMovie("btn_movie", "ans"+ i + "_mc", i);
trace(qs_mc["ans"+i+"_mc"].ans_btn);
_root.qs_mc["ans"+i+"_mc"].ans_btn.label = "New Name";


What it sounds like it is now .. is that you haven't declared an 'instance name' for your ans_btn - that is contained within the btn_movie.

Edit the btn_movie symbol -- and select the ans_btn. Look at it's properties, it should have an 'instance name' .. The instance is what you use to refer to objects in actionscript. So .. is 'ans_btn' the instance name of your button, or is it the symbol name in your library?

Right now I'm inclined to think that it's the name of your button resource, and that currently there is no instance name assigned, which would effectively set 'ans_btn' to undefined.

After your third line .. just before your function declaration .. do a trace (handler.ans_btn); Most likely it will be undefined.

Ranjit
06-14-2005, 12:20 AM
Hey There,

Yes, i have made sure that the instance name of the button embedded inside of the movie, is indeed "ans_btn". After dyanamically loading in the movie, when i do a trace on the button inside of the new movie, i get the correct instance name. But when i do this..

trace(qs_mc["ans"+ i + "_mc"].ans_btn);, all is fine.. I am also able to trace the other properties of the button such as _x, _y, etc.

but when i

trace(qs_mc["ans"+ i + "_mc"].ans_btn.label) .. i get an undefined on the label...and thats why the label is not getting set.. is there any other field for setting the label? i looked into the properties of the button component, and it looks like it is "label".

wonder why the label is not accessible..any clues?

Thanks again,
Ranjit

ManinBlaq
06-14-2005, 12:34 AM
You might be having the same problem I was having with Radio buttons.

Basically, I was trying to set the radio button properties ..before it was fully loaded.

Everything appeared correctly ..but trying to access it's properties such as 'selected' always resulted in undefined.

Ultimately I had to move my code that sets the button values, to it's 'onLoad' function.

Afterwards everything worked fine. It seems that several of the components are like that.

Personally, I always tend to make my own buttons from movie clips and setting the 'onPress' function.