Skip to main content
December 31, 2008
Question

Still don't get how to have a class ref timeline...

  • December 31, 2008
  • 10 replies
  • 906 views
If I load a class on my root timeline and I have a movie clip call "ball_mc" on the root timeline. How do I make a function in the class control that movieclip?

I don't like the addchild because I design my pages and then animate them.

If I'm inside the movie clip using actions trying to reference a main timeline item I use MovieClip(root).instanceName. This works perfect, however it doesn't work in a class.

I would use classes if they were easy to reference objects on the main timeline...

Thanks...
This topic has been closed for replies.

10 replies

Inspiring
January 2, 2009
>>For instance I do a lot of remoting with Coldfusion. I would love to make
>>a
remoting class where most of the code is reusable. However when a call to
remoting returns an array of information to place it in the list box that is
located in the mainPageMC.resultList you can't easily. It just seems to me
you
interact one way with a class. The class doesn't interact back with the main
timeline.
<<

In class terms I'd do like Andrei1 said, and use events/listeners. In your
remoting class you call your service and get the array back. When it comes
back properly you fire an event and anything listening knows the data is
ready. Your listBox then just gets the data from the class. This makes it
easy for other things to use the data. You just need to think of the
structure a little differently than you are used to.

>> I always found that since 99% of my work was all custom websites and apps
>> that
if I were making classes I wouldn't reuse them much anyways. So I just
placed
all my custom code into different code layers to organize it. It was very
neat
and easy to work with
<<

I think most everyones work is custom sites and apps... Say for instance you
have need to scroll text a lot. You might make a scroller class, that just
takes references to the scroll objects, and a text field. That way your
scroller code can work with any custom elements, and can easily be used in
other projects, or be edited by other people. Everytime you want to use a
scroller you don't have to open the last project and copy/paste code... you
can just do something like: var myScroller = new
com.crock.CustScroller(upArrow, dnArrow, track, slider, textField); and
you're done... If you're a smaller shop, or a one man show the whole 'other
people can work on it' may mean nothing to you. However once you get a code
library built up it can make building apps much quicker and less error
prone.

--
Dave -
www.offroadfire.com
Head Developer
http://www.blurredistinction.com
Adobe Community Expert
http://www.adobe.com/communities/experts/


January 2, 2009
Thanks a lot for all of your help. So if you (any of you) were building a project that were to be a simple website say five pages with a "background swf" and each "page" it's own swf, what's the standard for your architecture. Obviously most of the classes would be loaded in the background swf and most of your "global" variables would be created there as well to be used throughout all movies, correct?

Do you guys use the add child method to "building" your stages? If so how do you design it? Build everything so it looks nice and then note it's location and then remove it from the stage and then build it with add child?

I want to do things the "standard" way because I believe adobe will keep adapting its applications this way. It also makes me more valuable as my company grows and when working with others on a project.

I always found that since 99% of my work was all custom websites and apps that if I were making classes I wouldn't reuse them much anyways. So I just placed all my custom code into different code layers to organize it. It was very neat and easy to work with. But with everything being right in the root it made things very easy.

Thanks, again.
Inspiring
January 2, 2009
quote:

Originally posted by: C-Rock
So if you (any of you) were building a project that were to be a simple website say five pages with a "background swf" and each "page" it's own swf, what's the standard for your architecture. Obviously most of the classes would be loaded in the background swf and most of your "global" variables would be created there as well to be used throughout all movies, correct?


I like very much using document class that takes care of all the top level configuration and initialization needs. I guess this is an alternative to what you call "background swf".

How and what to load depends on each particular case. Usually there is more to it than loading several different pages/swfs (like navigation, etc.)

As for the "global" variables one of an approaches I use is to create a class or a group of classes that serve as a depository of the information that is available to all the classes in the package. Static properties and methods come very handy.

quote:

Do you guys use the add child method to "building" your stages? If so how do you design it? Build everything so it looks nice and then note it's location and then remove it from the stage and then build it with add child?


Yes, addChild is practically the only way to place anything into display list. As for the design - it depends. Sometimes I do try objects on the stage before implementing a more dynamic approach. But most of the time mockup (or wireframe) will do and I just write an algorithm based on the requirements. In may cases the full visual is not available until runtime, so there is no way to test it in hardcoded incarnation.

quote:

I always found that since 99% of my work was all custom websites and apps that if I were making classes I wouldn't reuse them much anyways. So I just placed all my custom code into different code layers to organize it. It was very neat and easy to work with. But with everything being right in the root it made things very easy.


I agree that in the majority of cases code is unique with the exception of some service functionality that I anticipate I will reuse. Nevertheless, classes and packages have much more than future recycling to offer. They are a much better way to organize the code than writing it on the timeline. They offer many great ways to perform asynchronous tasks that makes application faster and user experience more streamlined. Testing and debugging isolated classes is much easier and once you are finished with a class it is performing uniformly well.

List goes on and on.

