Skip to main content
Participant
July 3, 2009
Question

How to use a symbol instance on the stage in an external package?

  • July 3, 2009
  • 1 reply
  • 504 views

Hi All,

I am new to ActionScript and Flash. I am trying to develop a simple screen with a few symbols (button) and a youtube player(this part has already been taken care of- thanks to Ben Longoria's great class object).

I have a symbol called s1 in the library and I also created an instance of it on the stage on the first frame, its name is myS01

Before I linked the document to a class (this was for youtube player) I was able to add event listeners, such as mousevents, onto this instance in my actions panel. But, after I linked my document to the external class in a package without a name, I cannot add any code into Actions panel, because it gives me all kinds of weird problems.

Instead I am trying to use this symbol and add listeners to it in my external class file (the one linked to the document) with no luck.

my external class file looks like:

package {
    import flash.display.Sprite;
    import flash.display.Stage;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import src.com.enefekt.tubeloc.MovieSprite;
    import src.com.enefekt.tubeloc.event.*;
   
        import fl.controls.Button;
   
    /**
     * Example class for MovieSprite class and the chromed player
     * @7111211 Ben Longoria enefekt@gmail.com
     */
    [SWF(backgroundColor="#FFFFFF")]
    public class MainChromed extends Sprite {
        private var youtubeMovie:MovieSprite;

        public function MainChromed() {
                youtubeMovie = new MovieSprite("NN2I1pWXjXI");
                youtubeMovie.addEventListener(PlayerReadyEvent.PLAYER_READY, onPlayerReady);
                youtubeMovie.x = 250;
                    youtubeMovie.y = 160;
                addChild(youtubeMovie);
        }
       
        private function onPlayerReady(event_p:PlayerReadyEvent):void {
                    youtubeMovie.width = 320;
                    youtubeMovie.height = 240 + MovieSprite.CHROME_HEIGHT;
        }
    }
}

How would I use my myS01 instance of s1 in this .as file? I just want to be able to refer to it.

For example, I tried adding:

private var b:s1 = new s1(); right after the line private var youtubeMovie:MovieSprite;

,but got the following error:

1120: Access of undefined property b.

Any suggestions would be very helpful.

Thank you for your time in advance,

Pr@grammer

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
July 3, 2009

use myS01 in your document class.

and if you want to add code to your swf's timeline its document class must extend the MovieClip class, not the Sprite class.

Pr5Author
Participant
July 3, 2009

Kglad,

Thank your very much for your response. I finally figured it out. As someone who is not very good at OO Programming, I noticed that I was trying to reference to myS01 instance outside of the public function of the document, which is the start point.

So this is the final code, in case anybody else needs it:

package {
    import flash.display.Sprite;
    import flash.display.Stage;
    import src.com.enefekt.tubeloc.MovieSprite;
    import src.com.enefekt.tubeloc.event.*;
   
        import flash.events.MouseEvent;
        import fl.controls.Button;
   
    /**
     * Example class for MovieSprite class and the chromed player
     * @author Ben Longoria enefekt@gmail.com
     */
    [SWF(backgroundColor="#FFFFFF")]
    public class MainChromed extends Sprite {
        private var youtubeMovie:MovieSprite;

        public function MainChromed() {
                youtubeMovie = new MovieSprite("NN2I1pWXjXI");
             youtubeMovie.addEventListener(PlayerReadyEvent.PLAYER_READY, onPlayerReady);
                youtubeMovie.x = 250;
                youtubeMovie.y = 160;
            addChild(youtubeMovie);
           
                //Add mouse event listeners for the instance of the symbol
                    myS01.addEventListener(MouseEvent.MOUSE_OVER, toggleMovie);
            myS01.addEventListener(MouseEvent.MOUSE_OUT, toggleMovie);

        }
       
                // this is called by a mouse event
        private function toggleMovie(e:MouseEvent):void
        {
            trace("Gotcha!");
        }


        private function onPlayerReady(event_p:PlayerReadyEvent):void {
                        youtubeMovie.width = 320;
                        youtubeMovie.height = 240 + MovieSprite.CHROME_HEIGHT;
        }
    }
}

kglad
Community Expert
Community Expert
July 3, 2009

you're welcome.