Attaching mc To Object (OOP)

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?


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;	
	}
}

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

var oTile1:Tiles = new Tiles();

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

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

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.

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:


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.


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

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.

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

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:


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.

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

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

Here is the solution:


[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.

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.

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:

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

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?

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

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.

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

:wink: