A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: accessing sub function in a class

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

    accessing sub function in a class

    why can i access the first function in a class with
    PHP Code:
    import test.test
    var test:test = new test(); 
    but I cannot access the second or third function in the same manner? they seem to be seperte entities within the class definition.

    can access
    PHP Code:
    public class NetStreamex {
            
            public var 
    connection:NetConnections = new NetConnections();
            public function 
    NetStreamex():void {
            
                
    connection.createNetConnection("rtmp://localhost/test/1");
                
    connection.addEventListener("onConnect",NetStreamxx);
               
            } 
    cannot acesss
    PHP Code:
    public function NetStreamxx(event:NetStatusEvent):void{
                
                var 
    stream = new NetStream(connection);
                
    stream.publish("test");
                
    connection.call("checkBandwidth",null);
                var 
    passStream:CameraExample = new CameraExample();
                var 
    it:String ="passedit";
                 
    //return stream;


            

    ~calmchess~

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    How are you attempting to access it?

    The first example you gave is implicitly using that function because that function is the constructor for that class. It gives you an instance of the class. Once you have an instance of that class, you can call other public functions on that instance.

    The constructor function is a special function with the same name as the class. The constructor is always interpreted rather than run in JIT compiled bytecode, which means it's less efficient than other functions. But the purpose of the constructor is to produce an instance of the class. Other functions in that class should do things with that instance, or if they are static to things which are logically associated with that class as a whole.

    Please post more context so I can tell what you're trying to do.

  3. #3
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    I'm trying to get a variable out of a function and use it......my idea is to use........
    PHP Code:
    test:newtest= new test(); 
    newVar=test.myvar
    ........but i cant use that on the second function in the class.
    Last edited by calmchess; 05-02-2009 at 08:13 PM.
    ~calmchess~

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Post. the. exact. code. you. are. trying. and. failing. with.

    If I had to guess (and I do, since you haven't been telling us), I'd say you're doing something like this:

    Code:
    var test:Netstreamxx = new Netstreamxx();
    You can't do that because Netstreamxx is not a class, it is a function within the Netstreamx class. To use that function, you'd do something like this:

    Code:
    var test:Netstreamx = new Netstreamx();
    test.Netstreamxx(event);
    But of course, that's not what you actually want either. The Netstreamxx function is an event listener which is called as a consequence of the constructor. Calling it from outside the class would be nonsense. And to do that nonsense you'd need a NetStatusEvent object which you don't have.

  5. #5
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    I have netStatus event object on the NetConnection Object....do i need one for both netConnection and NetStream?
    ~calmchess~

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I can't even understand what you think you mean by that. You need to back way up and read about object oriented programming.

    Unless I'm way off and someone else can help you out.

    You still haven't posted the code which is failing.

  7. #7
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    I'm trying to get my NetStream attached to my camera...here is the failing code.

    I have a essential actionscript 3.0 book It just doesn't answer my questions so i ask here after these questions I'll stop asking because these are way too basic of questions for the forum.

    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 NetConnections.NetStreams.NetStreamex;
        public class 
    CameraExample extends Sprite 
            private var 
    video:Video
            var 
    test:NetStreamex = new NetStreamex();
            
    // test.NetStreamxx(event);
             
            
    public function CameraExample() { 
            
            
              
    addEventListener(Event.ADDED_TO_STAGEinit); 
            } 

            private function 
    init(event:Event):void
                
    stage.scaleMode StageScaleMode.NO_SCALE
                
    stage.align StageAlign.TOP_LEFT

                var 
    camera:Camera Camera.getCamera(); 

                if (
    camera != null) {
                    
    //var getStream:NetStreamex = new NetStreamex();
                    //var streamit = getStream.stream;
                  
                   
    camera.addEventListener(ActivityEvent.ACTIVITYactivityHandler); 
                    
    video = new Video(camera.width 2camera.height 2); 
                    
    video.attachCamera(camera); 
                    
                    
    addChild(video); 
                } else { 
                    
    trace("You need a camera."); 
                } 
            } 
            private function 
    activityHandler(event:ActivityEvent):void 
                
    trace("activityHandler: " event); 
            } 
        } 

    ~calmchess~

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I haven't played with Netstreams, but I'm wondering why you have your own Netconnections and Netstreamx classes. You really shouldn't need those.

    But basically, you just need a reference to a Netstream, and one to a Camera, and you can call netstream.attachCamera.

    So if you use your Netstreamx class, you just need a way to get the NetStream instance out of that class.

    Combining this question with the other thread, you would have a public variable stream in Netstreamx which is the Netstream.

    After you insure the connection is made and the stream exists, you can do this:
    Code:
    test.stream.attachCamera(camera);
    You might have to dispatch an event from Netstreamx to indicate when the connection is established. Then in CameraExample you would listen for that event. Make the camera variable class level in CameraExample so that you can attach it to the stream in that even listener.

  9. #9
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    well the stream is null but at least now i'm accessing the netstream variable ....its probably null because of the order in which everything is attaching to each other.....I'll have it fixed with a few hundred more posts.....AS 2.0 is so simple compared to this oop programming. I'm going to enjoy learning AS 3.0
    ~calmchess~

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    That stuff about the order is what I was referring to with dispatching an even when the connection is established.

    Good luck.

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