-1

Is there a way of writing an if statement that involves the following?

if (MovieClip1 reaches last frame)
{
    addChild(MovieClip2)
    removeChild(MovieClip1)
}

Basically, all I want to happen is when my MovieClip finishes, it will change to another MovieClip or image. I know it is probably very simple, but how can I do it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

2 Answers2

1

All code runs on command. So your conditional will run immediately and never run again.

Instead, you use events. You attach a listener to an event and when that event fires (is "dispatched" to use correct vocabulary), your code is called again.

var movieClip:MovieClip = new MovieClip();
movieClip.addEventLister( Event.ENTER_FRAME, this.enterFrameHandler ); //will be called on every frame enter

function enterFrameHandler( e:Event ):void {
    if ( movieClip.currentFrame == movieClip.totalFrames ) {
        // do something
    }
}

So you listen for each new frame and in that handler, you check if it is the last frame or not.

As an extra tidbit, the standard naming convention for AS3 is to use lowercase, camelcase (thisIsAnExampleOfthat) for all objects. Package names should be in all lowercase and only Class names should be capitalized.

Josh
  • 8,079
  • 3
  • 24
  • 49
  • That looks very promising :) I will try it as soon as I can and mark it as correct if it works. Thank you :) – Stylaphone Jul 17 '13 at 11:24
  • ok well I have tried that and I think what you have written is right but something I am doing is wrong? this is what I have written: – Stylaphone Jul 18 '13 at 20:10
0
package
{
    import flash.display.MovieClip;
    import flash.events.KeyboardEvent;
    import flash.events.Event;

    public class Main extends MovieClip
    {
        var firstScene:FirstScene = new FirstScene;
        var movement:Movement = new Movement;
        var green:Green = new Green;

        public function Main()
        {
            addChild(firstScene);

            firstScene.addEventListener(KeyboardEvent.KEY_DOWN, mClick);
            movement.addEventListener(Event.ENTER_FRAME, endChange);
        }
        function mClick(event:KeyboardEvent):void
        {
            if (event.keyCode == 77);
            {
                addChild(movement);
                removeChild(firstScene);
            }
        }
        function endChange(event:Event):void
        {
            if (movement.currentFrame == movement.totalFrames)
            {
                addChild(green);
                removeChild(movement);
            }
        }
    }
}
  • You need to learn how to use access modifiers (public, private, protected, internal, final, static). I can almost guarantee that is what is wrong here. If you are using a class, every function and variable defined outside of a function must have an access modifier. Additionally, you should not instantiate any non-constant/non-basic datatype object outside a function. Beyond those, you definitely should read up on basic OOP (specifically syntax and scope) as it seems like you do not have a very good understanding of it. – Josh Jul 18 '13 at 21:57
  • I'll have a look into it. The output panel says something along the lines of: display object must be a child of caller. – Stylaphone Jul 19 '13 at 11:09