A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: How to avoid this using classes

  1. #1
    Senior Member
    Join Date
    Oct 2008
    Posts
    179

    How to avoid this using classes

    So I have made a class and need access to the stage to talk to movie clips and whatnot. In my flash movie, here is my new class instance:

    Code:
    var doc1:VTODDocument = new VTODDocument(this,"Sample Document 1111","somepdf1.pdf");
    You see I have to pass 'this' in order to have access to my stage. In my class use 'this' as a stage reference.
    Code:
    		public function VTODDocument(stage:MovieClip,title:String,url:String)
    		{
    			_p = (stage as MovieClip);
    			_title = title;
    			_url = url;
    			initDocs();
    		}
    From that I can call _p.SomeMovieClip.anotherMovieClip and so on. Is their anyway I can avoid entering 'this' in all my class constructors? I would like to just use
    Code:
    var doc1:VTODDocument = new VTODDocument("Sample Document 1111","somepdf1.pdf");
    but still have access to movie clips on the stage. Any ideas?

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    First, "this" in your example was never the Stage. It might be the root, but the stage is different.

    If your VTODDocument is a DisplayObject and put on the displayList, you can access things with parent, root, and stage, but I'd argue that the current set up is better than that. Even better still would be to re-think things so that VTODDocument never needs to call _p. anything.

    In general, letting kids control parents is a bad idea. You've been to McDonald's, you know this.

  3. #3
    Senior Member
    Join Date
    Oct 2008
    Posts
    179
    You are right, I shouldn't have named it stage. That was a bad reference. I meant more or less root. If you say it would be better to make it so it never has to call _p anything, how would I attach a movieclip from my library and place it on a movieclip holder? I am doing that here:
    PHP Code:
            private function initDocs():void
            
    {
                var 
    tH:Number _p.infowin.docHolder.height;
                var 
    dc:DocumentClip = new DocumentClip();
                
    dc.txt.autoSize TextFieldAutoSize.LEFT;
                
    dc.txt.text _title;
                
    dc.tH 15;
                
    dc.addEventListener(MouseEvent.CLICKlaunchLink);
                
    dc.addEventListener(MouseEvent.ROLL_OUTbtnOut);
                
    dc.addEventListener(MouseEvent.ROLL_OVERbtnOver);
                
    dc.buttonMode true;
                
    dc.useHandCursor true;
                
    dc.mouseChildren false;
                
    _p.infowin.docHolder.addChild(dc);
            } 
    So I am making a new DocumentClip() which is the movieclip I need to attach and then I am attaching it to _p.infowin.docHolder.addChild(dc);

    I dont know another way to do that without pointing it to one movie clip or another.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I'd ask why VTODDocument needs to mess with its parent's displaylist. You could create a method of VTODDocument which returns that new DocumentClip and let the parent call that method.

    Code:
    public function getAClip():DocumentClip
    {
                var dc:DocumentClip = new DocumentClip();
                dc.txt.autoSize = TextFieldAutoSize.LEFT;
                dc.txt.text = _title;
                dc.addEventListener(MouseEvent.CLICK, launchLink);
                dc.addEventListener(MouseEvent.ROLL_OUT, btnOut);
                dc.addEventListener(MouseEvent.ROLL_OVER, btnOver);
                dc.buttonMode = true;
                dc.useHandCursor = true;
                dc.mouseChildren = false;
                return dc;
    }
    Or you could create a method which accepts a DisplayObjectContainer to modify and let the parent call that.

    Code:
    public function initDocs(docHolder:DisplayObjectContainer):void
    {
                var tH:Number = docHolder.height;
                var dc:DocumentClip = new DocumentClip();
                dc.txt.autoSize = TextFieldAutoSize.LEFT;
                dc.txt.text = _title;
                dc.y = tH + 15;
                dc.addEventListener(MouseEvent.CLICK, launchLink);
                dc.addEventListener(MouseEvent.ROLL_OUT, btnOut);
                dc.addEventListener(MouseEvent.ROLL_OVER, btnOver);
                dc.buttonMode = true;
                dc.useHandCursor = true;
                dc.mouseChildren = false;
                docHolder.addChild(dc);
    }

  5. #5
    Senior Member
    Join Date
    Oct 2008
    Posts
    179
    I think I am going to try the first method you mentioned. That one makes the most sense to me at the moment.

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