Skip to main content
Inspiring
June 16, 2008
Answered

how to send a variable from one class to another?

  • June 16, 2008
  • 2 replies
  • 16444 views
hello,
i have "One.as", which is the document class for one.swf.
i also have "two.swf", with "Two.swf" entered as its document class...

One loads two into it. two is basically a 10 frame movieClip with a variable at the beginning called "var endFrame:Boolean = false", then on the last frame it says endFrame = true.

my question: how in the world do I communicate this back to One.as??? i've tried ENTER_FRAME listeners, declaring the variable here and there... and many other embarrassingly usuccessful strategies.

i would just like to load in "three.swf" after two.swf finishes... but One needs to know that it has indeed finished.

your help would be greatly appreciated. thanks
This topic has been closed for replies.
Correct answer Newsgroup_User
yarkehsiow,

> David,
> thank you for responding.

Sure thing! :)

> so does what you are saying mean that endFrame is a property of
> two (the movieClip), or Two (the Class)?

If you've written a property named endFrame for your Two class, then
yes, Two.endFrame is a property of that class.

Looking back at your original post, I see that you wrote this:

> One loads two into it. two is basically a 10 frame movieClip
> with a variable at the beginning called "var endFrame:Boolean = false",
> then on the last frame it says endFrame = true.

So it sounds like your Two class extends MovieClip. (I'm not sure
that's true, but that's what it sounds like.) That means your Two class
supports all the features of the MovieClip class, including a play() method,
a currentFrame property, and so on. In addition, you've added new
functionality that amounts to -- by the sound of it -- a property named
endFrame. If you made your property public (i.e., public var endFrame),
then it should be accessible by way of an object reference to your Two
instance.

myTwoInstance.endFrame;

> so can i invoke that method in One.as? do I call it Two.endFrame (if
> (Two.endFrame == true) {?

Methods are things an object can *do,* such as gotoAndPlay(). What
you're describing is a property (a characteristic ... in this case, a
Boolean characteristic). You wouldn't use the expression Two.endFrame
unless that property was static. Static classes are those that cannot have
an instance made of them. Think of the Math class. It contains numerous
static properties in the form of constants, such as Math.PI, Math.E,
Math.SQRT2, and so on. You can't create an instance of the Math class -- it
wouldn't make sense to -- so Math is a static class.

On the other hand, you definitely create instances of the MovieClip
class. Every movie clip symbol is an instance of MovieClip class, which
means that each instance carries its own unique values for MovieClip class
members. The MovieClip class defines x and y properties, but each movie
clip symbol (that is, each instance of the MovieClip class) configures its
own values of those properties, depending on where each instance is located
on the Stage.

Assuming your Two class is not static, then somewhere along the line,
your One class will have to make an instance of it. Somethine like ...

// inside your One class ...
var myTwo:Two = new Two();

... at which point that myTwo variable because a reference to that
particular instance of Two. You can invoke Two methods on that instance.
You can invoke Two properties and events on that instance. You can invoke
whatever functionality is defined by the Two class on that myTwo instance.
If Two extends MovieClip, that means you can also invoke any MovieClip class
member on that myTwo instance.

At some point in your One class, you can refer to that myTwo instance
later and check if the value of myTwo.endFrame is true or false.


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."


2 replies

Inspiring
June 16, 2008
dear David,
your long, detailed explanation helped me finally figure out my problem! (problems... there were several)
thank you SO much for your time helping me with this most basic problem. i now have 3 .as files communicating, using (at least what seem to me) getter/setter variables that you mentioned in your first post.
again, thank you.
RW
Inspiring
June 16, 2008
yarkehsiow,

When you write a custom class, what you're doing is creating a new data
type, just like any of the data types that ship with Flash (e.g., Array,
Date, String, etc.). So imagine this: you have a dynamic text field on the
Stage and you want to populate it with the current date. To do that, you
would either a) instantiate the TextField class or b) draw a text field to
the Stage with the Text tool. Either one of those gives you a TextField
instance. The TextField class features a TextField.text property, and
that's what lets you instruct the text field on what text to display.

Next, you would create a Date instance and store it in a variable:

var myDate:Date = new Date();

At this point, your Date instance (myDate) gives you access to a
selection of the variables inside that instance. Because of how Date was
written, you can get the full year by invoking the Date.getFullYear() method
on your instance. You can get the month by invoking the Date.getMonth()
method on your instance, then converting the resultant number to a string --
such as "June" -- by comparing it against an array of month names (just for
example).

The Date class allows you "in" by way of these methods. Depending on
how a date is written, such variable data can be retrieved by methods or
properties. Think of MovieClip.x, for example, which gives you the x
position of a movie clip (and, by happenstance, allows you to *set* that x
position).

You would invoke TextField.text on your text instance and pass it a
string of some sort, probably concatenated from the return values of various
methods of the Date class. And that's how it's done. Classes are
specifically structured to hide certain information and reveal only what is
useful to outside code.

Does that help? It sounds like you want to write getter/setter methods
for your class.


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."


Inspiring
June 16, 2008
David,
thank you for responding.
so does what you are saying mean that endFrame is a property of two (the movieClip), or Two (the Class)?
so can i invoke that method in One.as? do I call it Two.endFrame (if (Two.endFrame == true) {?
I'm sorry, I'm very confused, but would really like to figure this out...