2 Replies Latest reply on Aug 27, 2006 10:09 AM by WebXperience

    assign ActionScript to mc created by "createEmptyMC"

    bibberlam
      hello,

      I wonder if it is possible to assign actionscript to a movieclip wich is created by the "createEmptyMovieClip("test", 100);"

      the goal of my question is to make this new mc acting like a button: clicking on it must perform an action.

      can you help me out?
      thanks in advance
      dries
        • 1. Re: assign ActionScript to mc created by "createEmptyMC"
          Rothrock Level 5
          Why wouldn't it be. It is an instance of the MovieClip class. There are a couple things you will need to do.

          First here is a neat little trick. The createEmptyMovieClip method of the MovieClip class returns a reference to the new clip you created. For the example you have given here it isn't such a bit deal, butt it is a useful trick and if you learn it now you will find uses for it. So you can assign that name to a variable and use that variable again.

          curClip=this.createEmptyMovieClip("test",100);
          trace("The current clip is: "+curClip);

          If this code is on the main timeline you should see a trace that says:

          The current clip is: _level0.test

          So then you can use that variable to attach the button-like actions:

          curClip.onRelease=function(){
          trace("You've clicked on "+this);
          //otherstuff
          }

          Now the problem with this is something that a lot of folks seem to have a hard time grasping. It is called create EMPTYMovieClip for a reason, because the movieclip is empty and has nothing in it. Therefore there is nothing to onRelease on. So you will need to put something into the clip. A good choice for that is to use the Drawing API.

          Look it up in the help files. It is part of the MovieClip class so you will find it under that. Look for lineTo, moveTo, lineStyle, beginFill, and endFill. So after doing the create part you could do this:

          curClip.beginFill(0xff0000,100);
          curClip.moveTo(0,0);
          curClip.lineTo(0,20);
          curClip.lineTo(20,20);
          curClip.lineTo(20,0);
          curClip.lineTo(0,0);
          curClip.endFill();

          Now there will be a 20 x 20 red square inside the newly created clip. So there will be something to actually click on.
          • 2. Re: assign ActionScript to mc created by "createEmptyMC"
            WebXperience
            As you may already know the syntax for using createMovieClip is:

              path.createEmptyMovieClip(name, depth);

            So, for example: if you if you were to create a MovirClip named box_mc at the root level of your movie the syntax would look like:

            _root.createMovieClip("box_mc", 1);

            Then if you wanted to attach a new property, function, or another movie to it you would use the full pathname:

            level01.box_mc.onRelease () {
            gotAndStop(1); // Or any other instruction you wanted be performed upon pressing this movie which you've now given
            // the ability to respond to a Mouse Event.
            }

            You could also, give it features like onReleaseOutside, onMouseOver, onClick or a slew of other cool mouse type attributesusing the same "full path" method. check out Help and invest in a good ActionScript book!