Attaching Actions To Dynamically Generated MCs

Currently I have a Flash menu that load a bunch of numerically named jpgs in my menu. Using this code…

i=1
currentX = 585
currentY = 88
while( i < 10){
	_root.createEmptyMovieClip(i,i+10)
	with (eval('_root.'+i)){
		_x=currentX
		_y=currentY
		_xscale = 100
		_yscale = 100
		loadMovie(_root.section+'/thumb.'+i+'.jpg', this);
	}
	i++
	if (currentX<680){
		currentX+=32
	}else {
		currentX=585
		currentY+=32
	}
}

Of course the menu, must do something, so I am wondering how i can attach an action, so that when these MCs are clicked they will call a function.

Can anyone tell me how to do this?

you can use the onRelease event handler. After you load the movie assign an onRelease event to it :slight_smile:

Thanks, but I didnt work.

The code I now have is…

i=1
currentX = 585
currentY = 88
while( i < 10){
	_root.createEmptyMovieClip(i,i+10)
	with (eval('_root.'+i)){
		_x=currentX
		_y=currentY
		_xscale = 100
		_yscale = 100
		loadMovie(_root.section+'/thumb.'+i+'.jpg', this);
		this.onRelease = function() {
			loadMovie(_root.section+'.'+i+'.jpg', Pic);
			// check to see if the button is in fact working
			_x=100
		}
	}
	i++
	if (currentX<680){
		currentX+=32
	}else {
		currentX=585
		currentY+=32
	}
}

Im sure ive done something wrong, because I dont normally work with dynamically created MCs.

And, cant we only use button event handlers on buttons?

first, i’d suggest you use _root* instead of eval(’_root.’+i) as eval() is deprecated in MX.

I don’t really the _root.section+’.’+i+’.jpg’. Is that the actual URL to the file? :q:

You can assign onRelease actions to movieclips, simply by going like:

myMovieClip.onRelease = function() {
//code goes here
}

[size=1]hint: look up MovieClip.onRelease in your AS dictionary![/size]

Ok, well it’s still not working, but I checked the AS dictionary, and what I’m doing should work… but doesnt.

I now have the following code…

i=1
currentX = 585
currentY = 88
while( i < 10){
	_root.createEmptyMovieClip(i,i+10)
	with (_root*){
		_x=currentX
		_y=currentY
		_xscale = 100
		_yscale = 100
		loadMovie(_root.section+'/thumb.'+i+'.jpg', this);
		onRelease = function() {
			loadMovie(_root.section+'.'+i+'.jpg', _root.Pic);
			// check to see if the button is in fact working
			trace("Work?")
		}
	}
	i++
	if (currentX<680){
		currentX+=32
	}else {
		currentX=585
		currentY+=32
	}
}

By the way _root.section+’.’+i+’.jpg’ is indeed the link to the url. Is that a problem?

I think _root.section+’/thumb.’+i+’.jpg’ is causing it to not function. I still don’t get it, do you have a folder names ‘_root.section’ and in it are the ‘thumbi.jpg’ files :q:

I’ve got it now. The problem was you can’t assign actions to a MC, that is a JPG. The second the JPG is loaded into MC the onRelease handle won’t work. So i created a new symbol inside it, and had that one take the JPG, while it’s parent took the onRelease actions. Here’s my finished code…

section = "18";
 function loadDay() {
	linearnotes_18._alpha=linearnotes_19._alpha=0
	eval('linearnotes_'+section)._alpha=75
	currentX = 595;
	currentY = 88;
	for (i=1; i<40; i++) {
		_root.createEmptyMovieClip('pic'+i, i+10);
		with (eval('pic'+i)) {
			eval('pic'+i).createEmptyMovieClip('thumb', i+10+100);
			with (eval('pic'+i+'.thumb')) {
				_x = currentX;
				_y = currentY;
				_xscale = 100;
				_yscale = 100;
				if ((_root.section == '18' and i<=32) or (_root.section == '19' and i<=25)) {
					loadMovie(_root.section+'/thumb'+i+'.jpg');
				}
			}
			eval('pic'+i).onRelease = function() {
				_root.Picture.loadMovie(_root.section+'/'+_name+'.jpg');
				_root.shotinfo = eval('_root.'+_root.section+_name);
			};
		}
		if (currentX<670) {
			currentX += 32;
		} else {
			currentX = 595;
			currentY += 32;
		}
	}
}
loadDay();


Kinda hard to figure it out, because I didn’t add my comments yet. :stuck_out_tongue:

By the way, my images saved like this ‘18/thumb1.jpg’, where 18 was the current section, and 1 was the picture number. Sorry that was kinda confusing. But either way thanks for you help! With out your help I wouldn’t have learned about the myMovieClip.onRelease handler.

Thanks :slight_smile: