Skip navigation
Currently Being Moderated

Basic AS3 question...

Oct 8, 2007 12:28 AM

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
 
Replies
  • Currently Being Moderated
    Oct 8, 2007 12:43 AM   in reply to Iron_Mike
    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
     
    |
    Mark as:
  • Currently Being Moderated
    Oct 8, 2007 5:50 AM   in reply to Iron_Mike
    >> 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/


     
    |
    Mark as:
  • Currently Being Moderated
    Oct 8, 2007 10:03 PM   in reply to Iron_Mike
    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);
     
    |
    Mark as:
  • Currently Being Moderated
    Oct 9, 2007 10:05 AM   in reply to Iron_Mike
    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 :)
     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points