One suggestion I would like to make. Since you are just starting this approach, it would be very beneficial for you if you look into application architecture modeling. UML offers very nice venues to conceptualize and visualize you projects. They say that 80% of time should go into planning and 20% to coding. I discovered a hard way how right they are. You cannot imagine what benefits await for you once you develop a discipline and ways to model your projects before you open Flash IDE - your classes will fly off the shelf like crazy :-)


January 2, 2009
Thanks.
Inspiring
January 2, 2009
"However if you weren't a class user in AS 2.0 the jump is very hard to 3.0."

I agree. But I don't think it's that much different to becoming a class user in as 2.0. Perhaps in some ways it may be easier as there are more strict approaches that give you more compiler errors when you make mistakes. In other ways it may be more difficult as you also need to get used to the differences in the way the avm works/Display list etc.

I am self taught also. The way I learnt quickest (making the 'jump' to as3 after learning class based programming in as2) was getting involved and contributing to an as3 opensource project (actually it is a flex project... but that's just as3 if you forget about the stuff the compiler does with mxml etc). It's a great way to learn because you have team support, common goals and open discussion etc. I realize not everyone has the time to do that though. I'm just letting you know that it helped me learn as3, as well as a start into design patterns and also to start to get my head around flex. I mention it because not everyone might think of doing that to learn.

I think the "as3 is too" hard/difficult/verbose comments are not new. I still see people making the change though - but it is a choice. And I'm sure there are people who choose not too as well. I think that's fine as as2 is still 100% supported in the player (and is still vital for targetting some devices in any case). I suspect that at some point, it will become inevitable that you'll need to commit to the change though.

As an aside, learning class programming in actionscript also made a huge difference to me with my serverside scripting as well. I now use classes routinely in my php work whereas I had limited and basic php scripts before. If you look at the structure of a coldfusion app with directories and component definitions, there is likely to be some similarities there as well even if the keywords are different and it's tag based.

I would say half of what you need to 'learn' is not the ability to code classes - the rules behind doing that is relatively easy, it's the thinking that goes into it beforehand - how you architect your application so it does all work well together. And that's where some design pattern books etc might help as well, although outside of that its just observing how others approach a particular 'problem type' or trying different approaches yourself (I would say I still do this a lot).

In terms of productivity, if you are writing a lot of class based code I would recommend using an external editor. I use FlashDevelop and its much quicker/easier to code a lot of things - that kind of offsets the 'as3 is too verbose' argument because there are a lot of smarts built in that accelerate coding - and it can work with flash itself to do the compiling.

This wasn't meant to be this long. Better stop there :)
January 2, 2009
For instance I do a lot of remoting with Coldfusion. I would love to make a remoting class where most of the code is reusable. However when a call to remoting returns an array of information to place it in the list box that is located in the mainPageMC.resultList you can't easily. It just seems to me you interact one way with a class. The class doesn't interact back with the main timeline.

Maybe my self taught way of programming in Flash is incorrect. I have four AS 3.0 books and they all show creating classes to do anything. It doesn't seem user friendly or fast programming. Sure it may be faster for the client but I've never had a problem with file size or speed, it's always been my company's strength with Flash.
Inspiring
January 2, 2009
quote:

Originally posted by: C-Rock
I wouldn't say that the "root" syntax was bad programming.


Carey,

You still can use root. There is nothing wrong with it. It will work almost exactly as it worked in AS2.

The truth is that there is no right or wrong way of doing things. Whatever works for you at this stage is just fine.

I hope you will realize in a short awhile that AS3 is much better and more powerful than AS2 and it can not only pay your bills but also buy you luxury.

There is a higher level of abstraction in AS3 that gives you so much more flexibility. So, in response to your statement "The class doesn't interact back with the main timeline" I can just say that it is up to you to make it interact. Ned and I made a couple of suggestions on how to achieve it.
January 2, 2009
I understand everything you guys have said, however I build many applications where you just have variables on the root timeline that are storing variables. You also have movie clips that need to react to some other movie clips, not always at the end of the frames.

If you're making a movie clip do something in a class and after or during you're in a class function and you want another movie clip on the stage to do something, it's not easy. It's all this loading of class re initializing a class, a pain...

I want to use something simple without 100 lines of code to just make something work. AS 3.0 has turned to nothing but awkward coding.

I wouldn't say that the "root" syntax was bad programming. It paid my bills for 5 years and I've done some really nice things with Flash. However if you weren't a class user in AS 2.0 the jump is very hard to 3.0.
Inspiring
January 1, 2009
In the simple example I gave, what doesn't make sense? You can make a simple
movie to test - stick the .as file and the .fla in the same folder - make a
mc with an instance name of ball_mc and test.

You pass in a reference to ball_mc in the constructor, assign it to a
local - control variable, and then use that in the moveX method.


If you wanted to do it the listener way that Andrei1 mentioned, something
like this should get you started:

package
{
import flash.display.Sprite;
import flash.events.Event;

public class Testing extends Sprite
{
public static const XMOVE:String = "xm";

function Testing() {}

public function moveX()
{
addEventListener(Event.ENTER_FRAME, emoveX);
}

private function emoveX(e:Event)
{
dispatchEvent(new Event(Testing.XMOVE));
}
}
}


Then in Flash:

var mt = new Testing();
mt.moveX();

mt.addEventListener(Testing.XMOVE, doxmove);

function doxmove(e:Event){
ball_mc.x += 2;
}


You make a new instance of the Testing class, and call the moveX method,
which in turn starts calling the class' emoveX method at each enter frame.
In emoveX a new event is dispatched...and in Flash a listener is added to
call the local doxmove method, which does the actual moving... This is
likely not the most efficient way, but I'm still coming to grips with as3
myself.

--
Dave -
www.offroadfire.com
Head Developer
http://www.blurredistinction.com
Adobe Community Expert
http://www.adobe.com/communities/experts/


Inspiring
January 1, 2009
quote:

Originally posted by: Newsgroup User
.and in Flash a listener is added to
call the local doxmove method, which does the actual moving... This is
likely not the most efficient way, but I'm still coming to grips with as3
myself.



On a side note. I observe that AS3 event model is the least understood and, thus, the least used feature although it is one of the most (if not THE most) powerful things any programmer can wish for. Using events is extremely efficient and infinitely reliable.

January 1, 2009
Still doesn't make sense to me? It doesn't matter how I want to control it. I want to know how to "target" or "reference" a variable or a movie clip from inside a class that has been loaded on the main timeline.
Inspiring
January 1, 2009
quote:

Originally posted by: C-Rock
Still doesn't make sense to me? It doesn't matter how I want to control it. I want to know how to "target" or "reference" a variable or a movie clip from inside a class that has been loaded on the main timeline.


My point is that you don't have to and, perhaps, you should not for sanity sake. See my previous post with an example.

Inspiring
January 1, 2009
I did say 'easiest'. Maybe you can provide a simple example...

--
Dave -
www.offroadfire.com
Head Developer
http://www.blurredistinction.com
Adobe Community Expert
http://www.adobe.com/communities/experts/


Inspiring
January 1, 2009
I don't think passing references is the easiest thing on a large scale. I think such a tight coupling leads to a very cumbersome unscalable architecture and, thus, makes it more difficult. Not only tileline's child must be aware of timeline - it also must be aware of other children on the timeline. It becomes a nightmare if a necessity for decoupling transpires in the future.

I believe that a much better approach is to make any object as independent as possible.

In this particular case (although we don't know much about what needs to happen) I would do it the following way:

Say, we have 1) timeline, 2) object that, say, we need to move (name of the object is movableObject) and 3) object that is doing something and when it is finished (say, it reaches the last frame) - movableObject needs to move. I am not using a button example here because it doesn't require anything happen in it - just listening to MouseEvents on the timeline will do.

