# Beginner's question: targeting clips on stage from a class

**URL:** <https://forum.kirupa.com/t/beginners-question-targeting-clips-on-stage-from-a-class/262972>\
**Category:** flash\
**Created:** [June 11, 2008, 2:50pm UTC](https://forum.kirupa.com/t/beginners-question-targeting-clips-on-stage-from-a-class/262972 "2008-06-11T14:50:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![0L4F](https://avatars.discourse-cdn.com/v4/letter/0/848f3c/32.png) [@0L4F](https://forum.kirupa.com/u/0L4F)\
**Post date:** [June 11, 2008, 2:50pm UTC](https://forum.kirupa.com/t/beginners-question-targeting-clips-on-stage-from-a-class/262972/1 "2008-06-11T14:50:12Z")

</div>

Hello all,

I’m currently making my first steps in migrating from AS2 to AS3, and I’ve stumbled upon a very basic issue:  
I design my Flash-sites inside the Flash application. At some point, I have some clips on the stage that I want to target with code. Now, from what I understand, in AS3 it’s best to put your code in class files.  
But: how do I target clips that are on the stage from a class when this:

```auto

package com.rocketclowns.app {

    import flash.display.*;
    import flash.events.*;

    public class BoxMove extends MovieClip {

        public function BoxMove() {
            mBox.addEventListener(Event.ENTER_FRAME,onFrameLoop, false, 0 ,true);
        }

        public function onFrameLoop():void {
            mBox.x ++;
        }
    }

}

```

throws the famous 1120 “possibly undefined” error, even with an mBox instance on stage? In other words, how do I tell the class what is currently on the stage?

Thank you for your help!

1.

---

<div class="post-metadata">

**Author:** ![DiamondDog](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DiamondDog](https://forum.kirupa.com/u/DiamondDog)\
**Post date:** [June 11, 2008, 5:13pm UTC](https://forum.kirupa.com/t/beginners-question-targeting-clips-on-stage-from-a-class/262972/2 "2008-06-11T17:13:26Z")

</div>

How are you using the BoxMove class?

Is it your Document Class?

Is it linked to a library symbol?

Or is it neither of those?

---

<div class="post-metadata">

**Author:** ![0L4F](https://avatars.discourse-cdn.com/v4/letter/0/848f3c/32.png) [@0L4F](https://forum.kirupa.com/u/0L4F)\
**Post date:** [June 11, 2008, 6:34pm UTC](https://forum.kirupa.com/t/beginners-question-targeting-clips-on-stage-from-a-class/262972/3 "2008-06-11T18:34:29Z")

</div>

Well, in AS2, I’d put the code in it’s own layer on the frame where mBox first appears. Now in AS3, I do something similar: I import / instantiate the class on the frame where mBox first appears:

```auto

import com.rocketclowns.app.BoxMove;

var MyBoxMove = new BoxMove;

stop();

```

I do this mainly to avoid a lot of data on the first frame of my .swf. In this way I can easily create lightweight preloaders, etc. A habit I learned in my AS2 days. This is also the reason I won’t use the document class, but timeline imports instead.

Is this a wrong way of thinking?

---

<div class="post-metadata">

**Author:** ![DiamondDog](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DiamondDog](https://forum.kirupa.com/u/DiamondDog)\
**Post date:** [June 11, 2008, 7:08pm UTC](https://forum.kirupa.com/t/beginners-question-targeting-clips-on-stage-from-a-class/262972/4 "2008-06-11T19:08:42Z")

</div>

No, I’m sure it’s not a wrong way of thinking.

If mBox is simply a symbol on the stage, and you want to create an instance of BoxMove which is then going to do something with mBox (like move it around?) you could pass mBox in as a parameter.

So your code on that frame where mBox appears would look like this

```auto
var MyBoxMove:BoxMove = new BoxMove(mBox);
stop();

```

And then in your BoxMove.as file, your constructor will take a movieclip argument.

```auto
public class BoxMove extends MovieClip
{
public function BoxMove(theClip:MovieClip):void // constructor
{
    theClip.addEventListener( etc etc

} // end constructor

}// end class

```

Hope that makes sense. Tell me if it doesn’t.

---

<div class="post-metadata">

**Author:** ![dthought](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dthought/32/3573_2.png) [@dthought](https://forum.kirupa.com/u/dthought)\
**Post date:** [June 12, 2008, 3:26am UTC](https://forum.kirupa.com/t/beginners-question-targeting-clips-on-stage-from-a-class/262972/5 "2008-06-12T03:26:09Z")

</div>

Your listening function also needs to accept an Event as a parameter.

```auto
public function onFrameLoop(evt:Event):void
{
     trace (evt.target.name);
}

```

---

<div class="post-metadata">

**Author:** ![0L4F](https://avatars.discourse-cdn.com/v4/letter/0/848f3c/32.png) [@0L4F](https://forum.kirupa.com/u/0L4F)\
**Post date:** [June 13, 2008, 8:39am UTC](https://forum.kirupa.com/t/beginners-question-targeting-clips-on-stage-from-a-class/262972/6 "2008-06-13T08:39:43Z")

</div>

Yay, thanks guys, that’s it! I got it to work with this code:

```auto

package {

    import flash.display.*;
    import flash.events.*;

    public class BoxMove extends MovieClip {

        public function BoxMove(mc:MovieClip):void {
            mc.addEventListener(Event.ENTER_FRAME,onFrameLoop, false, 0 ,true);
        }

        public function onFrameLoop(evt:Event):void {
            evt.target.x += 1;
        }
    }

}

```

…and then an import in the .fla like this:

```auto

import BoxMove;

var MyBoxMove = new BoxMove(mBox);

stop();

```

Things are now starting to dawn on me a little bit. What I still don’t get though, is why you wouldn’t / shouldn’t put simple code like this on the timeline, like in AS2. Is there any reason, beside good practices?

Thanks again,

1.

---

<div class="post-metadata">

**Author:** ![DiamondDog](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DiamondDog](https://forum.kirupa.com/u/DiamondDog)\
**Post date:** [June 13, 2008, 9:39am UTC](https://forum.kirupa.com/t/beginners-question-targeting-clips-on-stage-from-a-class/262972/7 "2008-06-13T09:39:25Z")

</div>

I think it is just a case of good practice.

With really simple stuff, I don’t see a problem with putting it all on the timeline, but  
things have a habit of getting complicated quickly and that’s when the discipline of writing most of your code in classes is going to become useful.

I’m lucky in the sense that AS3 has been my first introduction to Flash and although the learning curve is steep (and I’m still only at base camp!) at least I’m not having to unlearn AS2.

Good Luck.

---

<div class="post-metadata">

**Author:** ![0L4F](https://avatars.discourse-cdn.com/v4/letter/0/848f3c/32.png) [@0L4F](https://forum.kirupa.com/u/0L4F)\
**Post date:** [June 13, 2008, 2:01pm UTC](https://forum.kirupa.com/t/beginners-question-targeting-clips-on-stage-from-a-class/262972/8 "2008-06-13T14:01:46Z")

</div>

> [@DiamondDog;2342064](#):
>
> … at least I’m not having to unlearn AS2.

Indeed, lucky you! 😉  
I feel like I’m starting from scratch again, where I am able to build CMS-driven Flash sites like this (shameless self-plug):

[http://www.state-xnewforms.nl/main.html](http://www.state-xnewforms.nl/main.html)

in AS2. Very frustrating. But at least, thanks to your help, I’m getting somewhere. Thanks! 🙂

---

<div class="post-metadata">

**Author:** ![DiamondDog](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DiamondDog](https://forum.kirupa.com/u/DiamondDog)\
**Post date:** [June 13, 2008, 2:49pm UTC](https://forum.kirupa.com/t/beginners-question-targeting-clips-on-stage-from-a-class/262972/9 "2008-06-13T14:49:13Z")

</div>

Nice work!

I’m betting it won’t be long before you’re knocking out that sort of thing in AS3.
