-
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?
-
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.
-
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.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; _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.
-
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);
}
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|