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

How to not use dynamic class ?

Guest
Feb 01, 2011 Feb 01, 2011

Hi all, I have some trouble trying to create real classes instead of using the dynamic property of the MovieClip class. Here is the thing :

1. I created a MovieClip in Flash, named MyClip, compiled as a swf.

2. In an actionscript application, I load the swf and I get an instance of the class MyClip that extends MovieClip :

private function loadComplete(event:Event):void {

     var MyClip:Class = getDefinitionByName("MyClip");

     var myClip:MovieClip = new MyClip();

}

3. Now I want to use the clip, sometimes as an object A that extends MovieClip but with attributeA, sometimes as an object B that extends MovieClip with attributeB. I could add dynamically attributes attributeA and attributeB to my MovieClip since it is a dynamic class. But I would prefer not to use dynamic class for all known reasons (performance, maintenance, ...).I come from Java, that may explain why it's hard for me to use dynamic class

I wanted to create class A and class B that both extend MovieClip, and that show the MyClip image. But I can't downcast myClip to A or B since it is an instance of MyClip (himself extending MovieClip). All I can do is create class A and B that both have an attribute mc:MovieClip, which will be myClip. But then if I want to put this object A to a stage, I will have to do :

stage.addChild(a.mc);

wheareas I would want A to extend Movieclip to be able to add it directly :

stage.addChild(a);

Do you see a way to do that or am I stucked with using dynamical attributes of MovieClip ?

TOPICS
ActionScript
2.1K
Translate
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
LEGEND ,
Feb 01, 2011 Feb 01, 2011

I am not sure what you are trying to accomplish and this is maybe why it feels like there is a flaw in the architecture.

What is the difference between objects A and B in the context of using loaded class? Why can't you just instantiate this class as many times as you need anywhere you want? Even if you need to use objects A and B with instances of the loaded class, why not to create a super class C that A and B extend?

Also (besides your question), I am not sure that adding objects to stage is such a good idea under any circumstances. Using container(s) is a much more controllable way of dealing with custom display list.

Translate
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
Guest
Feb 02, 2011 Feb 02, 2011

I want to set the MovieClips I imported in a container, and I want to add to them different properties.

For example, I add in a container images of dogs and cats. Cats can be clicked and the sound is "mew", Dogs can't be clicked but can be dragged/dropped.

I wanted to have Cat and Dog extending MovieClip so I can set them in a container.

And I wanted to have the loaded MovieClip to be a Cat or a Dog, but it is a MyClip.

Translate
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
Community Expert ,
Feb 02, 2011 Feb 02, 2011

Do you mean you want to have classes Cat and Dog, both extend MyClip, which itself extends MovieClip? If so that's what we do all the time

Translate
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
Guest
Feb 02, 2011 Feb 02, 2011

No, Cat and Dog can't extend MyClip because I load several clips, MyClip1, MyClip2, MyClip3, ...

I want to have all those several images in my container, and they can be either Cat or Dog.

One instance of MyClipx can be clickable (a Cat), and another instance of MyClipx can be draggable (a Dog).

I don't know if what I ask is equals to changing the type of the class (from MyClip1 to Cat for example), and then it's not possible.

Translate
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
Community Expert ,
Feb 02, 2011 Feb 02, 2011
var myClip1:MyClip = new MyClip("cat");
var myClip2:MyClip = new MyClip("dog");

...something like this you are after? If so this is what we do all the time as well

Translate
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
Guest
Feb 02, 2011 Feb 02, 2011

I'm sorry I'm new to Flash : what does it do when you write new MyClip("cat") ?

In adobe documentation, MovieClip does not take any argument in its constructor.

Translate
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
Community Expert ,
Feb 02, 2011 Feb 02, 2011

MovieClip does not take any arguments but your class MyClip can:

package {
    import flash.display.MovieClip;
   
    public class MyClip extends MovieClip {
        public function MyClip(type:String):void {
            switch(type){
                case "cat":
                    trace("meow");
                    break;
               
                case "dog":
                    trace("woof");
                    break;
            }
        }
    }
}
Translate
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
Guest
Feb 02, 2011 Feb 02, 2011

So you say I implement the behaviour of a Cat and Dog in each MyClipx I create, instead of creating 2 classes Cat and Dog ?

But if I have millions classes MyClip : MyClip1, ... MyClip9999999 (because I can have many many images in my application), I will have to enter this code in the definition of each MovieClip ?

Or is it possible to create a class Animal that extends MovieClip, that define the behaviour of Cat and Dog, and when I create my image in Flash, I create it from the Animal class instead of MovieClip ?

Translate
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
Community Expert ,
Feb 02, 2011 Feb 02, 2011

You are saying exactly the same thing as I just described.

Translate
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
Guest
Feb 02, 2011 Feb 02, 2011

But still, when I will see an occurence of Animal in the code, I won't know if it's a Cat or Dog (when human read the code). And Animal will provide all the attributes and methods of Cat even if I want MyClip to be a Dog :

var dog:MyClip = new MyClip("dog");

dog.meow(); //will compile even if I define the meow() to fire Error at runtime when species is "dog"

So how is it different from having MyClipx extending MovieClip, and adding cats and dogs attributes and methods to MovieClip dynamically ?

Translate
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
Community Expert ,
Feb 02, 2011 Feb 02, 2011

That's why I am suggesting to create classes Cat/Dog extend MyClip (or Animal) extends MovieClip, so that you cannot do "new Dog().meow()".

The differences are portability, maintainability, editability, extensibility, modularity...

Translate
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
Guest
Feb 02, 2011 Feb 02, 2011

Ok I thought you said to not create classes Cat and Dog but just Animal.

So in FlashBuilder I create Cat -> Animal -> MovieClip and Dog ->Animal -> MovieClip.

Then in Flash I create an image named MyClip1 that is a sub-class of Animal (because I don't know if it will be used as a Dog or a Cat). And compile it to MyClip1.swf.

When I load MyClip1.swf, I get the class reference :

var classRef:Class = getDefinitionByName("MyClip1");

Now I can't do  : var dog:Dog = new classRef();

or : var dog:Dog = new classRef("dog");

because classRef (that is MyClip1) cannot be cast to Dog. MyClip1 can be cast to Animal or MovieClip, but not to Cat or Dog.

So how can I/is it possible to create an instance of Dog from the classRef ?

Translate
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
Community Expert ,
Feb 02, 2011 Feb 02, 2011

You have a class Dog already, why not just do

var dog:Dog = new Dog();

?

Translate
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
Guest
Feb 02, 2011 Feb 02, 2011

Then where is the link between my instance of Dog, and the image I created in Flash ?

I want to put the Dog instance in a container and then see the image MyClip1 when I run the application.

Translate
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
Community Expert ,
Feb 02, 2011 Feb 02, 2011

Dog can load your image. Or Animal can load your image as Andrei says.

Translate
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
LEGEND ,
Feb 02, 2011 Feb 02, 2011

You cannot subclass anything at runtime - only at authoring.

If you want to reuse externally loaded object at runtime - you can do it by reference. Perhaps, Animal class should accomplish loading/instantiation of external class and, because Dog and Cat subclass Animal - they gain access to the external object's instance.

Translate
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
Guest
Feb 04, 2011 Feb 04, 2011
LATEST

thank you, so what I wanted to do is just impossible. All I can do is having a MyClip referencing Dog or Cat. And finally it seems to me this follows better the MVC pattern : the view MyClip is separated from the model Dog or Cat, and some controller add the MyClips to a container and create the reference to Dog or Cat.

Translate
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