A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [FLASH 9] IDE Defined Movieclip with Buttons

  1. #1
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923

    [FLASH 9] IDE Defined Movieclip with Buttons

    I'm in the process of doing the UI for a game, and I have an elegant solution, however there is something stopping me.

    I have a MovieClip in the library with a nice graphical menu, and 1 button 'play'.

    I have linked this MovieClip to a class similar to:

    code:

    package{
    class UIStuff extends MovieClip
    {
    var play:MovieClip; //changing to Button, or Class, or * does not work!
    function UIStuff()
    {
    //here is where I want to add event listeners to the play button
    }
    }
    }



    So this is the problem, I can't seem to make the buttons I have defined in the IDE, link to the code in the class. I'm sure I'm missing something completely obvious.
    Christopher Rhodes
    squarecircleco.

  2. #2
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    Cracked it, you have to define the class as dynamic, and use a reference to the class... It's a hack, but it works. If this is the only solution, this thread is resolved.

    code:

    package{
    dynamic class UIStuff extends MovieClip
    {
    var ui:UIStuff;
    function UIStuff()
    {
    ui = this;
    //Now I can reference ui.play!
    }
    }
    }

    Last edited by iopred; 10-31-2006 at 01:46 AM.
    Christopher Rhodes
    squarecircleco.

  3. #3
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    1. You are not importing the MovieClip class
    2. The class has to be declared as something usually public
    3. play is a reserved word. You cannot use it as a variable name.

    package
    {
    import flash.display.MovieClip;
    public class UIStuff extends MovieClip
    {
    private var myPlay:MovieClip;
    public function UIStuff()
    {
    //here is where I want to add event listeners to the play button
    }
    }
    }
    - The right of the People to create Flash movies shall not be infringed. -

  4. #4
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    As I said, that small snippet was just that, a snippet. As my second post shows, the answer is in having a dynamic class, small quibbles with variable names aside.

    Tomsamson managed to reveal another way to solve the problem, and that is to use:

    getChildByName();

    To grab the children from the MC, it works nicely too if you set all the variables in the constructor.
    Christopher Rhodes
    squarecircleco.

  5. #5
    Senior Member sand858's Avatar
    Join Date
    Aug 2001
    Posts
    327
    Another option is to define a variable in the class with the same name as the child MC in the library and to declare its scope as public.

    I don't know why it works, but it does.

    I believe this is a preferable way to do this because "dynamic" classes are dynamic and therefore much slower than traditional closed classed, not to mention more difficult to code with.
    gamedozer games
    Free multiplayer and singleplayer games

  6. #6
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    play is also a method of MovieClip - you wouldnt want to have definitions within that instance of the same name.

    When you use the timeline to build movie clips, Flash automatically constructs that definition for your instance during initialization prior to the constructor call. Part of this process includes defining instance names of children as properties to your movie clip. It assumes these names are public properties (or accesses them as such) which is why public declarations for these objects work, as sand858 pointed out. Alternatively, as a dynamic class, Flash would naturally have no issues with defining whatever it wants for the class since its opened up.

    If you want a child to be private or internal (default) or otherwise, you would have to define it through code in your class rather than the timeline. However, the instance itself, variable private or not, would still be accessible through the publically available display list... unless of course overridden with your own methods.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center