カスタムイベント

by _wonder
♥0 | Line 44 | Modified 2010-01-12 19:04:30 | MIT License
play

ActionScript3 source code

/**
 * Copyright _wonder ( http://wonderfl.net/user/_wonder )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/y45I
 */

package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;
    
    [SWF( width="465", height="465", backgroundColor="0xffffff", frameRate="30" )]
    public class EventTest extends Sprite {
        private var txt:TextField;
        public function EventTest() {
            stage.addEventListener(Event.ENTER_FRAME, mousePos);
            txt = new TextField();
            txt.text = "defalut";
            addChild(txt);
            
            addEventListener(CustomEvent.UP_EVENT, showUp);
            addEventListener(CustomEvent.DOWN_EVENT, showDown);
        }
        
        private function mousePos(e:Event):void {
            if( stage.mouseX > stage.stageWidth/2 ){
                dispatchEvent(new CustomEvent(CustomEvent.UP_EVENT));
            } else {
                dispatchEvent(new CustomEvent(CustomEvent.DOWN_EVENT));
            }
        }
        
        private function showUp(e:CustomEvent):void {
            txt.text = "right!!!!!!";
        }
        
        private function showDown(e:CustomEvent):void {
            txt.text = "left!!!!!!!";
        }
    }
}

import flash.events.Event;

class CustomEvent extends Event {
    public static const UP_EVENT:String = "upevent";
    public static const DOWN_EVENT:String = "downevent";
    public function CustomEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false){
        super(type, bubbles, cancelable);  
    }
    public override function clone():Event { 
        return new CustomEvent(type, bubbles, cancelable);
    }
    public override function toString():String{ 
        return formatToString("CustomEvent", "type", "bubbles", "cancelable", "eventPhase");
    } 
}

Forked