0

I'm new to AS3 and seem to be overlooking something related to classes and/or targeting. I have a MovieClip in my Library that is linked to a class called 'cloud' (yes cloud is just a picture of a rain cloud). I want to add cloud to the stage. I understand there are two ways to do this:

  1. Drag it directly across from the Library to the Stage.
  2. Use AS3 to addChild to the Stage.

The class works when I publish my swf using the "drag it directly across from the Library to the Stage" approach. But it doesn't work when I try using the AS3 approach.

The class is as follows:

package  {

    import flash.display.MovieClip;
    import flash.events.KeyboardEvent;

    public class cloud extends MovieClip {

        public function cloud() {
            stage.addEventListener(KeyboardEvent.KEY_DOWN,keyMapping);
        }
        private function keyMapping(event:KeyboardEvent):void {
            if (event.charCode == 13) {
                MovieClip(parent).testme.text = "hello world!";
            }
        }
    }
}

The AS3 I have been using on Frame 1 of my main timeline:

stop();
var cloud_mc:cloud = new cloud();
addChild(cloud_mc);

==================================================================================

SUGGESTED FIX

Class:

package  {

    import flash.display.MovieClip;
    import flash.events.KeyboardEvent;

    public class cloud extends MovieClip {

        public function cloud() {
        }
        public function tralala() {
            stage.addEventListener(KeyboardEvent.KEY_DOWN,keyMapping);
        }
        private function keyMapping(event:KeyboardEvent):void {
            if (event.charCode == 13) {
                MovieClip(parent).testme.text = "hello world!";
            }
        }
    }
}

AS3:

stop();
var cloud_mc:cloud = new cloud();
addChild(cloud_mc);
cloud_mc.tralala();
Actionman
  • 13
  • 5

1 Answers1

1

Use the linkage name of the library object, not the class that it extends.

Your library object extends the cloud class which extends movie clip. If you instanciate the cloud class you miss the library object.

The process is described in detail here:

http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b8ea63-7fee.html

Malte Köhrer
  • 1,577
  • 10
  • 19
  • The link posted gives an detailed explanation. A short version would be: Right click library item, make sure that [x] Export for actionscript is ticked, then copy the value from the "class" field and use it instead of the cloud class. – Malte Köhrer Jul 03 '14 at 12:41
  • Thanks for that! I think I understand now and I've just amended my original post with a suggested fix - is that what we are talking about? The suggested fix does work but I just want to be sure I'm doing this correctly/the proper way. – Actionman Jul 03 '14 at 12:54
  • Looks correct. Just one note: The code you posted has the same name for both class and library linkage. That is allowed but might be leading to confusion when you have multiple library items with different linkage ids that all extend the same class. For example you could have 8 different looking asteroid graphics named "asteroid1" to "asteroid8" that all extend the Metroid class. In that case you need to do "new metroid1()" and not "new Metroid()". – Malte Köhrer Jul 03 '14 at 15:49
  • Ah I see. I want to adopt some good coding practices as early as I can so I will do as you suggested "new cloud1()". Thank you, you've been incredibly helpful! – Actionman Jul 03 '14 at 22:28
  • Glad I could help. And sorry, the example was written in a hurry, so missed something. In AS3 you capitalize class names, meaning the linkage Id should be "Cloud1" and now "cloud1". Also I use only numbers when I really have multiple exchangeable library items that extend the same class. If it's a 1:1 relation I for example use another naming scheme like "CloudLib" or "CloudSprite". That makes it as easy to later figure out that it's not the class that has been instanciated (there's no class with that name after all) but the Library item. – Malte Köhrer Jul 04 '14 at 11:17