all groups > flash actionscript > february 2006 >
You're in the

flash actionscript

group:

Properties/Functions of attached MovieClips


Properties/Functions of attached MovieClips blemmo
2/16/2006 6:59:37 PM
flash actionscript:
Hello,

I would be glad if someone could bring some light in this:
I did a Movie where I have a MovieClip in the library with some variables and
functions on its first Frame. I added this dynamically in a loop to the main
Movie via
--
day = _root.attachMovie("day", "day"+(i+1), i+1, {_x:50, _y:110});
--
I was possible to access its variables and functions. Great.
Now I'm doing another Movie, and wanted to use the same approach. Inserted a
new MC, put the function on Frame 1, and attached it to an empty Movieclip on
stage. But, for some reasons I can't see, now it's not possible to access the
function. What's going on?
The only differences: in the first Movie, the MovieClip had graphics in it; in
the second Movie, it's basically empty. It creates a TextField in Frame 1 with
ActionScript, and that TextField shows up on stage, but the function is not
accessible.
The other difference is that in the first, I attached the MC to _root, and in
the second, to another MC.

Could anybody explain what's going on here?

greets,
blemmo

Re: Properties/Functions of attached MovieClips kglad
2/16/2006 7:27:20 PM
Re: Properties/Functions of attached MovieClips blemmo
2/16/2006 7:57:17 PM
Ok, I set up a test movie. Inserted a MC, named and linked it "day", and put
this on its Frame 1:
--
function funk(){
trace("hi there");
}
--
In the main timeline, I put this on Frame 1:
--
day = _root.attachMovie("day","day1",1);
day.funk();
_root.day1.funk();
--
No traces happening. "List Variables" shows "Variable _level0.day1.funk = ",
so it should be present.

I don't get it...

blemmo
Re: Properties/Functions of attached MovieClips blemmo
2/16/2006 8:01:07 PM
It seems that "attachMovie" isn't synchronous. When I insert a second Frame and call 'funk()' there, it's working.

Not very nice...

cheers,
Re: Properties/Functions of attached MovieClips kglad
2/16/2006 8:05:29 PM
it has nothing to do with attachMovie and everything to do with the way flash
actionscript is executed, blemmo.

first flash executes all code from top to bottom in frame 1 of the _level0
timeline. it then executes all code in frame 1 of any movieclips that exist on
frame 1 (with the movieclips code executing one movieclip at a time) including
movieclips that exist by virtue of being attached.

that's the timing issue mentioned as one of the two probable causes of the
op's problem.
Re: Properties/Functions of attached MovieClips blemmo
2/16/2006 8:25:30 PM
Thanks kglad, now I see what was happening here. This makes sense, but you have to know it...

Thx for the explaination.

greets,
Re: Properties/Functions of attached MovieClips kglad
2/16/2006 9:15:50 PM
Re: Properties/Functions of attached MovieClips blemmo
2/16/2006 11:13:27 PM
Originally posted by: Newsgroup User
However, the code in the parent executes BEFORe the code in the just-placed
children.
...
On subsequent frames, the code in the child executes before the parent
...
Unfortunately, Macromedia/Adobe does not document the order of code
execution for parents and children.

Yes, way to go for MM/Adobe.

I think this is a serious flaw in logic. With attachMovie, you kind of
instantiate an object (hope it's the right word... making an instance of
s.th.). So calling a method of that object should be possible right after that
(even if it returns rubbish, because the object's members may not be set yet).
At least that's what I'd expect with a little OO background.

Originally posted by: Newsgroup User
If you want, define you function in the parent .. ie

day = _root.attachMovie("day","day1",1);
day.funk = function (){
trace("hi there");
};
day.funk();
_root.day1.funk();

That's what I did then. :)

Is it the same behaviour with class objects? I mean, would it be the same if I
had that function in an external class, and would use the class constructor
instead of attachMovie? I hope it wouldn't... guess I'll just try it.

Anyway, thx for the feedback.

greets,
blemmo
Re: Properties/Functions of attached MovieClips Jeckyl
2/17/2006 12:00:00 AM
[quoted text, click to view]

The flaw is really in your logic I'm afraid :) .. you asume that the code in
the first frame will get called immediately .. that sort of thing never
happens. All the code ofr a frame executes completely before any other
frame code .. so the code for the frame in the new clip does not get to
execute until after the code in the frame of the parent finishes. That is a
very sensible behaviour, you will know that the code in your frame executes
uninterrupted.

That's why it is better not to define 'methods' for your clips in the first
frame. Do it from the parent frame .. or define a class for your clip.

[quoted text, click to view]

All the methods of the clips class are available .. its just that then LATER
ON, you define an additional method .. that method is not available until
its defined.

[quoted text, click to view]

In your OO background, you would have defined you methods BEFORE you
instantiate .. you are adding a new one dynamically AFTER you instantiate.
That's where your problem lies. The same is true in any other OO system
that allows you to dynamically add methods.

[quoted text, click to view]

If your movieclip is an instance of a class, then attaching it will call the
constructor for that class immediately whenit is instantiated.

[quoted text, click to view]

You're welcome.

Jeckyl

Re: Properties/Functions of attached MovieClips Jeckyl
2/17/2006 12:00:00 AM
You're welcome :)

Re: Properties/Functions of attached MovieClips Jeckyl
2/17/2006 12:00:00 AM
[quoted text, click to view]

That is incorrect .. the movie clip is attached immediately

However, the code in the parent executes BEFORe the code in the just-placed
children.

This allows the parent to make changes ot the child when it is first placed

On subsequent frames, the code in the child executes before the parent

[quoted text, click to view]

Yes .. because then you give the code in the first frame of the clip a
chance to execute and define the function before it is called.

[quoted text, click to view]

No .. you're just doing things in the wrong order (or rather, making
assumptions about the order that the code exuctes that are incorrect). If
you want, define you function in the parent .. ie

day = _root.attachMovie("day","day1",1);
day.funk = function (){
trace("hi there");
};
day.funk();
_root.day1.funk();

Unfortunately, Macromedia/Adobe does not document the order of code
execution for parents and children.
--
Jeckyl

Re: Properties/Functions of attached MovieClips blemmo
2/17/2006 12:24:33 AM
Jeckyl, thanks for that little excursion, I think I see where this going. :)
I guess I thought of that code in Frame 1 of the MC as a kind of class
declaration, and not of code that gets executed after the instantiation. When I
think about it, it really makes sense that it's not; you could seriously mess
up the code execution.

Thanks for clearing this up!

cheers,
blemmo
AddThis Social Bookmark Button