A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: how to addressing the MC as a button in AS3

  1. #1
    Senior Member
    Join Date
    Mar 2005
    Posts
    154

    how to addressing the MC as a button in AS3

    Hi...

    Okay I just keep wondering how to address the mcBTN in AS3.0 and assign the action to the button MC....
    eg previously...

    btn.onRelease = Function(){

    gotoAndPlay(framelabel);
    }

    tq

  2. #2
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188
    btn.addEventListener("click",someFunction);

    Note that someFunction should be a function that you can reference afterwards or you won't be able te remove the event listener (which can cause GC problems, apparantly), so don't use an inline function() {} as in your example.

  3. #3
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    When you have a movieclip and want to use it as a button, the cursor will not automatically become a hand. You need to determine that. Here is an example with myBut as MovieClip:
    PHP Code:
    myBut.buttonMode true;// creates the hand cursor
    myBut.addEventListener("click"onClick);
    function 
    onClick (obj:Object)
    {
        
    trace("Hello World");
        
    trace(obj.target);

    The function must always have one argument. The argument refers to the event.
    - The right of the People to create Flash movies shall not be infringed. -

  4. #4
    Senior Member
    Join Date
    Mar 2005
    Posts
    154
    Quote Originally Posted by cancerinform
    When you have a movieclip and want to use it as a button, the cursor will not automatically become a hand. You need to determine that. Here is an example with myBut as MovieClip:
    PHP Code:
    myBut.buttonMode true;// creates the hand cursor
    myBut.addEventListener("click"onClick);
    function 
    onClick (obj:Object)
    {
        
    trace("Hello World");
        
    trace(obj.target);

    The function must always have one argument. The argument refers to the event.
    Thanks for the tips cancerinform....but from what I explore is it I have to put the class I write inside the package..correct me if I'm wrong...so I try with the code u give ..but it seems error..is it I have to put inside package..as well..(just wanna ******** this confuse)
    ...by the way thanks for ur concern ...

  5. #5
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    No this code is for a MovieClip on stage and the script in a frame on the main timeline. If you want to create a class it is slightly more complex.
    - The right of the People to create Flash movies shall not be infringed. -

  6. #6
    Senior Member
    Join Date
    Mar 2005
    Posts
    154
    Quote Originally Posted by cancerinform
    No this code is for a MovieClip on stage and the script in a frame on the main timeline. If you want to create a class it is slightly more complex.
    Hi..cancerinform...tq for ur explaination...

    So it means I can still code on frame just like usual...Actually i just want to familiarize with AS3...as example shown in flash AS3 docs...
    Code:
    package{
    	import flash.display.Sprite;
    	
    	public class Example extends Sprite{
    		public function Example(){
    			var child:ChildSprite = new ChildSprite();
    			addChild(child);
    //is it for mc symbol I have to addChild also?...
    		}
    	}
    }
    
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    
    class ChildSprite extends Sprite{
    	public function ChildSprite(){
    		graphics.beginFill(0xFF0000);
    		graphics.drawRect(0,0,100,100);
    		graphics.endFill();
    		addEventListener(MouseEvent.CLICK,_listener.clickHandler);
    	}
    }
    okay..what I want to know is how to addChild for MC symbol on stage...then assign onClick...hope someone can give some shed on light on it...

    Thanks

  7. #7
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    You have 2 possibilities.
    1. Create a movie with a movieclip in the library and link it to the class ChildSprite.
    PHP Code:
    package
    {
        
    import flash.display.MovieClip;
        public class 
    ChildSprite extends MovieClip
        
    {
            public function 
    ChildSprite ()
            {
                
    this.buttonMode true;
                
    this.addEventListener("click"onClick);
            }
            private function 
    onClick (obj:Object)
            {
                
    trace("Hello World");
            }
        }

    Then create a document class.
    PHP Code:
    package
    {
        
    import flash.display.MovieClip;
        
    import ChildSprite;
        public class 
    Example extends MovieClip
        
    {
            private var 
    myBut:MovieClip;
            public function 
    Example()
            {
                var 
    myBut:ChildSprite = new ChildSprite();
                
    addChild(myBut);
                
    myBut.100;
                
    myBut.100;
            }
        }

    In the Document class field in the fla add: Example and test movie.

    2. Create a movie with a movieclip in the library and link it to the class ChildSprite_2, which is basically empty.
    PHP Code:
    package
    {
        
    import flash.display.MovieClip;
        public class 
    ChildSprite_2 extends MovieClip
        
    {
            public function 
    ChildSprite_2 ()
            {
            }
        }

    Then create a document class.
    PHP Code:
    package
    {
        
    import flash.display.MovieClip;
        
    import ChildSprite_2;
        public class 
    Example_2 extends MovieClip
        
    {
            private var 
    myBut:MovieClip;
            public function 
    Example_2 ()
            {
                var 
    myBut:ChildSprite_2 = new ChildSprite_2 ();
                
    addChild(myBut);
                
    myBut.100;
                
    myBut.100;
                
    myBut.buttonMode true;
                
    myBut.addEventListener("click"onClick);
            }
            private function 
    onClick (obj:Object)
            {
                
    trace("Hello World");
            }
        }

    This last example is closer to the original stage example.

    A third possibility is this to mark the movieclip as button.
    PHP Code:
    package
    {
        
    import flash.display.MovieClip;
        public class 
    ChildSprite_2 extends MovieClip
        
    {
            public function 
    ChildSprite_2 ()
            {
                
    this.buttonMode true;
            }
        }

    and then an above script as document class without the buttonmode.
    Last edited by cancerinform; 08-22-2006 at 08:10 AM.
    - The right of the People to create Flash movies shall not be infringed. -

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