• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Passing arguments to a custom MC class constructor

Contributor ,
Oct 30, 2011 Oct 30, 2011

Copy link to clipboard

Copied

I'm having trouble with the constructor for a custom Class tied to a library MovieClip.

Say I have a MovieClip in my library named Circle, which is tied to the class com.shapes.Circle . I want the Circle class contstructor to take 2 arguments, xScale and yScale:

public function Circle(xScale:Number, yScale:Number) { }

However, if I try to call that from in code, for example Circle ball = new Circle(3.14,2.0); , I always get an "Incorrect number of arguments. Expected 0" error.

Is it possible to have a custom class tied to a MovieClip that can take arguments, or does Flash not allow this? I'd asked about this before and thought I figured it out, but from looking at it now I apparently hadn't figured it out and had resorted to a sloppy workaround; I'm hoping to fix it now.

TOPICS
ActionScript

Views

1.7K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Oct 30, 2011 Oct 30, 2011

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.

Votes

Translate

Translate
Enthusiast ,
Oct 30, 2011 Oct 30, 2011

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 30, 2011 Oct 30, 2011

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 30, 2011 Oct 30, 2011

Copy link to clipboard

Copied

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();

    }

   

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 30, 2011 Oct 30, 2011

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 30, 2011 Oct 30, 2011

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 30, 2011 Oct 30, 2011

Copy link to clipboard

Copied

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"));

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 30, 2011 Oct 30, 2011

Copy link to clipboard

Copied

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();

    }

   

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 30, 2011 Oct 30, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 30, 2011 Oct 30, 2011

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 30, 2011 Oct 30, 2011

Copy link to clipboard

Copied

Yes.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 30, 2011 Oct 30, 2011

Copy link to clipboard

Copied

Hmm, I probably typed something wrong. Thanks again for all your help.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 30, 2011 Oct 30, 2011

Copy link to clipboard

Copied

LATEST

You're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines