ITeye博客

ChinaMap

shine1200 · 2012-01-05 00:59 · 非技术 · Markdown

package { import event.MapEvent;

import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.external.ExternalInterface;
import flash.geom.ColorTransform;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.text.StaticText;
import flash.text.TextField;
import flash.text.TextFormat;

[SWF(width="600",height="500",frameRate="25",backgroundColor="#FFFFFF")]

public class ChinaMap extends Sprite {
	
	private var mapConfig:Object;
	private var mapBackGroud:MapBackgound;
	private var mapArea:MapArea;
	private var mapXML:XMLList;
	private var mapTip:MapTip;
	private var tipShandow:Sprite;
	private var wrapperFunction:String;
	
	public function ChinaMap(){
		mapConfig = new Object();
		mapConfig.title = loaderInfo.parameters.title;
		wrapperFunction = loaderInfo.parameters.jsHandler;
		/*UI*/
		var mapLoading:MapLoading = new MapLoading();
		addChild(mapLoading);
		var xmlLoader:URLLoader = new URLLoader();
		var xmlAdress:String = (loaderInfo.parameters.xmlurl != null)?loaderInfo.parameters.xmlurl:"data/d.xml";
		xmlLoader.addEventListener(Event.COMPLETE,function(e:Event):void{
			mapXML = new XML(e.target.data).area;
			removeChild(mapLoading);
			drawUI();
		});
		xmlLoader.load(new URLRequest(xmlAdress));
	}
	
	private function drawUI():void {
		mapBackGroud = new MapBackgound();
		mapBackGroud.title = (mapConfig.title == null)?"you need set title":mapConfig.title;
		addChild(mapBackGroud);
		mapArea = new MapArea();
		mapArea.x  = mapArea.y = 20;
		addChild(mapArea);
		stopAll(mapArea.map);
		registAction(mapArea.map);
		tipShandow = new Sprite();
		addChild(tipShandow);
		mapTip = new MapTip();
		addChild(mapTip);
		mapTip.visible = false;
	}
	
	private function registAction(c:DisplayObjectContainer):void {
		var me:DisplayObject;
		var meTf:DisplayObject;
		var meTf1:DisplayObject;
		for(var i:uint = 0; i<c.numChildren; i++) {
			me = c.getChildAt(i);
			if(me is MovieClip && me.name != "bg") {
				me.alpha = 0.5;
				f:for each(var node:XML in mapXML){
					if(node.@id == me.name) {
						me.alpha = 1;
						(me as MovieClip).title = node.@title;
						(me as MovieClip).value = node.@value;
						(me as MovieClip).navUrl = node.@url;
						(me as MovieClip).navTarget = node.@target;
						(me as MovieClip).buttonMode = true;
						var color:ColorTransform = new ColorTransform; 

// var color24:Number=0x00 << 16 | Math.random()*0xff << 8 | 0x00; // color.color=color24; chanceColor(color); (me as MovieClip).transform.colorTransform=color; (me as MovieClip).addEventListener(MouseEvent.MOUSE_OVER,mapOverHandler); (me as MovieClip).addEventListener(MouseEvent.MOUSE_OUT,mapOutHandler); (me as MovieClip).addEventListener(MouseEvent.CLICK,mapClipHandler); break f; } }

				for(var k:uint = 0; k<(me as MovieClip).numChildren;k++){
					meTf = (me as MovieClip).getChildAt(k);
					if(meTf is MovieClip){
						meTf1 = (meTf as MovieClip).getChildAt(0);
						if(meTf1 is StaticText){
							(meTf as MovieClip).removeChildAt(0);
						}else{
							if(meTf1 is MovieClip){
								if(me.name=="gansu"){
									for(var j:uint = 0; j<(meTf1 as MovieClip).numChildren;j++){
										var meTf3:DisplayObject = (meTf1 as MovieClip).getChildAt(j);
										if(meTf3 is StaticText){
											(meTf1 as MovieClip).removeChild(meTf3);
										}
									}
								}else{
									var meTf2 :DisplayObject = (meTf1 as MovieClip).getChildAt(0);
									if(meTf2 is StaticText){
										(meTf1 as MovieClip).removeChildAt(0);
									}
								}
							}
						}
					}
				}
			}
		}
		function chanceColor(c:ColorTransform):void{ 
	       var color24:Number=0x00 << 16 | Math.random()*0xff << 8 | 0x00; 
             c.color=color24;
		} 
		function chanceColor11(c:ColorTransform):void{ 
	        var n:Number=Math.random(); 
	        if(n<.3) c.redOffset=255; 
	        else if(n<.6) c.greenOffset=255; 
	        else{ 
	                c.redOffset=255; 
	                c.greenOffset=255; 
	        } 
		} 
		function mapOverHandler(e:MouseEvent):void {
			(e.currentTarget as MovieClip).gotoAndStop(2);
			showTip((e.currentTarget as MovieClip),(e.currentTarget as MovieClip).value);
		}
		function mapOutHandler(e:MouseEvent):void {
			(e.currentTarget as MovieClip).gotoAndStop(1);
			if(mouseX < mapTip.x || mouseX > (mapTip.x+mapTip.width) || mouseY < mapTip.y || mouseY > (mapTip.y+mapTip.height)) {
				hideTip();
			}
		}
		function mapClipHandler(e:MouseEvent):void {
			var me:MovieClip = e.currentTarget as MovieClip;
			if(me.navUrl != null) {
				navigateToURL(new URLRequest(me.navUrl),me.navTarget);
			}
			var clickEvent:MapEvent = new MapEvent(MapEvent.ITEMCLICK,true,true);
			clickEvent.value = me.name;
			dispatchEvent(clickEvent);
			if (ExternalInterface.available) {
				try {
					var t:Object = new Object();
					t.value = me.name;
					ExternalInterface.call(wrapperFunction,t);
				} catch(err:Error) {
					trace(err);
				}
			}
		}
	}
	
	private function showTip(mc:MovieClip,t:String):void {
		mapTip.addEventListener(Event.ENTER_FRAME,moveTip);
		mapTip.visible = true;
		mapTip.t.htmlText = t;
	}
	private function hideTip():void {
		mapTip.visible=false;
		mapTip.t.text="";
		mapTip.removeEventListener(Event.ENTER_FRAME,moveTip);
	}
	
	private function moveTip(e:Event):void {
		e.currentTarget.x = mouseX+10;
		e.currentTarget.y = mouseY+10;
		if((e.currentTarget.x+e.currentTarget.width) > stage.stageWidth) {
			e.currentTarget.x = stage.stageWidth - e.currentTarget.width;
		}
	}
	
	private function stopAll(c:DisplayObjectContainer):void {
		var me:DisplayObject;
		for(var i:uint = 0; i<c.numChildren; i++) {
			me = c.getChildAt(i);
			if(me is MovieClip) {
				(me as MovieClip).stop();
			}
		}
	}
}

}

评论(1)

  1. #1 楼 · shine1200 · 2012-02-09 11:39

    终于能对chinamapforflash地图修根据不同数据展现不同颜色,并支持图例了。

评论功能维护中,当前仅展示历史评论。