# attachMovie and startDrag

**URL:** <https://forum.kirupa.com/t/attachmovie-and-startdrag/261444>\
**Category:** flash\
**Created:** [May 27, 2008, 6:38pm UTC](https://forum.kirupa.com/t/attachmovie-and-startdrag/261444 "2008-05-27T18:38:50Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![daylight](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@daylight](https://forum.kirupa.com/u/daylight)\
**Post date:** [May 27, 2008, 6:38pm UTC](https://forum.kirupa.com/t/attachmovie-and-startdrag/261444/1 "2008-05-27T18:38:50Z")

</div>

Hello all

I am building a simple (ho ho) game. A number of mc’s are added dynamically to the stage using ‘attachMovie’ in a ‘for’ loop. The mc’s appear, in the exact place I want them. But how can I set them up so they can be dragged? I thought setting an event listener would do the trick - but no joy so far.

Thanks in advance

db

---

<div class="post-metadata">

**Author:** ![neilmmm](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@neilmmm](https://forum.kirupa.com/u/neilmmm)\
**Post date:** [May 27, 2008, 8:34pm UTC](https://forum.kirupa.com/t/attachmovie-and-startdrag/261444/2 "2008-05-27T20:34:07Z")

</div>

```auto
var mc:MovieClip=this.attachMovie("my_mc","my_mc",1)
mc.onPress=startDrag;
mc.onRelease=mc.onReleaseOutside=stopDrag;

```

it is as easy as above

my\_mc is the linkage name

note you do not need () on the functions

---

<div class="post-metadata">

**Author:** ![daylight](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@daylight](https://forum.kirupa.com/u/daylight)\
**Post date:** [May 27, 2008, 9:00pm UTC](https://forum.kirupa.com/t/attachmovie-and-startdrag/261444/3 "2008-05-27T21:00:19Z")

</div>

Thanks for that - real progress. However (theres always one, isn’t there?!), once in my ‘for’ loop, only the last attached mc is draggable. BUT I remember reading this afternoon, either here or over at [actionscript.org](http://actionscript.org), how to tackle that. Will get back to this in the morning.

regards

db

---

<div class="post-metadata">

**Author:** ![neilmmm](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@neilmmm](https://forum.kirupa.com/u/neilmmm)\
**Post date:** [May 27, 2008, 11:52pm UTC](https://forum.kirupa.com/t/attachmovie-and-startdrag/261444/4 "2008-05-27T23:52:32Z")

</div>

post your code all should be draggable

---

<div class="post-metadata">

**Author:** ![neilmmm](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@neilmmm](https://forum.kirupa.com/u/neilmmm)\
**Post date:** [May 28, 2008, 12:02am UTC](https://forum.kirupa.com/t/attachmovie-and-startdrag/261444/5 "2008-05-28T00:02:52Z")

</div>

```auto
for (i=0; i<5; i++) {
 var mc:MovieClip = this.attachMovie("my_mc", "my_mc"+i, i);
 mc.onPress = startDrag;
 mc.onRelease = mc.onReleaseOutside=stopDrag;
 mc._x = Math.floor(Math.random()*Stage.width-mc._width);
 mc._y = Math.floor(Math.random()*Stage.height-mc._height);
}

```

see attached fla

---

<div class="post-metadata">

**Author:** ![daylight](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@daylight](https://forum.kirupa.com/u/daylight)\
**Post date:** [May 28, 2008, 7:41am UTC](https://forum.kirupa.com/t/attachmovie-and-startdrag/261444/6 "2008-05-28T07:41:20Z")

</div>

Thanks very very much for that!! Soon as I saw your code, realised I’d put the ‘onPress’ and ‘onRelease’ statements in the wrong place - had it outside of the ‘for {}’ (but still within the function creating all the mc’s).

cheers

db

---

<div class="post-metadata">

**Author:** ![neilmmm](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@neilmmm](https://forum.kirupa.com/u/neilmmm)\
**Post date:** [May 28, 2008, 8:37am UTC](https://forum.kirupa.com/t/attachmovie-and-startdrag/261444/7 "2008-05-28T08:37:09Z")

</div>

yw

---

<div class="post-metadata">

**Author:** ![daylight](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@daylight](https://forum.kirupa.com/u/daylight)\
**Post date:** [May 28, 2008, 3:16pm UTC](https://forum.kirupa.com/t/attachmovie-and-startdrag/261444/8 "2008-05-28T15:16:25Z")

</div>

Very sorry to come back with this - spent all afternoon on it and feeling pretty dense. I’ve no got all my tiles laid out and dragging around the stage. I have another set of tiles, also added dynamically, and I want to perform a hitTest. Only having limited success. The comment in the code explains. Have read the ‘hitTest’ entry in the help, and scope - but not getting it.

Think I’ll go back to Letraset…

heres the relevant code:

```auto
function tilesSetUp() {
	if(cutup.length<=10) {
	wordlength = cutup.length * 64;
	} else {
	wordlength = 640;	
	}
	for(var i = 0;i<cutup.length; i++) {//lays out top tiles 
	buildtop(i);
	}
	for(var bi = 0;bi<original.length; bi++) {//lays out static bottom tiles
		buildbottom(bi);
	}
}
//define draggable top tiles
function buildtop(i:Number):Void {
	tnum = "tile"+i;
	var t_mc:MovieClip = this.attachMovie("Tile",tnum,i+original.length);
	t_mc._x = (i % 10) * 64 + (400 - wordlength/2);
	t_mc._y = Math.floor(i/10) * 64 + 22;   
	t_mc.theText.text = cutup.charAt(i);//populates a text field from array
	t_mc.onPress=startDrag;
	t_mc.onRelease=t_mc.onReleaseOutside=stopDrag;
	//start experiment to detect hit
		t_mc.onRelease = function() {
		this.stopDrag();
		for (var i = 0; i < 11; i++) {
			tileB = "chktile" + i;
			trace("tileB "+tileB);
		if (this.hitTest(tileB)) 
		//this 'if' fails - but replace 'tileB' with any distinct instance name i.e. 'chktile8' and the trace works
		{
			trace("here");
		}
		}
}
}
//define static bottom tiles
function buildbottom(bi:Number):Void {
	btnum = "chktile"+bi;
	var h_mc:MovieClip = this.attachMovie("chk_Tile", btnum, bi);//_root.
	h_mc._x = (bi % chkt_y) * 64 + 10;//_root[btnum]
	h_mc._y = Math.floor(bi/chkt_y) * 64 + 464; //_root[btnum] 
}

```

Thanks in advance

db

---

<div class="post-metadata">

**Author:** ![daylight](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@daylight](https://forum.kirupa.com/u/daylight)\
**Post date:** [May 28, 2008, 3:54pm UTC](https://forum.kirupa.com/t/attachmovie-and-startdrag/261444/9 "2008-05-28T15:54:33Z")

</div>

Hopefully solved. Built a much simpler model in a new .fla, got that working and ported it over.

Heres the fix:

```auto
		for (var i = 0; i < 11; i++) {
			var h_mc = "chktile" + i;
			trace("h_mc "+h_mc);
		if (t_mc.hitTest(_root[h_mc])) {
			trace("here");
		}
		}

```

Next job - make the dragged tile ‘stick’ to the static tile, and return a variable! I could well be back…

cheers

db

---

<div class="post-metadata">

**Author:** ![neilmmm](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@neilmmm](https://forum.kirupa.com/u/neilmmm)\
**Post date:** [May 28, 2008, 3:55pm UTC](https://forum.kirupa.com/t/attachmovie-and-startdrag/261444/10 "2008-05-28T15:55:21Z")

</div>

firstly you have 2 onrelease

```auto

// first onrelase
t_mc.onRelease=t_mc.onReleaseOutside=stopDrag;
	//start experiment to detect hit
//second onRelease
 	t_mc.onRelease = function() {
		this.stopDrag();
		for (var i = 0; i < 11; i++) {
			tileB = "chktile" + i;
			trace("tileB "+tileB);
		if (this.hitTest(tileB)) 
		//this 'if' fails - but replace 'tileB' with any distinct instance name i.e. 'chktile8' and the trace works
		{
			trace("here");
		}
		}
}

```

you also have some sort of scope problem

```auto
tileB = "chktile" + i;

```

needs to be soemthing like

tileB = \_root[“chktile” + i];

hope this helps

---

<div class="post-metadata">

**Author:** ![daylight](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@daylight](https://forum.kirupa.com/u/daylight)\
**Post date:** [May 28, 2008, 6:10pm UTC](https://forum.kirupa.com/t/attachmovie-and-startdrag/261444/11 "2008-05-28T18:10:11Z")

</div>

elo

Yes - fixed the ‘tileB = “chktile” + i;’ line - thats when it started working! And have now got the drag tiles snapping to the static tiles and returning the contents of the text field. Need to sort out having x2 ‘onRelease’ still.

Thanks for all your help.

db

---

<div class="post-metadata">

**Author:** ![neilmmm](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@neilmmm](https://forum.kirupa.com/u/neilmmm)\
**Post date:** [May 28, 2008, 8:40pm UTC](https://forum.kirupa.com/t/attachmovie-and-startdrag/261444/12 "2008-05-28T20:40:48Z")

</div>

yw
