# Calling a function embedded in a movie

**URL:** <https://forum.kirupa.com/t/calling-a-function-embedded-in-a-movie/21969>\
**Category:** flash\
**Created:** [May 28, 2003, 4:58pm UTC](https://forum.kirupa.com/t/calling-a-function-embedded-in-a-movie/21969 "2003-05-28T16:58:51Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Obelisk](https://avatars.discourse-cdn.com/v4/letter/o/f475e1/32.png) [@Obelisk](https://forum.kirupa.com/u/Obelisk)\
**Post date:** [May 28, 2003, 4:58pm UTC](https://forum.kirupa.com/t/calling-a-function-embedded-in-a-movie/21969/1 "2003-05-28T16:58:51Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2003, 5:19pm UTC](https://forum.kirupa.com/t/calling-a-function-embedded-in-a-movie/21969/2 "2003-05-28T17:19:12Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2003, 5:21pm UTC](https://forum.kirupa.com/t/calling-a-function-embedded-in-a-movie/21969/3 "2003-05-28T17:21:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2003, 5:47pm UTC](https://forum.kirupa.com/t/calling-a-function-embedded-in-a-movie/21969/4 "2003-05-28T17:47:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2003, 6:21pm UTC](https://forum.kirupa.com/t/calling-a-function-embedded-in-a-movie/21969/5 "2003-05-28T18:21:49Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2003, 6:42pm UTC](https://forum.kirupa.com/t/calling-a-function-embedded-in-a-movie/21969/6 "2003-05-28T18:42:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2003, 6:53pm UTC](https://forum.kirupa.com/t/calling-a-function-embedded-in-a-movie/21969/7 "2003-05-28T18:53:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2003, 7:08pm UTC](https://forum.kirupa.com/t/calling-a-function-embedded-in-a-movie/21969/8 "2003-05-28T19:08:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2003, 7:36pm UTC](https://forum.kirupa.com/t/calling-a-function-embedded-in-a-movie/21969/9 "2003-05-28T19:36:22Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2003, 7:36pm UTC](https://forum.kirupa.com/t/calling-a-function-embedded-in-a-movie/21969/10 "2003-05-28T19:36:57Z")

</div>

Check my attachment…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2003, 7:46pm UTC](https://forum.kirupa.com/t/calling-a-function-embedded-in-a-movie/21969/11 "2003-05-28T19:46:11Z")

</div>

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 …

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 28, 2003, 7:52pm UTC](https://forum.kirupa.com/t/calling-a-function-embedded-in-a-movie/21969/12 "2003-05-28T19:52:17Z")

</div>

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.
