|
-
talking to another class
i am still trying to wrap my head around as3/oop logic- say i have an interactive image on the stage...it is its own class (BigImage), with private functions to do different things. Depending on what you want, you can shrink it, tweek it, move it, etc.... Behind it, i have an MC , also it's own class (BackDrop), designed to respond to this images changes (scale, alpha, etc)---if, for example, i drag the image, BackDrop mc, will follow. At present, my logic was to set up public function s on the BackDrop mc , and then from the BigImage class, call these as they are necessary. So, for example, i have a public function on the BackDrop that states:
Code:
public function positionMe(wd:Number,ht:Number):void
{
this.readyHeight=false;
this.readyWidth=false;
this.finalW=wd;
this.finalH=ht;
this.alpha=0
this.x=stage.stageWidth/2
this.y=stage.stageHeight/2
this.addEventListener(Event.ENTER_FRAME,rebuild)
}
and then from BigImage, on a click, drag, or whatever, i have :
Code:
private function fadeIn(e:Event):void
{
this.newW=maxW;
this.newH=maxH;
bd=BackDrop(stage.getChildByName('backdrop'))
bd.positionMe(newW+10,newH+10)
}
i get this feeling there is a more oop way of doing the same thing....there's a voice in my head saying eventListeners and dispatchEvent -so, before i start really bad habits, can someone guide me the right way, and also tell me why this the wrong way?
ps- i already read this http://board.flashkit.com/board/showthread.php?t=743340
and i am browsing oop related articles after i click submit new thread....just asking the above to help the learning curve
-
I am not completely clear on what you are trying to do, but I would suggest having one of the instances contain another (therefore having a reference to it), or have a third instance (of a new class) contain both instances (and therefore be able to access and manipulate both of them).
Event listeners etc work well, but I prefer composition for simplicity. If only one object will be accessing a second object, there is no need to set up the whole event dispatcher thing.
Now if several objects were accessing the one object, you would have to work out a better method.
The greatest pleasure in life is doing what people say you cannot do.
- Walter Bagehot
The height of cleverness is to be able to conceal it.
- Francois de La Rochefoucauld
-
bd=BackDrop(stage.getChildByName('backdrop'))
what this suppose to means?
why stage[backdrop] wont work?
and I tried this code:
NEW_OBJECT_NAME = CLASS(stage.getChildByName('INSTANCE'S_NAME'))
and it return a run time error.
Last edited by ArielGenesis; 09-04-2007 at 04:28 AM.
-
69...'s got a good point. There's really no reason to look up the backdrop every time when you could just store a reference to it.
Code:
public var bd:BackDrop;
private function fadeIn(e:Event):void{
...
Then wherever it is you create/place BigImage on stage, you can just set the bd property to the backdrop. You could even pass it in the BigImage constructor.
------------------
ArielGenesis, the line you pointed out gets a reference to the DisplayObject named "backdrop" which is a child of the stage, then casts it to a BackDrop so that methods defined in BackDrop can be used.
stage[backdrop] won't work because 1) backdrop is not defined. If you put backdrop in quotes, it may work, but that syntax would get you the object regardless of whether or not it's in the display list, so the meaning is a little different.
Your code
Code:
NEW_OBJECT_NAME = CLASS(stage.getChildByName('INSTANCE'S_NAME'))
Is pretty much nonsense. If I understand your naming conventions, you would actually have written something like:
Code:
var foo:Bar = Bar(stage.getChildByName('baz'));
If that's so, then it should work if and only if there actually is an instance named "baz" of type Bar that is a direct child on the stage. Since you didn't actually say what error you got, I don't know what went wrong for you.
-
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
|