Groups | Blog | Home
all groups > flash actionscript > january 2005 >

flash actionscript : Creating Listeners Dynamically?


BNZ
1/22/2005 9:04:24 PM
Hi, I am creating a menu dynamically using Flash Remoting. I have got that
working fine, I just want to add to the loop that generates the list an onClick
event that loads a movieclip dependent on which button they press. Here is the
code as it stands:- This is the function that builds the menu. var
varname:String=''; var ypos:Number=100; var zpos:Number=0; function
GetClientsArray_Result(result:ResultEvent) { // display successful result
for(var item=0;i<result.result.length;item++) {
varname='MenuItem'+item+'_mc'; trace(varname);
this.attachMovie('mcMenuItem', varname, zpos); this[varname]._x=1;
this[varname]._y=ypos; this[varname].MenuList_txt.text=result.result[item];
ypos+=20; zpos+=10; } } mcMenuItem is a movieclip that consists of a
piece of dynamic text called MenuList_txt and an invisible buttton covering the
area it is likely to fill called Invis_btn. I have a movie clip called holder
that I want to load the external movie clip into when one of the menu buttons
is pressed and I am not sure how to add that in the loop that creates the menu.
Any help will be gratefully received. Thanks.
NSurveyor
1/23/2005 1:10:00 AM
var varname:String="";
var ypos:Number=100;
var zpos:Number=0;

function GetClientsArray_Result(result:ResultEvent)
{
// display successful result
for(var item=0;i<result.result.length;item++)
{
varname="MenuItem"+item+"_mc";
trace(varname);
this.attachMovie("mcMenuItem", varname, zpos);
this[varname]._x=1;
this[varname]._y=ypos;
this[varname].MenuList_txt.text=result.result[item];
this[varname].Invis_btn.onPress = function(){
//CODE FOR LOADING SPECIFIED MOVIE!!!!!!!
}
ypos+=20;
zpos+=10;
}
}
BNZ
1/23/2005 11:52:24 AM
That is great thanks. I still have a problem though. The filenames for the
movies I am trying to load are the same as the position in the loop. ie
0.swf, 1.swf, 2.swf, etc.. I tried this to test the theory I had:-
this[varname].Invis_btn.onPress = function() { trace(i + '.swf'); } But
as the function is not called until the button is pressed the value of 'i' is
not read until then so all the buttons have the same result. Is there a way of
forcing it to set the value as the loop executes. Or am I just going about it
in a really retarded fashion? Thanks.
billwatson
1/23/2005 1:47:09 PM
try

this[varname].Invis_btn.onPress = function()
{
trace(this._name);
}
NSurveyor
1/23/2005 6:10:12 PM
Try this:

var varname:String="";
var ypos:Number=100;
var zpos:Number=0;

function GetClientsArray_Result(result:ResultEvent)
{
// display successful result
for(var item=0;item<result.result.length;item++)
{
varname="MenuItem"+item+"_mc";
trace(varname);
this.attachMovie("mcMenuItem", varname, zpos);
this[varname]._x=1;
this[varname]._y=ypos;
this[varname].MenuList_txt.text=result.result[item];
this[varname].cnum = item;
this[varname].Invis_btn.onPress = function(){
trace(''+this.cnum+'.swf');
}
ypos+=20;
zpos+=10;
}
}
BNZ
1/24/2005 9:39:11 PM
Thanks, but trace(this._name); returns Invis_btn which is the buttons instance name.

BNZ
1/24/2005 9:40:33 PM
trace(''+this.cnum+'.swf'); returns Undefined.swf

Any other clever fixes will be gratfully received.

AddThis Social Bookmark Button