PDA

View Full Version : Accessing components/objects to one movieclip instance from another


nestorm
07-28-2006, 08:14 AM
This should be a very simple scope question. I'm a total newbie, please help!

MovieClip_A contains Component_1A

MovieClip_B contains Component_1B

How can I reference Component_1B from an event fired by Component_1A? I've already got the code, I just can't seem to reference the object with a qualified name. I've created a linkage name and exported for actionscript, still nothing.

icemart525
07-28-2006, 08:55 AM
assuming what you mentioned above are their instance names and all of them are in the same frame in the _root then:

Component_1A.onRelease = function() {
MovieClip_B.Component_1B.text = "hi"; // if it's a textbox
}

:D

straydog
07-28-2006, 08:57 AM
depends on how you have it set up on the timeline, if each MC is in a seperate keyframe that dont over lap then one Mc isnt on the stage while the other is, and when you try to access code from something that sint on stage, nothing happens.

maybe you could post your .fla or some examples of what you mean

nestorm
07-28-2006, 10:14 AM
I haven't been able to figure it out yet. I did some reading about components and I found that if addEventListener is used with a function, which is what I'm doing, then the function is put into the scope of the calling component instance rather than the object that declares it. It then said to use the Delegate class in order to bring the function into the desired scope. However, I tried quite a few different paths and none of them worked. I'm obviously not doing something right. Both MovieClip's are located on the same timeline frame, but separate layers & key frames. The code below is contained within one of the MovieClip's on its own layer and key frame. The same MovieClip contains the components that fire the events, which in this case are Button components.

mcServiceInfo.taDescription is the object I'm trying to modify from within mcServiceButtons, which the path of the button is mcServiceButtons.Button_Marketing. I could just put all of this into one MovieClip and fix this stupid problem I'm having, but I'd like to learn and fix this.

import mx.utils.Delegate;

Button_Marketing.addEventListener("click",Delegate.create(mcServiceInfo,ServiceButtonListen er));
Button_Business.addEventListener("click",Delegate.create(mcServiceInfo,ServiceButtonListen er));
Button_Internet.addEventListener("click",Delegate.create(mcServiceInfo,ServiceButtonListen er));

function ServiceButtonListener(objEvent:Object):Void {
if(objEvent.target == Button_Marketing) {
trace(taDescription.text);
trace("1");
}
else if(objEvent.target == Button_Business) {
//taDescription.text = "Test2";
trace("2");
}
else {
taDescription.text = "Test3";
trace("3");
}
}

[From Flash 8 Help File]

Delegate.create()
Availability
Flash Player 6 (6.0.79.0).

Edition
Flash MX Professional 2004.

Usage
Delegate.create(scopeObject, function)


Parameters
scopeObject A reference to an object. This is the scope in which to run the function.

function A reference to a function.

Description
Method (static); allows you to delegate events to specific scopes and functions. Use the following syntax:

import mx.utils.Delegate;
compInstance.addEventListener("eventName", Delegate.create(scopeObject, function));


The scopeObject parameter specifies the scope in which the specified function is called.

Example
For examples of Delegate.create(), see Delegating events in Using Components.

icemart525
07-28-2006, 10:43 AM
try using movieclips for your buttons and textareas. it's more simple and much better.:D

icemart525
07-28-2006, 10:49 AM
or try it this way:
for buttons

btnListener = new Object();
btnListener.click = function(eventObj) {
//do something
};
btn.addEventListener("click", btnListener);

place this code inside the movieclip and not in the _root.

nestorm
07-28-2006, 08:04 PM
Are there any tools that I can use that will aid ActionScript development? For example, how would I debug a scope issue like this?

fou99004
07-28-2006, 08:16 PM
I use Xray. It gives you the location tree where everything is at. You can try an use flash's debugger but it is a little harder to follow. trace() is always useful as well.

nestorm
07-28-2006, 09:55 PM
I just gave in and put it all in the same MovieClip. I used Xray to find the appropriate paths, but no matter what I did, the damn thing would not find the Object. I think it had something to do with the timeline, but I don't understand. I would reference _parent and it would bring me to the appropriate level, but then when I tried to access the MovieClip by instance name, it wouldn't work. One thing I noticed was that the MovieClip _name attribute was never the instance name that I gave it, in fact, while using Xray I never saw the instance name anywhere in the properties inspector of the appropriate MovieClip. So...I just gave up and put it all in the same clip and no more problems.

fou99004
07-28-2006, 10:09 PM
I'm sorry we couldn't help. When you were trying to use the Delegate.create method, did you try using this instead of mcServiceInfo? Or you could even say _level0. This way everything will fall under its scope and you should catch everything. Should and what actually happens are two different things though.

nestorm
07-28-2006, 11:58 PM
Yeah, I tried a mixture of everything I could think of. I will have to play with it some other time, I don't have the time to mess with it right now. I'm still learning ActionScript, so I'm sure I will catch on eventually.