PDA

View Full Version : [MX2004] Prototype on specific movieclips only



ScriptFlipper
June 24th, 2004, 08:36 PM
Kay, I've searched, and I've skimmed through sen's OOP tutorial, but not found the answer. I'm sure it's here somewhere, I just can't find it...

What I am trying to do is to make a prototype apply to only certain movieclips. For example...

I have one apple movieclip and one orange movieclip. For these I have two prototypes:


MovieClip.prototype.onLoad = function () {
this.fruitType = "Apple";
};
and

MovieClip.prototype.onLoad = function () {
this.fruitType = "Orange";
};

But I don't want the apple prototype to apple to the orange movieclips and the orange prototype shouldn't apply to the apple movieclips (they will overrun eachother).

How can I accomplish this, without adding code to the specific movieclips, and WITHOUT doing like this:

MovieClip.prototype.onLoad = function () {
n = this._name.substr(0, 1);
if (n = "a") this.fruitType = "Apple";
if (n = "o") this.fruitType = "Orange";
};

Thank you in advance!

Adam
June 24th, 2004, 08:44 PM
I'd imagine, and this isn't tested, but if you have a MC named Apple and another named Orange, you could do something like this:

MovieClip.prototype.nameMe = function () {
this.fruitType = this._name
};

Apple.nameMe()
Orange.nameMe()

Sorry if it's off, but it seems like it should work.

Adam

ScriptFlipper
June 24th, 2004, 08:56 PM
I'd imagine, and this isn't tested, but if you have a MC named Apple and another named Orange, you could do something like this:

MovieClip.prototype.nameMe = function () {
this.fruitType = this._name
};

Apple.nameMe()
Orange.nameMe()

Sorry if it's off, but it seems like it should work.

Adam


Thanks for trying, but that doesn't quite do it... Maybe I didn't describe it as detailed as I should have.

The prototypes doesn't look like that, and what they do isn't what I'm trying to achieve, I just took that as a short example.. ;) So here's the long story:

I'm having six different types/categories of movieclips, and I have 16 instances of some of them on the stage. Each type has it's own movement pattern, and depending each movieclip's current position on the stage, the movieclip has 3 or four types of movement. So it's like this:

Type 1
- Movement 1
- Movement 2
- Movement 3
Type 2
- Movement 1
- Movement 2
- Movement 3
and so on...


Now, I could put all of that on one single prototype, but that single prototype would be around 300 lines long and extremely clumsy ;)

So, if there was a way that I could do something like this, it would be great!:

MovieClip.Type1.prototype.onLoad() = ...
MovieClip.Type2.prototype.onLoad() = ...

Adam
June 24th, 2004, 09:00 PM
Sorry, SF, that one's waayy above my head :D hope you get what you're looking for ;)

Adam

ScriptFlipper
June 24th, 2004, 09:02 PM
Sorry, SF, that one's waayy above my head :D hope you get what you're looking for ;)

Adam


haha it's over my head too ;)
but I think it has got something to do with classes... hmmm... I'll keep searching for info :D

thanks anyway ;)

Voetsjoeba
June 25th, 2004, 03:43 AM
I don't think you can do it like that, ScriptFlipper. By setting the onLoad method in the prototype you are setting it for every movieclip. This way you'll have to use an if statement, since _every_ movieclip will now have this onLoad method, no exceptions.

ScriptFlipper
June 25th, 2004, 04:53 AM
Too bad :(
But i'll use for-loops instead then ;)

Thanks for your reaply, voets!

senocular
June 25th, 2004, 07:42 AM
you can do it, you just need classes (or really one class if they are going to behave the same) associated with each movieclip you want that to happen with. This may be more trouble than looping though.

ScriptFlipper
June 25th, 2004, 07:46 AM
Yay! I KNEW IT! Thanks sen! :love:
Is it mentioned in your OOP tutorial?

ScriptFlipper
June 25th, 2004, 07:49 AM
This is it, right?
http://www.kirupa.com/developer/oop/AS1OOPClassesWithMCs1.htm

ScriptFlipper
June 25th, 2004, 07:59 AM
Hmm... I think I'll use loops instead... ;)

senocular
June 25th, 2004, 08:22 AM
This is it, right?
http://www.kirupa.com/developer/oop/AS1OOPClassesWithMCs1.htm

Thats where it starts, yeah ;) The next page discusses Object.registerClass which is really the key component in something like this. It allows you to specify which clips will behave the way they do - which class (with your onLoad event within it) they will make use of. Then again, you have to worry about when this fails:
http://www.kirupa.com/developer/oop/AS1OOPClassesWithMCs4.htm

but if you're always using attachMovie and all your apple symbols and all your orange symbols will always be apples and oranges with the appropriate apple and orange onLoad methods, then its not that hard.

Something like...


Apples = function(){} // apples class
Apples.prototype = new MovieClip(); // make sure its also a MovieClip class (inheritance)
Apples.prototype.onLoad = function () { // onLoad for all apples
this.fruitType = "Apple";
};
Object.registerClass("apples_ID", Apples); // all attached "apples_ID" symbols get the Apples.prototype.onLoad

Oranges = function(){} // oranges class
Oranges.prototype = new MovieClip(); // make sure its also a MovieClip class (inheritance)
Oranges.prototype.onLoad = function () { // onLoad for all oranges
this.fruitType = "Orange";
};
Object.registerClass("oranges_ID", Oranges); // all attached "oranges_ID" symbols get the Oranges.prototype.onLoad

ScriptFlipper
June 25th, 2004, 09:38 AM
Thanks for your reply sen! I'll check that out, and I bet it's what I'm looking for :D