# Attaching Actions To Dynamically Generated MCs

**URL:** <https://forum.kirupa.com/t/attaching-actions-to-dynamically-generated-mcs/19297>\
**Category:** flash\
**Created:** [April 26, 2003, 6:12am UTC](https://forum.kirupa.com/t/attaching-actions-to-dynamically-generated-mcs/19297 "2003-04-26T06:12:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamapathy](https://avatars.discourse-cdn.com/v4/letter/i/ac8455/32.png) [@iamapathy](https://forum.kirupa.com/u/iamapathy)\
**Post date:** [April 26, 2003, 6:12am UTC](https://forum.kirupa.com/t/attaching-actions-to-dynamically-generated-mcs/19297/1 "2003-04-26T06:12:23Z")

</div>

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 26, 2003, 6:22am UTC](https://forum.kirupa.com/t/attaching-actions-to-dynamically-generated-mcs/19297/2 "2003-04-26T06:22:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 26, 2003, 6:44am UTC](https://forum.kirupa.com/t/attaching-actions-to-dynamically-generated-mcs/19297/3 "2003-04-26T06:44:07Z")

</div>

Thanks, but I didnt work.

The code I now have is…

```auto
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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 26, 2003, 6:51am UTC](https://forum.kirupa.com/t/attaching-actions-to-dynamically-generated-mcs/19297/4 "2003-04-26T06:51:17Z")

</div>

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:

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

```

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 26, 2003, 2:01pm UTC](https://forum.kirupa.com/t/attaching-actions-to-dynamically-generated-mcs/19297/5 "2003-04-26T14:01:24Z")

</div>

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…

```auto
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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 26, 2003, 6:13pm UTC](https://forum.kirupa.com/t/attaching-actions-to-dynamically-generated-mcs/19297/6 "2003-04-26T18:13:24Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 27, 2003, 1:32am UTC](https://forum.kirupa.com/t/attaching-actions-to-dynamically-generated-mcs/19297/7 "2003-04-27T01:32:01Z")

</div>

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…

```auto
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. 😛

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 🙂
