# Attaching mc To Object (OOP)

**URL:** https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714
**Category:** Uncategorized
**Created:** [August 8, 2004, 12:22pm UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714 "2004-08-08T12:22:45Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![anarcoma](https://avatars.discourse-cdn.com/v4/letter/a/cdc98d/32.png) [@anarcoma](https://forum.kirupa.com/u/anarcoma)
#### Post date: [August 8, 2004, 12:22pm UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714/1 "2004-08-08T12:22:45Z")

</div>

Is there any way to attach a movieclip to an object from inside a class??? i want to access the movieClip from within the library using its Linkage-name…

this is the class-code i tried but this obviously doesnt work…i tried passing a movieclip as a parameter in the constructor and it worked but then i have to place the mc on the stage and use that instancename…and i dont want that

anyone?

```auto

class Tiles extends MovieClip {
	
	var iWidth:Number;
	var iHeight:Number;
	var mTile:MovieClip;
		
	function Tiles () {
		mTile = this.attachMovie( "tile", "circle1", 500 );
		iWidth	= mTile._width;
		iHeight	= mTile._height;	
	}
}

```

---

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [August 8, 2004, 12:35pm UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714/2 "2004-08-08T12:35:30Z")

</div>

there shouldnt be a problem. How are you creating your Tiles instance(s)?

---

<div class="post-metadata">

### Author: ![anarcoma](https://avatars.discourse-cdn.com/v4/letter/a/cdc98d/32.png) [@anarcoma](https://forum.kirupa.com/u/anarcoma)
#### Post date: [August 8, 2004, 12:57pm UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714/3 "2004-08-08T12:57:44Z")

</div>

var oTile1:Tiles = new Tiles();

---

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [August 8, 2004, 1:56pm UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714/4 "2004-08-08T13:56:58Z")

</div>

thats not the right way. You have to use attachMovie with a symbol in your library associated with the class for classes that extend MovieClip (otherwise its not a movieclip)

[http://www.kirupa.com/developer/oop2/AS2OOPInheritance4.htm](http://www.kirupa.com/developer/oop2/AS2OOPInheritance4.htm)

---

<div class="post-metadata">

### Author: ![anarcoma](https://avatars.discourse-cdn.com/v4/letter/a/cdc98d/32.png) [@anarcoma](https://forum.kirupa.com/u/anarcoma)
#### Post date: [August 8, 2004, 6:52pm UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714/5 "2004-08-08T18:52:43Z")

</div>

ok let me rephrase…

is there anyway to, from inside a class, attach a movieClip from the library to a movieClip in your class…using the mc’s, in the library, linkagename???

---

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [August 8, 2004, 7:07pm UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714/6 "2004-08-08T19:07:45Z")

</div>

yes

but the original code you posted wasnt wrong - just the way you instantiated the Tiles class was. That is what was your problem.

If you want to use new Classname() then you wouldnt be extending MovieClip and you’d instead have to pass in (or hardcode) a reference to a movieclip within that class so that it could be used to call attachMovie from.

---

<div class="post-metadata">

### Author: ![dustin\_c1](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@dustin\_c1](https://forum.kirupa.com/u/dustin_c1)
#### Post date: [August 9, 2004, 2:25am UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714/7 "2004-08-09T02:25:58Z")

</div>

I have a similar question. In fact, you may be answering my question and I might not be following.

I can get following code will display the image in my flash file:

```auto

class MyClass extends MovieClip () { 
   var sourceURL:String = ......;
   function MyClass() {
	   this.loadMovie(sourceURL);
   } 
}

```

What I want is code that will attach a jpg image in the form of a MovieClip object to a custom class of mine that extends MovieClip. Something like this, but the following code, of course, doesn’t work.

```auto

class MyClass extends MovieClip () { 
   var mc_imageContainer:MovieClip;
   var sourceURL:String;
 
   function MyClass() {
	   mc_imageContainer.loadMovie(sourceURL);
	   //....more stuff
   } 
   //more stuff
}

```

---

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [August 9, 2004, 12:35pm UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714/8 "2004-08-09T12:35:07Z")

</div>

how are you creating an instance of MyClass?

---

<div class="post-metadata">

### Author: ![dustin\_c1](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@dustin\_c1](https://forum.kirupa.com/u/dustin_c1)
#### Post date: [August 9, 2004, 2:02pm UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714/9 "2004-08-09T14:02:59Z")

</div>

> [@senocular](#):
>
> how are you creating an instance of MyClass?

I insert a MovieClip symbol into the library with AS 2.0 Class “MyClass.”  
I then drag&drop it onto the timeline.

---

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [August 9, 2004, 2:50pm UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714/10 "2004-08-09T14:50:55Z")

</div>

that should do it on that part (which is where the previous issue had problems)

the next step is to make sure you have a valid movieclip destination and that the url is correct. The url you can easily double check. The movieclip you would want to trace to make sure its referenced right when you try to perform loadMovie

---

<div class="post-metadata">

### Author: ![dustin\_c1](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@dustin\_c1](https://forum.kirupa.com/u/dustin_c1)
#### Post date: [August 9, 2004, 3:21pm UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714/11 "2004-08-09T15:21:55Z")

</div>

> [@senocular](#):
>
> the next step is to make sure you have a valid movieclip destination and that the url is correct. The url you can easily double check. The movieclip you would want to trace to make sure its referenced right when you try to perform loadMovie

Yes, the URL is correct. The same URL loads the image in the first code snippet (in my first post), but does not in the second.

Here is some behavior I am seeing that I don’t understand:

```auto

class MyClass extends MovieClip () { 
var mc_imageContainer:MovieClip;
var sourceURL:String;
 
function MyClass() {
	trace(this._name); //works - remember I earlier described how I instanciated the instance of MyClass
	trace(mc_imageContainer._name); //does not work, prints "undefined"
	//I expected it to print "mc_imageContainer"
	//so I explicitly call a constructor
	mc_imageContainer = new MovieClip();
	trace(mc_imageContainer._name); //does not work, prints "undefined"
	mc_imageContainer.loadMovie(sourceURL); //does not work
	//note: this.loadMovie(sourceURL) does work as expected
	//....more stuff
} 
//more stuff
}

```

I believe I have an instance of a MovieClip named mc\_imageContainer object as a property in my instance of MyClass. But mc\_imageContainer is not acting like a MovieClip object. However, _this_, the instance of MyClass is behaving like a MovieClip. It should, as it inherits from MovieClip.

---

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [August 9, 2004, 3:27pm UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714/12 "2004-08-09T15:27:25Z")

</div>

if youre attaching it to a movie manually placed in the movieclip, then try calling it from onLoad (create an onLoad function in your class), not the constructor.

[http://www.kirupa.com/developer/oop/AS1OOPClassesWithMCs7.htm](http://www.kirupa.com/developer/oop/AS1OOPClassesWithMCs7.htm)

---

<div class="post-metadata">

### Author: ![dustin\_c1](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@dustin\_c1](https://forum.kirupa.com/u/dustin_c1)
#### Post date: [August 9, 2004, 3:58pm UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714/13 "2004-08-09T15:58:55Z")

</div>

> [@senocular](#):
>
> if youre attaching it to a movie manually placed in the movieclip, then try calling it from onLoad (create an onLoad function in your class), not the constructor.
> 
> [http://www.kirupa.com/developer/oop/AS1OOPClassesWithMCs7.htm](http://www.kirupa.com/developer/oop/AS1OOPClassesWithMCs7.htm)

Using onLoad isn’t going to do anything extra as, in this case, it gets called immediately after the constructor.

Here is the solution:

```auto

[left][/left]
class MyClass extends MovieClip () { 
var mc_imageContainer:MovieClip;
var sourceURL:String;
 
function MyClass() {
	//this.mc_imageContainer.loadMovie(sourceURL); does not work
[left]	loadMovie(sourceURL, "this.mc_imageContainer"); //works[/left]
	//....more stuff
} 
//more stuff
}

```

I still don’t understand why my orginal code did not work.

---

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [August 9, 2004, 4:12pm UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714/14 "2004-08-09T16:12:11Z")

</div>

wait, what was the mc\_imageContainer = new MovieClip();? (I didnt actually read your code before, but that shouldnt be there)

---

<div class="post-metadata">

### Author: ![dustin\_c1](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@dustin\_c1](https://forum.kirupa.com/u/dustin_c1)
#### Post date: [August 9, 2004, 4:21pm UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714/15 "2004-08-09T16:21:14Z")

</div>

> [@senocular](#):
>
> wait, what was the mc\_imageContainer = new MovieClip();? (I didnt actually read your code before, but that shouldnt be there)

That was just something I tried. Remember, mc\_imageContainer wasn’t showing any signs of being an instance of a MovieClip. For example, mc\_imageContainer.\_name was undefined. I figured that perhaps “var mc\_imageContainer:MovieClip;” wasn’t actually calling a constructor, so I inserted “mc\_imageContainer = new MovieClip();” just to be sure a constructor was called.

I don’t know, maybe I’m just too much in a Java mindset here. The Flash actionscript model is just plain confusing.

---

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [August 9, 2004, 4:46pm UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714/16 "2004-08-09T16:46:09Z")

</div>

something else I noticed is the () after extends MovieClip

class MyClass extends MovieClip —\>()\<— {

that shouldnt be their either…

But really, other than that, it should be ok (your movieclip should be accessible from the constructor, the onLoad shouldnt be necessary, as I think you found out but I thought Id see if that helped anyway). I dont know why its not working for you :-/

The new MovieClip() would call the constructor but of a generic (basically abstract) MovieClip class object. This would actually serve the purpose of replacing that variable which would have normally assumed a refernec to the movieclip within the movieclip inherting from this class - something obviously not desirable.

why loadMovie (target, url) works and target.loadMovie(url) doesnt is beyond me (unless maybe somewhere along the line you’ve overwritten loadMovie with your own method - but the problem is just that movieclip reference and why that just isnt happening)

a working class _should_ be:

```auto
class MyClass extends MovieClip { 
   var mc_imageContainer:MovieClip;
   var sourceURL:String = "image_url.jpg";
   function MyClass() {
	   mc_imageContainer.loadMovie(sourceURL);
   } 
}

```

where mc\_imageContainer exists within the movieclip assuming this class instance

---

<div class="post-metadata">

### Author: ![dustin\_c1](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@dustin\_c1](https://forum.kirupa.com/u/dustin_c1)
#### Post date: [August 9, 2004, 5:28pm UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714/17 "2004-08-09T17:28:01Z")

</div>

> class MyClass extends MovieClip —\>()\<— {

typo.

> why loadMovie (target, url) works and target.loadMovie(url) doesnt is beyond me (unless maybe somewhere along the line you’ve overwritten loadMovie with your own method - but the problem is just that movieclip reference and why that just isnt happening)

where mc\_imageContainer exists within the movieclip assuming this class instance

No overriding. Try your example real quick. It just won’t work. It’s a dirty shame. I may to have to go about a very unclean way (i.e. code all in the timeline, no real OO) of doing what I need to do. Loadmovie() isn’t enough. I need to know when the image loads, so I need to use a MovieClipLoader object. But myMovieClipLoader(url, target) work within MyClass, I think for the same reason that mc\_imageContainer.loadMovie(target, url) doesn’t work.

Is there any workaround so that I know when mc\_imageContainer has loaded without using a MovieClipLoader?

---

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [August 9, 2004, 5:47pm UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714/18 "2004-08-09T17:47:45Z")

</div>

ok, I just tried it and it works for me.

---

<div class="post-metadata">

### Author: ![dustin\_c1](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@dustin\_c1](https://forum.kirupa.com/u/dustin_c1)
#### Post date: [August 9, 2004, 6:08pm UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714/19 "2004-08-09T18:08:35Z")

</div>

You did different than I did.

You created a MovieClip symbol (Symbol 1) and named it mc\_imageContainer and placed it inside your timeline for your instance of the MyClass object.

Thanks a million! It would never have occured to me to do that. It just seems strange to have to do that.

---

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [August 9, 2004, 6:10pm UTC](https://forum.kirupa.com/t/attaching-mc-to-object-oop/118714/20 "2004-08-09T18:10:05Z")

</div>

thats why above I specified “where mc\_imageContainer exists within the movieclip assuming this class instance”

😉