Let's name the third object myMovie.

Inside myMovie class I would write:

this.addEventListener(Event.ENTER_FRAME, enterFrameListener);

private function enterFrameListener(e:Event):void{
if(currentFrame == totalFrames){
stop();
dispatchEvent(new Event("gotThereSafe"));
else if(currentFrame == Math.round(totalFrames * .5)){
dispatchEvent(new Event("halfWayThere"));
}
return;
}
}

==============================

On the timeline

myMovie.addEventListener("gotThereSafe", movieListener);
myMovie.addEventListener("halfWayThere", movieListener);

function movieListener(e:Event):void{
switch(e.type){
case "halfWayThere":
movableObject.x = 50;
break;
case "gotThereSafe":
movableObject.x = 100;
break;
}
return;
}

With this architecture dependency is created on the timeline and myMovie doesn't have to know anything about either timeline or what timeline decides to do with the internal workings of myMovie.

Another benefit is that if at some point myMovie becomes (for whatever reason) a child of another display object, functionality of myMovie doesn't have to change. Say, we place another clip with a name parentMovie on the timeline. And make muMovie a child of parentMovie. The only thing we need to do is to change listeners on the timeline and set capture to true:

parentMovie.addEventListener("gotThereSafe", movieListener, true);
parentMovie.addEventListener("halfWayThere", movieListener, true);

I had to change only two line of code.
Inspiring
December 31, 2008
You did not describe how want to control an object on the timeline from withing the class but, conceptually, I am not sure it is such a good idea to pass reference to the CONTROLLED object (or even timeline). It makes your class too dependent on its position in display list. For instance if you move this class a level deeper you will have to reprogram all the references.

With that said, I think it is a much better and more flexible architecture if your class broadcasts what it is doing via events and other objects (timeline or whatever) listen to these events and do whatever is needed.
Inspiring
December 31, 2008
You wouldn't need addChild() because the ball would already be on the
display list... I think your easiest method would just be to pass in a
reference to the object you want to control. You could do that in the
constructor, or in your control method, it's up to you.

Testing.as:

package
{
public class Testing
{
private var control:*

function Testing(targ:*) {
control = targ;
}

public function moveX():void {
control.x += 10;
}
}
}


in Flash:

var mt = new Testing(ball_mc);
mt.moveX();


--
Dave -
www.offroadfire.com
Head Developer
http://www.blurredistinction.com
Adobe Community Expert
http://www.adobe.com/communities/experts/