xin's blog

Just have a little faith.
  首页  :: 管理

剪辑的类绑定

Posted on 2009-10-31 23:33  greatxin  阅读(221)  评论(0)    收藏  举报

所谓类绑定剪辑,就当作为该剪辑写一个文档类,文档类中的this指向该剪辑.

 //

package  
{
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    
/**
     * 
     * 所谓类绑定剪辑,就当作为该剪辑写一个文档类,文档类中的this指向该剪辑
     * @author chancidal
     
*/
    public class MyButtonBase extends MovieClip
    {
        
//
        private var m_labels:Array = ["over""down""out"];
        
//
        public function MyButtonBase() 
        {
            
this.stop();
            init();
        }
        private 
function init():void {
            addEventListener(MouseEvent.MOUSE_DOWN, dragButton);
            addEventListener(MouseEvent.ROLL_OVER, rollOver);
            addEventListener(MouseEvent.ROLL_OUT, rollOut);
        }
        private 
function dragButton(evt:MouseEvent):void {
            
this.parent.addChildAt(thisthis.parent.numChildren - 1);
            
this.startDrag();
            
for (var i:int = 0; i < m_labels.length; i++) {
                
if (m_labels[i] == "down") {
                    
this.gotoAndStop("down");
                }
            }
            addEventListener(MouseEvent.MOUSE_UP, stopDragButton);
        }
        private 
function stopDragButton(evt:MouseEvent):void {
            
this.stopDrag();
            
for (var i:int = 0; i < m_labels.length; i++) {
                
if (m_labels[i] == "over") {
                    
this.gotoAndStop("over");
                }
            }
            removeEventListener(MouseEvent.MOUSE_UP, stopDragButton);
        }
        private 
function rollOver(evt:MouseEvent):void {
            
for (var i:int = 0; i < m_labels.length; i++) {
                
if (m_labels[i] == "over") {
                    
this.gotoAndStop("over");
                }
            }
        }
        private 
function rollOut(evt:MouseEvent):void {
            
for (var i:int = 0; i < m_labels.length; i++) {
                
if (m_labels[i] == "out") {
                    
this.gotoAndStop("out");
                }
            }
        }
    }

}

 Demo