flash on 2010-1-26

by mmlemon_
♥0 | Line 79 | Modified 2010-01-26 01:51:30 | MIT License
play

ActionScript3 source code

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

package {
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.events.ErrorEvent;
    import flash.display.Sprite;
    import flash.events.Event;
    [SWF(backgrondColor=0x0,frameRate=60)]
    public class FlashTest extends Sprite {
        
        private var m_sub1:SubClass1;
        private var m_tf:TextField;
        
        public function FlashTest() {
            // write as3 code here..
            addEventListener(Event.ADDED_TO_STAGE, init, false, 0, false);
        }
        private function init(event:Event):void
        {
        		removeEventListener(Event.ADDED_TO_STAGE, init, false);
        		// create Subclass1 instance.
        		// SubClass1:gray square  SubClass2:Red square
        		m_sub1 = new SubClass1();
        		m_sub1.y = 40;
        		addChild(m_sub1);
        		
        		
        		// create textfield for debugging.
        		m_tf = new TextField();
        		m_tf.width = 500;
        		m_tf.height = 40;
        		m_tf.wordWrap = true;        		
        		addChild(m_tf);
        		m_tf.text = "text"; 
        		m_sub1.addEventListener(CustomEvent.CUSTOM, customHandler, false, 0, false);
        }
        
        private function customHandler(event:CustomEvent):void
        {
        		m_tf.text = (new Date()).toString() + "customEvent target:" + event.target.toString() + " current:" + event.currentTarget.toString();
        }
    }
}
import flash.display.Sprite;
import flash.events.Event;
import flash.display.Graphics;
import flash.events.MouseEvent;

internal class SubClass1 extends Sprite
{
	
	
	public function SubClass1()
	{
		addEventListener(Event.ADDED_TO_STAGE, init, false, 0, false);
		
		graphics.beginFill(0xcccccc, 1);
		graphics.drawRect(0, 0, 400, 400);
		graphics.endFill();
		
		var sub2:SubClass2 = new SubClass2();
		addChild(sub2);
	}
	
	private function init(event:Event):void
	{
		removeEventListener(Event.ADDED_TO_STAGE, init, false);
	}	
}
internal class SubClass2 extends Sprite
{
	
	public function SubClass2()
	{
		addEventListener(Event.ADDED_TO_STAGE, init, false, 0, false);
	}
	
	private function init(event:Event):void
	{
		graphics.beginFill(0xff0000, 1);
		graphics.drawRect(0, 0, 200, 200);
		graphics.endFill();
		
		addEventListener(MouseEvent.CLICK, clickHandler, false, 0, false);
	}
	
	private function clickHandler(event:MouseEvent):void
	{
		dispatchEvent(new CustomEvent(CustomEvent.CUSTOM, true));
	}
}
internal class CustomEvent extends Event
{
	
	public static const CUSTOM:String = "custom";
	
	public function CustomEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
	{
		super(type, bubbles, cancelable);
	}
}