6 Replies Latest reply on Oct 9, 2007 10:05 AM by clbeech

    Basic AS3 question...

    Iron_Mike
      Hi,

      I'm absolutely new to AS3 and I'm trying to convert my AS2 fla files to AS3 code.

      Now one suggestion I've heard all the time is to keep ur code in one place, preferably at frame 1 of the main timeline.

      Now that sound like a good idea to me, but here's my question:

      Is it possible to assign actions on frame 1 to an object that appears on stage on frame 90 ?

      Assuming this is my simple code on frame 1, 'box' is a movieclip appearing on frame 90:

      Code:

      box.addEventListener(MouseEvent.CLICK, clickHandler);
      box.buttonMode = true;
      function clickHandler(evt:Object):void {
      trace("You just clicked me!");
      }

      I tried that and it didn't work, I had to place that code either on frame 90 or later, when that object already appeared in my flash movieclip...

      --> which means I would have to split up my code for every object that appears later on stage - which is gonna be a lot of objects and then the code is gonna be scattered around like in AS2...

      Is there any way to target that object and keeping my code on frame 1 ?

      As always, thanx for your help and advise in advance !

      Mike
        • 1. Re: Basic AS3 question...
          15DAVE15
          sorry mike
          but with flash there are lots of things to do what u just said makes no sense
          put more description into what your writing next time pleeze
          • 2. Re: Basic AS3 question...
            Level 7
            >> box.addEventListener(MouseEvent.CLICK, clickHandler);
            box.buttonMode = true;
            function clickHandler(evt:Object):void {
            trace("You just clicked me!");
            }

            I tried that and it didn't work, I had to place that code either on frame
            90
            or later, when that object already appeared in my flash movieclip...
            <<

            Right, you can't assign actions to non-existent objects. The better way
            would be to have a function on frame 1, that you pass a clip reference to.
            Something like this:

            function addClick(clipRef:MovieClip):void
            {
            clipRef.addEventListener(MouseEvent.CLICK, clickHandler);
            clipRef.buttonMode = true;
            function clickHandler(evt:Object):void {
            trace("You just clicked me!");
            }
            }


            --
            Dave -
            Head Developer
            http://www.blurredistinction.com
            Adobe Community Expert
            http://www.adobe.com/communities/experts/


            • 3. Re: Basic AS3 question...
              Iron_Mike Level 1
              Dave,

              thanx for your suggestion. What I do not undertand:

              So we're creating this function - I'm assuming 'clipRef' has to be replaced by my instance name of my movieclip - and then u want me to later on call that function or how is this going to work ?

              Thanx for your help.

              Mike
              • 4. Re: Basic AS3 question...
                clbeech Level 3
                Yes Mike, that's just what Dave's saying, your creating a function that you can call from any instance the you pass in this way. so at frame 90 when your object becomes instantiated on the timeline, you would call this function and pass in the instace as a reference, correct. so on f90 call:

                addClick(box);
                • 5. Basic AS3 question...
                  Iron_Mike Level 1
                  clbeech,

                  thanx for the explanation !

                  But the reason why I started this thread is to (try to) keep all of mycode in one place at frame 1 - that doesn't work with the example in my opening message because that object isn't instantiated yet...

                  So with the option u provided I basically write more code and still have to 'activate it' on frame 90...

                  There are much more complicated scenarios where, for example, there is a button nested inside three movieclips and those movieclips always appear 5-10 frames into that parent movieclips timeline, e.g:

                  movieclip 'container1' on main timeline appears on frame 90
                  'container 2' nested inside 'container 1'appears on frame 8
                  'container 3' nested inside 'container 2' appears on frame 6
                  'box' nested inside 'container 3' appears on frame 5

                  --> so in my (newbie) opinion u got three options to address 'box':

                  1. put ur actionscript code on frame 109 (90+8+6+5) on the main timeline (root)

                  --> which is not the solution I want, because then I have my code scattered around

                  2. pur ur AS inside 'container 3' on frame 5

                  --> not what I want, my code would be kinda 'hidden' inside that container - then I could stay with my AS2 file haha ;)

                  3. instantiate that object on frame 1 via action script and then later have it appear on the timeline and access it, unfortunately I have never done that and I tried to it with this:
                  // I assigned 'btn' as the 'linkage' / class in the library for that movieclip
                  // 'flash.display.MovieClip' is the base class
                  // this is AS on frame 1, objects appears on stage on frame 90 with instance name 'box'
                  box=new btn();
                  box.addEventListener(MouseEvent.CLICK, clickHandler);
                  box.buttonMode = true;
                  function clickHandler(evt:Object):void {
                  trace("You just clicked me!");
                  }

                  --> still doesn't work, it doesn't give me an error message when testing my movie but the 'box' movieclip doesn't even have button mode - on roll over you don't get the finger - although I put 'buttonMode= true'...

                  Thanx for your help !

                  Mike

                  • 6. Re: Basic AS3 question...
                    clbeech Level 3
                    Hey Mike, you're welcome :) I'm totally new to AS3 myself, and am just today embarking on my first look even at the manual, whew lots to learn! LOL! but some things stay the same in principle, which is why I though I could chime in here.

                    I understand where you're coming from, the advantage here would be that the function becomes dynamic, and accessible from anywhere within your system, and from any instance in your system. And by passing in a instance ref, you can 're-use' the function without haveing to type it all in each time. But yes you will still have to call the function from the button each time it's brought to the stage, we would still need to do so in AS2 as well, same rules apply.

                    But here's a thought, if the 'container #' is an instance of the same MC, how about placing the calling code within the MC's timeline? that way you wouldn't have to put it on the main timeline where the object is being instantiated, it 'comes with' the MC. Now (I think this still works) the depth of the nested MC won't matter if you use the call like so:

                    _level0.addClick(this);

                    In other words make an actions layer in the container MC and add that line, it will call to the common function on the main timeline, from wherever it is, whenever it's available to the user.

                    Just a thought for you :)