PDA

View Full Version : [AS2.0-Beginner) Working with a movie-clip & variables/events


Denaes
04-17-2006, 02:48 PM
Just re-starting to learn how to transplant my Java & .Net knowledge over to Flash and I had a few things come up that I just don't seem to be able to find an answer for.

Saying you're going with a normal Flash movie with a timeline (and not Forms), I might decide to add a few movie clips to the application, which contain controls and maybe images.

Just to be easy, assume I have a clip created called Name_mc with a textbox in it called Name_txt & a button called Send_btn.

First I was wondering about events. I can get an event handler to work within the same frame/level of the movieclip, but how would you assign a function on the main clip to listen for an event comming from a control within a MovieClip? Say I wanted to subscribe to Name_txt's "change" event? Or Send_btn's press event?

Next, I have a problem getting at the variables within a movie clip. I can get to "this.Name_mc.Name_txt.text" just fine to get the text from the textbox. But I can't seem to be able to create a variable on the instance that I can access from outside of the movie clip. Is there a particular way I have to expose properties/methods I'd want to use from outside?

The same as above, but applything the properties/methods with a SWF file. Does the SWF work the same as a movie clip once you have it in place?

Probobly the first question I should have asked, but would it be better (or even possible) to create a custom control with these many other controls (and their logic) in it and exposing properties/methods for outside use?

I seem to have scope down fine with classes with the whole public/private thing, but these don't seem to apply on a frames code.

I know most of this is Noob stuff, but the books I have are covering solely actionscript 2.0 and OOP techniques or making animation. I seem to not be able to find this one aspect I'm looking for.

kensuguro
04-17-2006, 04:12 PM
First I was wondering about events. I can get an event handler to work within the same frame/level of the movieclip, but how would you assign a function on the main clip to listen for an event comming from a control within a MovieClip? Say I wanted to subscribe to Name_txt's "change" event? Or Send_btn's press event?


Just had a similar problem myself. I believe this is a problem with flash's strange way of event handling.. in flash events travel from parent to child direction only, and not back up. And I doubt the location of the mc within the layers matter (a mc on top won't recieve event if the mc on bottom is parent) Anyway, it's just strange, and you'll need to work around it.

http://www.senocular.com/flash/tutorials/buttoncapturing/
I just try my best to avoid the situation all together since it's such a drag. I know it sounds really strange.


Next, I have a problem getting at the variables within a movie clip. I can get to "this.Name_mc.Name_txt.text" just fine to get the text from the textbox. But I can't seem to be able to create a variable on the instance that I can access from outside of the movie clip. Is there a particular way I have to expose properties/methods I'd want to use from outside?


I think the problem is that "text" is a property of "Name_txt" textfield instance, and so you really can't add properties to it at runtime. Since textfield is a class, the only way you can add properties and methods to it, is to subclass it... and I have a feeling that's not what you're looking for. or is it? By the way, can you dynamically add properties and methods to classes at runtime in java or .net? (just curious)

And your other question, I wasn't sure of what you meant.. what's a "custom control"? Well, I'm pretty new to this myself, so maybe more knowledgable people can offer their wisdom.

Denaes
04-17-2006, 04:25 PM
Just had a similar problem myself. I believe this is a problem with flash's strange way of event handling.. in flash events travel from parent to child direction only, and not back up. And I doubt the location of the mc within the layers matter (a mc on top won't recieve event if the mc on bottom is parent) Anyway, it's just strange, and you'll need to work around it.

http://www.senocular.com/flash/tutorials/buttoncapturing/
I just try my best to avoid the situation all together since it's such a drag. I know it sounds really strange.

Wow, thats interesting. I'll check out that tutorial. Maybe just making a custom control might be better for what I'm doing. This is what I would do in .Net

I think the problem is that "text" is a property of "Name_txt" textfield instance, and so you really can't add properties to it at runtime. Since textfield is a class, the only way you can add properties and methods to it, is to subclass it...

I'm not trying to add properties to Name_txt. "text" is a property of it, and I'm able to access that property to get the value from my main movie. But I can't get any other variables values from the main form.

"this.Name_mc.Name_txt.text" works just fine.

If I created a variable called CalculatedWeight_Num and tried: "trace(this.Name_mc.CalculatedWeight_Num.toString() )" it would be undefined. I can access the control, but not variables.


And your other question, I wasn't sure of what you meant.. what's a "custom control"? Well, I'm pretty new to this myself, so maybe more knowledgable people can offer their wisdom.

Well a custom control is a control that you create/derive.

In my case I'm talking about taking a group of associated controls and putting them into a new control as well as all of their logic. So you might have an "AddressControl" which is a few textboxes and maybe a combobox for state with logic for populating and validating all of the address information.

It's a method of encapsulation so that you can just use the control and worry about putting info in and getting it back out. Then say if you needed an address control, I could just give you mine and you wouldn't have to worry about creating your own controls/logic.

I'm not sure if you can do that with controls in Flash. I do that all the time in .Net.

Similar to an OOP concept of encapsulating all the information within a class (which is what the control would be)