dcsimg
A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Crack this AS 2.0 Class problem

  1. #1
    Senior Member
    Join Date
    Jan 2001
    Location
    London
    Posts
    361

    Crack this AS 2.0 Class problem

    Hi,

    If you can crack this one you're a better man than me.
    My class Menu defines an onRelease handler for a movieclip button on stage. I want a method of the class to be called when the button is clicked.
    code:

    class Menu
    {
    public function Menu()
    {
    init()
    }
    function init()
    {
    menuButton.onRelease = function()
    {
    // How do I call the method clicked() from here??
    }
    }
    function clicked()
    {
    trace("clicked")
    }
    }


    Help would be so appreciated.

    Dene

  2. #2
    Senior Member
    Join Date
    Sep 2000
    Location
    San Francisco
    Posts
    196

    try dis

    Hi,
    I think you were having trouble becuase of 2 things.

    1. the word Menu is a reserved word in flash, try nameing your class something else like CustomMenu

    2. (more important) in your class you are referrring to a button on the main timeline, not in your class, so naturally the button cannot find the clicked method because it does not exsits on the main timeline, if you make the clicked method static, then the button will find the method.

    Code:
    class CustomMenu
    {
    	private var menuButton:Button;
    	
            public function CustomMenu(btn:Button)
            {
                    init(btn);
            }
            function init(btn:Button)
            {
    			menuButton = btn;
                    menuButton.onRelease = function()
                    {
    					
                          CustomMenu.clicked();
                    }
            }
            public static function clicked():Void
            {
                    trace("clicked")
            }
    }
    Then instantiate the menu object by passing it a reference of your button object(assuming it is named myButton) like so:
    Code:
    var myMenu:CustomMenu = new CustomMenu(myButton);
    This is probably not ideal, an alternative would be to have your class write a clicked(); function to the maintime line instead of using a static function, also you don't even need a Button object in your class at all, the below code is probably your best bet:

    Code:
    class CustomMenu
    {
            public function CustomMenu(btn:Button)
            {
                    init(btn);
            }
            function init(btn:Button)
            {
    			_level0.clicked = function(){
    				trace("clicked");
    			}
                   btn.onRelease = function()
                    {
    					
                          _level0.clicked();
                    }
            }
    }
    Good Luck,
    ~Dev

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