-
1. Re: Passing arguments to a custom MC class constructor
Peter Celuch Oct 30, 2011 6:00 PM (in response to xTLS)Items in the library can't have constructor arguments, because user can drag them to the stage and this way has no way of passing arguments. If you wat to initialize instances, define public function init() where you can add as much arguments as you like.
-
2. Re: Passing arguments to a custom MC class constructor
Peter Celuch Oct 30, 2011 6:01 PM (in response to Peter Celuch)Anyways, whey you assign a class to the library item with so called Base class MovieClip (base class is not 100% the same as saying "extends ..."), you can't expect it to fully comply with the inheritance rules as you know them. This linking graphics with the actionscript class is special Flash construct with its special rules, it's not clean OOP inheritance.
-
3. Re: Passing arguments to a custom MC class constructor
Peter Celuch Oct 30, 2011 6:17 PM (in response to Peter Celuch)Another approach as opposed to creating init function is to link symbol with autogenerated class (just assign it a class but do not create *.as file for it) and use it just as graphical view with no functionality (well only MovieClip's functionality).
ViewClip.as
public class ViewClip extends MovieClip { public var view:MovieClip; public function ViewClip(){ this.view = instantiateView(); this.addChild(view); } protected function instantiateView():MovieClip { return new MovieClip(); } }Circle.as
public class Circle extends ViewClip { public function Circle(scaleX:Number, scaleY:Number) { super(); } override protected function instantiateView():MovieClip { return new ClassView(); } } -
4. Re: Passing arguments to a custom MC class constructor
xTLS Oct 30, 2011 6:25 PM (in response to Peter Celuch)Thanks, you explained it perfectly. The ViewClip solution is clever.
Another question, are you not allowed to add static methods to a class that extends MovieClip? I'm trying to get a static method working from a class that does so but when I try to call it I get the "1061: Call to a possibly undefined method through a reference with static type Class" error. Since I'm trying to call a static method, I don't understand why it doesn't work. I think I did it the same as examples I've found online; static methods aren't particularly difficult to set up.
-
5. Re: Passing arguments to a custom MC class constructor
Peter Celuch Oct 30, 2011 6:33 PM (in response to xTLS)Yep, the same reason as the constructor thingy. Defining classes with a Base class in library is simply NOT extending as we know it from OOP. They are "not real classes".
You should try creating such class and then in code create another class by extending the first one. Then you fall into a rabbit hole It's definitely something you dont want to do in a real project.
-
6. Re: Passing arguments to a custom MC class constructor
xTLS Oct 30, 2011 6:37 PM (in response to Peter Celuch)Thank you, that's good to know.
Ahh, one more question. Say the libary MovieClip Circle has a nested child with instance name square1. Is there a way to access square1 from the Circle class? Simply calling square1 or this.square1 doesn't work; neither does var square1:MovieClip=MovieClip(this.getChildByName("square1"));
-
7. Re: Passing arguments to a custom MC class constructor
Peter Celuch Oct 30, 2011 6:44 PM (in response to xTLS)Since you used tie ViewClip, you'll have to get used to one more object in the chain - view. See, the child with a name square1 is not child of a Circle, but its view.
So, the correct way to do it:
public class Circle extends ViewClip { private var square1:MovieClip; public function Circle(scaleX:Number, scaleY:Number) { super(); square1 = view.getChildByName("square1") as MovieClip; } override protected function instantiateView():MovieClip { return new ClassView(); } } -
8. Re: Passing arguments to a custom MC class constructor
Peter Celuch Oct 30, 2011 6:58 PM (in response to Peter Celuch)Don't forget to mark Helpful and Correct answers. Here and in all of your threads as well. It's the right way of thanking people that solve your problems..
-
9. Re: Passing arguments to a custom MC class constructor
xTLS Oct 30, 2011 7:03 PM (in response to Peter Celuch)But for situations where I don't need to pass arguments to the constructor and didn't use the ViewClip, would it be possible to access the square1 directly from the Circle where circle extends MovieClip?
-
10. Re: Passing arguments to a custom MC class constructor
Peter Celuch Oct 30, 2011 7:05 PM (in response to xTLS)Yes.
-
11. Re: Passing arguments to a custom MC class constructor
xTLS Oct 30, 2011 7:24 PM (in response to Peter Celuch)Hmm, I probably typed something wrong. Thanks again for all your help.
-
12. Re: Passing arguments to a custom MC class constructor
Peter Celuch Oct 30, 2011 7:25 PM (in response to xTLS)You're welcome.


