Calling a function embedded in a movie

I am trying to call a function i have created inside a movie. I can load the movie to a container with not issue, re -position the movie as well.

However when i call the function from the root, i get no response … here is the as code below.

[AS]

//root function
_root.attachMovie(“worldMap”, “geoMap”, 75);
geoMap._x = 663;
geoMap._y = 158;

geoMap.createRond(250, 90, 150);

//function inside geoMap movie
function createRond(rondSize, x_position, y_position){
x = “rond”+x_position;
trace(x);
trace(rondSize);

this.createEmptyMovieClip(x, 90);
eval(x).lineStyle(rondSize,0,100);
eval(x).lineTo(.55,.45);

var newColor = new Color(x);
newColor.setRGB(0x00CCFF);

eval(x)._y = y_position;
eval(x)._x = x_position;

}
[/AS]

Functions aren’t object specific like a prototype, therefore you cannot attach it to a clip, it only works for clips defined within the function.

So in that case you wouldn’t use [AS]
geoMap.createRond(250, 90, 150);
[/AS] you would use [AS]createRond(250, 90, 150);[/AS]

lostinbeta. thanks again for answering my questions. Will try it out.

tried it … unfortunately it didn’t work.

But just to clear things up, you are saying that i can have a function embedded or nested within a movie that I attach to the main timeline, and the nested function will be available to the main timeline root?

It should work, you may have to call the function after you create the function, perhaps it doesn’t work because you are calling the function before the function is ever being created.

And you kinda confused me with what you said about functions, so I will try and explain deeper.

Functions are not object specific, therefore you cannot call them from another clip (as in geoMap.functionName()) Instead you target them as if they were a variable on that timeline or a clip on that timeline. If you call it in the same timelien it is in you can use this.functionName() or just functionName(), but if you call it from a timeline within another clip it would be _parent.functionName() and so on and so forth.

If you want it to be object specific so you can call it attached to the movie clip you will need to make it a prototype, you can read up on those here…

http://www.kirupa.com/developer/actionscript/tricks/prototypes.asp

note: Ok, I decided to test this now, and I removed geoMap from the function call, and the function ran fine. I don’t know what your function is supposed to do, but I got a shape with 2 corners sharp and 2 corners rounded, and filled with blue. So this leads me to believe the function works fine, and it may be the actions inside the function that aren’t working for you. In that case I don’t know what to fix because I don’t know what your doing.

lostinbeta. your results are accurate. the function works for me if i call it within its own timeline. so let say movie1 contains the createRond function. If within movie1’s timeline i do createRond();. thatw orks without a problem.

The issue is when i am in movie2 (my main movie timeline) load geoMap into a empty movie clip called geoMap and then try to call createRond from the main timeline.

I will however go and check out the prototypes.

Thanks for your help

Sean

So you can target your function via movie2.

if movie2 is your main timeline, and movie1 is a timeline in the movie1 timeline it would be movie2.movie1.functionName() Or you could just do movie1.functionName() if you are in the movie2 timeline.

Can’t seem to make it work. Here is the file. Let me know if you can get it to work. I really appreaciate your help on this one.

I think that the problem may be that you are calling the function before the clip is getting loading therefore the function doesn’t exist yet.

So you can get around this by

A) creating the function in the main movie instead of in the worldMap movie.

or

B) using setInterval to wait a bit before the function is called. You can do this like this… [AS]attachMovie(“worldMap”, “geoMap”, 2);
geoMap._x = Stage.width/2;
geoMap._y = Stage.height/2;
function loadFunction() {
geoMap.createRond(90, 100, 100);
clearInterval(myInterval);
}
myInterval = setInterval(loadFunction, 1000);[/AS]

that will make it execute after 1 second has passed, which is enough time for the clip to be loaded out of the library. And you will have to change your function like so…
[AS]function createRond(rondSize, x_position, y_position) {
trace(“inside rond fucntion”);
trace(rondSize);
x = “rond”+x_position;
trace(x+" please wotk");
clip = _parent.createEmptyMovieClip(x, 120);
clip.lineStyle(rondSize, 0, 100);
clip.lineTo(.55, .45);
var newColor = new Color(clip);
newColor.setRGB(0x00CCFF);
clip._y = y_position;
clip._x = x_position;
}[/AS]

Check my attachment…

looking at the movie, it looks like you added some reference to the clip with cip. notation.

Looking at the main timeline it looks like you inserted a break (i.e. interval).

you are a genius …

Yeah I added a reference to the movie clip you created, it makes it easier to target and more efficient to code.

And I used setInterval to delay a second and then run the load function. The load funtion contains the function call of the geoMap, since you can’t call it immediate and you have to wait for the clip to be attached. And I don’t think there is a way to use onLoad for attachMovie so I used setInterval to just delay it a bit until the actions finish running. The function then clears the interval so it doesn’t keep running over and over and over every second.