# Referencing movieclip created in another function

**URL:** <https://forum.kirupa.com/t/referencing-movieclip-created-in-another-function/298180>\
**Category:** flash\
**Created:** [October 14, 2009, 8:12pm UTC](https://forum.kirupa.com/t/referencing-movieclip-created-in-another-function/298180 "2009-10-14T20:12:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![JayMasterJay](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@JayMasterJay](https://forum.kirupa.com/u/JayMasterJay)\
**Post date:** [October 14, 2009, 8:12pm UTC](https://forum.kirupa.com/t/referencing-movieclip-created-in-another-function/298180/1 "2009-10-14T20:12:53Z")

</div>

Hi,

I’m a complete newb to AS 3.0, but wrote a couple of small games using AS 2.0. One of the things I found easiest about AS 2.0 is that a MovieClip created using \_root.attachMovieClip() inside of one function was accessible to every other function. For instance:

AS 2.0

```auto
 
function AddClip() {
TheClip = _root.attachMovie("Object", "TheClip"+ClipCount, _root.getNextHighestDepth(), {_x:30, _y:30})
}
function ManipulateClip() {
TheClip._x = TheClip._x+10
}

```

Now in AS 3.0, when I declare a MovieClip inside of one function, I cannot figure out how to access that MovieClip from another function.

For instance:

AS 3.0

```auto
 
function AddClip() {
  var Terrain2_mc:Terrain=new Terrain();
  Terrain2_mc.x=0;
  Terrain2_mc.y=0;
  addChild(Terrain2_mc);
}
function ManipulateClip() {
  Terrain2.x = Terrain2.x+10
}

```

This causes an error, because the MovieClip variable was declared inside of a function and when the function ended, the MovieClip variable was erased.

Is there any way of doing this without declaring the MovieClip variable outside of the function? I want to be able to create an unknown amount of these movieclips on the fly, so I can’t just declare them outside of the function.

Any help would be appreciated.

Thanks,

-JayMasterJay
