2 Replies Latest reply on Dec 17, 2007 8:00 AM by ben_marker

    Newbie imported class & addChild question

    ben_marker
      I have a newbie question regarding addChild that I can't figure out. I'm new to Strict OOP and even newer to AS3.0. I can't figure out why the following code does not add the Sprite to the stage. I know its being instantiated because the mp3 plays. This class is being imported inside of a component if it matters. Why doesn't "playpauseBtn" show up in the component but the mp3 plays???

      import flash.display.*;
      import flash.media.*;
      import flash.net.*;
      import flash.events.*;

      public class PlayPause extends Sprite{

      private var mSound:Sound;
      private var mChannel:SoundChannel;
      private var playpauseBtn:Sprite;
      private var mPlaying:Boolean = false;
      private var mPosition:int;

      public function PlayPause(){

      var mRequest:URLRequest = new URLRequest("Lesson1/Lesson1.mp3");
      mSound = new Sound();
      mSound.load(mRequest);
      mChannel = mSound.play();
      mPlaying = true;
      playpauseBtn = new Sprite();
      addChild(playpauseBtn);
      playpauseBtn.graphics.beginFill(0x993300);
      playpauseBtn.graphics.drawRoundRect(0,0,30,15,10,1 0);
      playpauseBtn.graphics.endFill();
      playpauseBtn.addEventListener(MouseEvent.MOUSE_UP, onPlayPause);
      }

      public function onPlayPause(e:MouseEvent):void{
      if(mPlaying){
      mPosition = mChannel.position;
      mChannel.stop();
      }
      else{
      mChannel = mSound.play(mPosition);
      }
      mPlaying = !mPlaying;
      }


      }
        • 1. Re: Newbie imported class & addChild question
          peterent Level 2
          You are diving right into the deep end, huh?

          First off, you are really asking Flash questions here, not Flex. I'd recommend using Flex, at least at first, because it will be a lot easier.

          Second, you have to get used to events and the asynchronicity of the Flash Player.

          Third, it looks to me that while your PlayPause class does create a child Sprite which is added to PlayPause, it doesn't appear you have added your PlayPause instance to the stage.

          • 2. Newbie imported class & addChild question
            ben_marker Level 1
            Thanks Peter,
            I know this is "kind-of" an actionscript question, but I"m using Flex for this issue. I actually have the class being imported into a component that is being used in my main.mxml app file. The audio shows up so it is instantiated. Why doesn't playpauseBtn add to the stage inside of the AudioController component? If I"m still just completely hopeless, can you please send me in the direction of a tutorial/article that may help me?

            For this example I used comps as the name space, and the component is on stage correctly.
            "<comps:AudioController/>"

            in AudioController.mxml :
            <?xml version="1.0" encoding="utf-8"?>
            <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="150" height="50">
            <mx:Script>
            <![CDATA[
            var sController:SoundController = new SoundController();
            ]]>
            </mx:Script>
            <mx:VBox>
            <mx:HBox>
            <mx:Button label="replay" width="65"/>
            <mx:Button label="toggle" width="65"/>
            </mx:HBox>
            </mx:VBox>
            </mx:Canvas>

            ================end AudioController.mxml ============


            SoundController.as:

            package comps
            {
            import flash.display.*;
            import flash.media.*;
            import flash.net.*;
            import flash.events.*;

            public class SoundController extends Sprite
            {
            private var mSound:Sound;
            private var mChannel:SoundChannel;
            private var playpauseBtn:Sprite;
            private var mPlaying:Boolean = false;
            private var mPosition:int;
            public function SoundController():void
            {
            var mRequest:URLRequest = new URLRequest("audio/sample_audio.mp3");
            mSound = new Sound();
            mSound.load(mRequest);
            mChannel = mSound.play();
            mPlaying = true;
            playpauseBtn = new Sprite();
            addChild(playpauseBtn);
            playpauseBtn.graphics.beginFill(0x993300);
            playpauseBtn.graphics.drawRoundRect(0,0,30,15,10,10);
            playpauseBtn.graphics.endFill();
            playpauseBtn.addEventListener(MouseEvent.MOUSE_UP, onPlayPause);

            }
            public function onPlayPause(e:MouseEvent):void
            {
            if(mPlaying){
            mPosition = mChannel.position;
            mChannel.stop();
            }
            else{
            mChannel = mSound.play(mPosition);
            }
            mPlaying = !mPlaying;
            }
            }
            }
            =========== end .as file =====================