0
votes

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

1 Answers

1
votes

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