# Event handlers and Nested code

**URL:** <https://forum.kirupa.com/t/event-handlers-and-nested-code/224068>\
**Category:** flash\
**Created:** [April 28, 2007, 4:07am UTC](https://forum.kirupa.com/t/event-handlers-and-nested-code/224068 "2007-04-28T04:07:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshchernoff](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joshchernoff/32/1852_2.png) [@joshchernoff](https://forum.kirupa.com/u/joshchernoff)\
**Post date:** [April 28, 2007, 4:07am UTC](https://forum.kirupa.com/t/event-handlers-and-nested-code/224068/1 "2007-04-28T04:07:05Z")

</div>

here is the code  
Code:  
[LEFT]

```auto
 
AS2 Version
onClipEvent (enterFrame) {
        // Remember the scope of your clip it may not be in _root.
        // if the mouse IS over the clip 
    if (this.hitTest(_root._xmouse, _root._ymouse, true)) {
        // if the last frame of the tween hasn't been reached...
        if (this._currentframe < this._totalframes) {
            // keep playing the next frame to the last and stop...
            this.nextFrame();
        }
        // if the mouse is NOT over the clip 
    } else {
        // if we're past the first frame of the tween...
        if (this._currentframe > 1) {
            // play the previous frame until it reaches frame 1 and stop...
            this.prevFrame();
        }
    }
}

```

this will no longer work in AS3 because you can not nest code on movie clips.  
Any Ideas of how to convert to AS3?

```auto

AS3 Version

this.addEventListener (Event.ENTER_FRAME, detectObject);
 function detectObject (e:Event) {
    if (mouseX >= mc.x && mouseX <= mc.width +mc.x && mouseY >= mc.y && mouseY <= mc.height +mc.y) {
        if (mc.currentFrame < mc.totalFrames) {
            mc.nextFrame ();
        }
    } else {
        if (mc.currentFrame > 1) {
            mc.prevFrame ();
        }
    }
}
 

```

I have a close AS3 version but, it’s still not the same as it’s AS2 version:diss:

:::UPDATE::::  
heres the working AS3 version

```auto
// mc is just a movieclip on the stage with a motiontween and a stop(); in the first key frame

this.addEventListener (Event.ENTER_FRAME, detectObject);
function detectObject (e:Event) {
    if (mc.hitTestPoint(mouseX, mouseY,true)) {
        if (mc.currentFrame < mc.totalFrames) {
            mc.nextFrame ();
        }
    } else {
        if (mc.currentFrame > 1) {
            mc.prevFrame ();
        }
    }
}
}

```

[/LEFT]
