A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: [RESOLVED] add child to stage

  1. #1
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588

    resolved [RESOLVED] add child to stage

    I get the following error when i try to addChild to the stage.....1180: Call to a possibly undefined method addChild.....I'm not within the document class root and I can't google a relevant search result..Please help me learn to add something to the stage from within a class definition...the passit variable refers to the class which puts a camera object on stage....works fine if I call it from the .fla but I want to add it to the stage from within and .AS document. Thanks ahead of time for your help.


    PHP Code:
    package NetConnections.NetStreams{
        
    import flash.net.NetConnection;
        
    import flash.net.NetStream;
        
    import flash.events.NetStatusEvent;
        
    import flash.display.Stage;
        
    import NetConnections.NetConnections;
        
    import NetConnections.NetStreams.CameraExample;
        public class 
    NetStreamex {
             public var 
    stream:NetStream;
             public var 
    test:*;
             public var 
    passit:Object;
             public function 
    NetStreamex(nc):void {
                  
                 
                  
    stream = new NetStream(nc);
                  
    stream.publish("test");
                  
    nc.call("checkBandwidth",null);
                  
    passit = new CameraExample(nc,stream);
                 
                 
    addChild(passit);
                 var 
    _stage:Stage passit.stage;
                 
       
            }
            
        }

    ~calmchess~

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    You need to extend the class to the Sprite or MovieClip class, which have that method.
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Given your other threads, I do not think that you intended NetStreamx to be a displayObject. Only DisplayObjects (specifically subclasses of DisplayObjectContainer) have the addChild method.

    You could make NetStreamx extend Sprite, but then you'd have to put your instance of NetStreamx on the stage in order to see its children.

    It looks more like you just want NetStreamx to set up some relations between other classes. If I don't completely misread you, you just want to get a functional CameraExample out, and then do stuff with it elsewhere. You could write a static method to build a CameraExample.

    Code:
    public static function buildCameraExample(nc:NetConnection):CameraExample {
       var stream:NetStream = new NetStream(nc);
       stream.publish("test");
       nc.call("checkBandwidth",null);
       return new CameraExample(nc,stream);
    }
    Then you can call that method from some other class which is a DisplayObjectContainer and put the returned CameraExample on the stage.

    Again, I really don't have a good idea of the entire architecture of your project. And, no offense intended, I don't think you do either.

  4. #4
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    now i get the notice .....TypeError: Error #1009: Cannot access a property or method of a null object reference.
    ~calmchess~

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Using what code?

    I think it may be most helpful if you describe the general goal that you have, and perhaps one of us can outline an organization that streamlines the process.

  6. #6
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    I need to add my camera object to the stage after netStream fires because it passes its variable to the camera class . I'm going to put my code here in the order in which it flows.

    #1.......net connection fires then calls netstream and passes its netconnection (nc) refrence.
    PHP Code:
    package NetConnections{
        
    import flash.net.NetConnection;
        
    import flash.events.NetStatusEvent;
        
    import flash.events.SecurityErrorEvent;
        
    import flash.events.AsyncErrorEvent;
        
    import flash.events.IOErrorEvent;
        
    import flash.net.ObjectEncoding;
        
    import NetConnections.NetStreams.NetConnectionClient;
        
    import NetConnections.NetStreams.NetStreamex;

        public class 
    NetConnections extends NetConnection {
             public var 
    stream:Object;
            public var 
    nc:Object;
            public function 
    NetConnections():void {

                
    super();
                
    addEventListener(NetStatusEvent.NET_STATUSonConnectionStatus);
                
    addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecurityError);
                
    addEventListener(AsyncErrorEvent.ASYNC_ERRORonAsyncError);
                
    addEventListener(IOErrorEvent.IO_ERRORonIOError);
                
    this.client = new NetConnectionClient();
                
    createNetConnection("rtmp://localhost/test/1");
                
    nc this;
            }
            public function 
    createNetConnection(command:String, ... arguments):void {
                
    arguments.unshift(command);
                
    super.connect.apply(thisarguments);

            }
            protected function 
    onConnectionStatus(event:NetStatusEvent):void {
                
    trace("#onConnectionStatusEvent# "+event.info.code);

                var 
    infoObjectsArray:Array = event.info.code.split(".");

                if (
    infoObjectsArray[1] == "Connect") {
                    switch (
    infoObjectsArray[2]) {
                        case 
    "Success" :
                            
    dispatchEvent(new NetStatusEvent("onConnect"falsefalseevent.info));
                             
    stream = new NetStreamex(nc);
                            
                            break;
                        case 
    "Closed" :
                            break;

                    }
                }
            }
            
            protected function 
    onSecurityError(event:SecurityErrorEvent):void {
                
    trace("#onSecurityError# "+event.text);
            }

            protected function 
    onAsyncError(event:AsyncErrorEvent):void {
                
    trace("#onAsyncError#"+event.error.message);
            }

            protected function 
    onIOError(event:IOErrorEvent):void {
                
    trace("#onIOError#"+event.text);
            }
        }

    #2 netstream fires and puts camera object on stage and passes nc and stream to camera class.
    PHP Code:
    package NetConnections.NetStreams{
        
    import flash.net.NetConnection;
        
    import flash.net.NetStream;
        
    import flash.events.NetStatusEvent;
        
    import flash.display.Stage;
        
    import flash.events.*;
        
    import flash.display.DisplayObject;
        
    import NetConnections.NetConnections;
        
    import NetConnections.NetStreams.CameraExample;
        
    import flash.display.Sprite;
        public class 
    NetStreamex extends Sprite{
             public var 
    stream:NetStream;
             public var 
    test:*;
             
             public function 
    NetStreamex(nc):void {
                  
                 
                  
    stream = new NetStream(nc);
                  
    stream.publish("test");
                  
    nc.call("checkBandwidth",null);
                 var 
    passit:CameraExample = new CameraExample(nc,stream);
                 
                var 
    checkit stage.addChild(passit);  
               
    trace(checkit);
                
                 
       
            }
            
        }

    #3...camera class detects the camera object has been added to the stage via event listener and connects the netstream to the camera...this is where i get the error TypeError: Error #1009: Cannot access a property or method of a null object reference.
    PHP Code:
    package NetConnections.NetStreams{
        
    import flash.display.Sprite;
        
    import flash.display.StageAlign;
        
    import flash.display.StageScaleMode;
        
    import flash.events.*;
        
    import flash.media.Camera;
        
    import flash.media.Video;
        
    import flash.net.NetStream;
        
    import flash.events.NetStatusEvent;
        
        
        public class 
    CameraExample extends Sprite{
            private var 
    video:Video;
            
            
            public function 
    CameraExample(nc,stream) {
               
                
    addEventListener(Event.ADDED_TO_STAGEinit(stream)); 
            }
            
            private function 
    init(stream):Function{
                 return function(
    event:Event):void
        
    {
            
    trace(stream);
         

                
                
                
    stage.scaleMode StageScaleMode.NO_SCALE;
                
    stage.align StageAlign.TOP_LEFT;
                
                 
                var 
    camera:Camera Camera.getCamera();

                if (
    camera != null) {
                    
    camera.addEventListener(ActivityEvent.ACTIVITYactivityHandler);
                    
    video = new Video(camera.width 2camera.height 2);
                    
    video.=150;
                    
    video.=150;



                    
    video.attachCamera(camera);
                    
    //netStream.attachCamera(camera);
                   
                    
    addChild(video);

                } else {
                    
    trace("You need a camera.");
                }
            }
            };
            private function 
    activityHandler(event:ActivityEvent):void {
                
    trace("activityHandler: " event);
            }
            
        }

    ~calmchess~

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It looks like you come from a procedural programming background and are having some difficulty transitioning to object oriented design. I say this because of the use of terms like "fire", and trying to do everything as a result of the constructor functions. Objects/classes do not "fire". They are things, which have properties and methods.

    The 1009 error you are getting is actually not from CameraExample, if I read it right. It should be thrown by this line:
    Code:
                var checkit = stage.addChild(passit);
    because when that line is executed, the NetStreamx is not on the stage. It can't be, because a) that line is in the constructor, and b) it is created by a NetConnections which is not a DisplayObject and has no reference to the stage to attempt to add the NetStreamx to, even if it tried. Which it doesn't.

    Let's start from the top.

    You have a document class or an fla. For this exercise which one you choose is not important. That class, which I will call Main, IS a DisplayObject and is on the stage at runtime. Main needs to a) set up a Netstream b) set up a camera c) get them put together somehow. It can delegate these responsibilities to other classes. Netconnections is a class which automates setting up a NetStream. I do not think you need NetStreamx at all. CameraExample is a class which sets up a Camera, and attaches it to a video and a NetStream.

    So, here's some code sketches. These are rough, and probably contain typos and minor bugs. I will leave out import statements for brevity and because I am lazy.

    Main.as (make this your Document Class)
    PHP Code:
    package {

      public class 
    Main extends Sprite {
         private var 
    stream:NetStream;
         private var 
    camex:CameraExample;
         private var 
    connectionURL:String "rtmp://localhost/test/1";

         public function 
    Main() {
           
    stage.scaleMode StageScaleMode.NO_SCALE;
           
    stage.align StageAlign.TOP_LEFT;

           var 
    nc:NetConnections = new NetConnections();
           
    nc.addEventListener("onConnect"handleConnect);
           
    nc.connect(connectionURL);
         }

         private function 
    handleConnect(event:Event):void{
            
    stream NetConnections(event.currentTarget).stream;
            
    camex = new CameraExample(stream);
            
    addChild(camex);
         }

      }


    NetConnections.as
    PHP Code:
    package NetConnections{
        
    import flash.net.NetConnection;
        
    import flash.events.NetStatusEvent;
        
    import flash.events.SecurityErrorEvent;
        
    import flash.events.AsyncErrorEvent;
        
    import flash.events.IOErrorEvent;
        
    import flash.net.ObjectEncoding;
        
    import NetConnections.NetStreams.NetConnectionClient;
        
    import NetConnections.NetStreams.NetStreamex;

        public class 
    NetConnections extends NetConnection {
            public var 
    stream:NetStream;

            public function 
    NetConnections():void {

                
    super();
                
    addEventListener(NetStatusEvent.NET_STATUSonConnectionStatus);
                
    addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecurityError);
                
    addEventListener(AsyncErrorEvent.ASYNC_ERRORonAsyncError);
                
    addEventListener(IOErrorEvent.IO_ERRORonIOError);
                
    this.client = new NetConnectionClient(); //what is this for?
            
    }
            public function 
    connect(command:String):void{
               
    createNetConnection(command);
            }

            public function 
    createNetConnection(command:String, ... arguments):void {
                
    arguments.unshift(command);
                
    super.connect.apply(thisarguments);

            }
            protected function 
    onConnectionStatus(event:NetStatusEvent):void {
                
    trace("#onConnectionStatusEvent# "+event.info.code);

                var 
    infoObjectsArray:Array = event.info.code.split(".");

                if (
    infoObjectsArray[1] == "Connect") {
                    switch (
    infoObjectsArray[2]) {
                        case 
    "Success" :
                            
    stream = new NetStream(this);
                            
    stream.publish("test");
                            
    call("checkBandwidth",null);

                            
    dispatchEvent(new Event("onConnect"falsefalseevent.info));
                            
                            break;
                        case 
    "Closed" :
                            break;

                    }
                }
            }
            
            protected function 
    onSecurityError(event:SecurityErrorEvent):void {
                
    trace("#onSecurityError# "+event.text);
            }

            protected function 
    onAsyncError(event:AsyncErrorEvent):void {
                
    trace("#onAsyncError#"+event.error.message);
            }

            protected function 
    onIOError(event:IOErrorEvent):void {
                
    trace("#onIOError#"+event.text);
            }
        }

    CameraExample.as (note, I don't think this should go in the NetConnections.NetStreams package, but that's your choice)
    PHP Code:
    package NetConnections.NetStreams{
        
    import flash.display.Sprite;
        
    import flash.display.StageAlign;
        
    import flash.display.StageScaleMode;
        
    import flash.events.*;
        
    import flash.media.Camera;
        
    import flash.media.Video;
        
    import flash.net.NetStream;
        
    import flash.events.NetStatusEvent;
        
        
        public class 
    CameraExample extends Sprite{
            private var 
    video:Video;
            private var 
    stream:NetStream;
            
            public function 
    CameraExample(stream:NetStream) {
                
    this.stream stream;       
                
    addEventListener(Event.ADDED_TO_STAGEinit);
            }
            
            private function 
    init(event:Event):void
            
    {
              
    trace(stream);
                var 
    camera:Camera Camera.getCamera();

                if (
    camera != null) {
                    
    camera.addEventListener(ActivityEvent.ACTIVITYactivityHandler);
                    
    video = new Video(camera.width 2camera.height 2);
                    
    video.=150;
                    
    video.=150;

                    
    video.attachCamera(camera);
                    
    stream.attachCamera(camera);
                   
                    
    addChild(video);

                } else {
                    
    trace("You need a camera.");
                }
            }

            private function 
    activityHandler(event:ActivityEvent):void {
                
    trace("activityHandler: " event);
            }
            
        }


  8. #8
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    after careful modification of your main.as file I was able to get everything to attach properly.........here is the final main.as file.....Thankyou so much for helping me Now i can study this application and hopefully learn how to write AS 3.0 I've noticed its not only how you add and process things on the stage but also when. AS 2.0 I could add somthing to the stage and access and process on it anytime.

    PHP Code:
    package 
          
    import NetConnections.NetConnections;
          
    import NetConnections.NetStreams.CameraExample;
          
    import NetConnections.NetStreams.NetStreamex;
      
    import flash.display.MovieClip;
        
        
    import flash.display.StageAlign;
        
    import flash.display.StageScaleMode;
        
    import flash.net.NetStream;
        
    import flash.events.NetStatusEvent;
      public class 
    main extends MovieClip 
      
         public var 
    stream:*; 
         private var 
    camex:CameraExample
         public var 
    nc1:*;
         public function 
    main() { 
           
    stage.scaleMode StageScaleMode.NO_SCALE
           
    stage.align StageAlign.TOP_LEFT
            
    nc1=new NetConnections();
           
    nc1.createNetConnection("rtmp://localhost/test/1");
            
    nc1.addEventListener("onConnect"handleConnect); 
        
         } 

         private function 
    handleConnect(event:NetStatusEvent):void
         
            
    stream = new NetStreamex(nc1); 
            
    camex = new CameraExample(nc1,stream.stream); 
            
    trace("stream2 =" +stream);
            
    addChild(camex); 
         } 

      } 


    ~calmchess~

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Congrats on getting it working. In the future, you should be very wary of *-typing variables. There are not many circumstances that warrant it. Instead, I'd have declared nc1 as type NetConnections and stream as NetStreamx.

    The reason for this is twofold. First, *-typing is actually slower in some cases, but except in tight loops that really isn't a concern. Second, by using specific types you get the compile-time type checking which prevents an entire class of run-time errors.

    On a less technical note, I'd like to thank you for having a good attitude and the willingness to actually learn rather than just get your immediate questions answered.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center