Forum Topic: Oop Relation To Mc?

(73 views • 3 replies)

This topic is 1 page long.

<< < > >>
None

WolfAkela

Reply To Post Reply & Quote

Posted at: 4/26/09 10:58 AM

WolfAkela LIGHT LEVEL 08

Sign-Up: 12/19/05

Posts: 2,327

I kinda don't get the logic here (AS2.0, yeah old'd).

Say I have a class, called "Box". It has a constructor that takes in the width and height, which it stores to its variables "myWidth" and "myHeight". It has a get and set method for each.

And I have a movie clip in the library, with identifier "mcBox".

Can anyone explain to me the logic of attaching the movie clip onto the stage, but in doing so I'm also declaring an instance of "Box", where I define the values in the constructor, so the movie clip will appropriately change its width and height?


None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 4/26/09 11:09 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,386

You can't normally have parameters for the constructor in AS2 because 'attachMovie' is used to create instances of 'mcBox' and thus the Box class.

You can however force an object to be 'assimilated' into a class dynamically.

function force (obj : Object, type : Function, args : Array) : Object
{
	obj.__proto__ = type.prototype;
	type.apply (obj, args);
	return obj;
}

You should know how those hacks work, but anyway:

force (attach ('mcBox'), Box, [200, 200]);

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

WolfAkela

Reply To Post Reply & Quote

Posted at: 4/26/09 11:58 AM

WolfAkela LIGHT LEVEL 08

Sign-Up: 12/19/05

Posts: 2,327

Lost at the prototype line, but I got the apply part. The method also returns an Object, which makes me realize something: which one should I even be using to refer to an instance, the stage name or the class instance name? I couldn't even figure how to use the method. Where is the instance name? Where is the MC's stage name? Which should I be using? It returns an object, but how do I access the Box' methods? I tried something like this, but returns undefined:

var obj:Object = force (attach ('mcBox'), Box, [200, 200]);
trace(obj.getWidth());

The force() method kinda feels more like a workaround; is there a proper way I should be doing it? All this time I treated my movieclips as classes, and the attached MCs as its instances, but something's telling me that's actually a bad way of doing things.


None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 4/26/09 12:39 PM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,386

At 4/26/09 11:58 AM, WolfAkela wrote: Lost at the prototype line, but I got the apply part. The method also returns an Object,

Well it's obviously the same object passed to it. This way you can do the movieclip attaching, forcing and assigning in one line.

which makes me realize something: which one should I even be using to refer to an instance, the stage name or the class instance name? I couldn't even figure how to use the method. Where is the instance name? Where is the MC's stage name? Which should I be using? It returns an object, but how do I access the Box' methods? I tried something like this, but returns undefined:

In case you managed to assume something like 'attach' being a magical builtin function that I use over 'attachMovie' because I'm cool, that's exactly it. Uhuh. Yes.

The force() method kinda feels more like a workaround; is there a proper way I should be doing it?

Yup, it's a straight up hack. There isn't a different way.

All this time I treated my movieclips as classes, and the attached MCs as its instances, but something's telling me that's actually a bad way of doing things.

Treating? That's exactly what they are.

Anyway, you just need to figure out that one out of exactly one things that can be wrong with the code that you pasted, without looking at it, emphasis, and you'll be set.

You need to disassociate your library movieclips with their classes in the linkage because the 'force' function calls the class manually now.

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


All times are Eastern Standard Time (GMT -5) | Current Time: 09:50 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!