google mapでマ○オカート

2011/03/23  as3.0

google map3dの上で、何か動かしたら楽しいなーと思いまして、

マ○オカートを作りました。

 

仕様には、数秒に一度、現在地をつぶやき続けるだけです。

new mario()はライブラリにあらかじめ置いてあります!(ふきだしのSpriteもかな??)

車の揺れはmovieclipで表現してます。

↑:前進

↓:バック

→、←:曲がる

スペース:ジャンプ

キーボード操作が聞かない場合は、一度flashエリアをクリックしてください。

This movie requires Flash Player 9.0.0

 

何か問題あれば、shun[at]prfry.netまで。

 

 

ファイルはアップできないですがソースは↓です。

package {

	import com.google.maps.*;
	import com.google.maps.controls.*;
	import com.google.maps.overlays.*;
	import com.google.maps.geom.*;
	import com.google.maps.services.*;
	import flash.utils.Timer;

	import flash.display.*;
	import flash.events.*;
	import flash.system.*;
	import flash.geom.*;
	import flash.ui.Keyboard;

	import caurina.transitions.*;

	public class map3d extends Sprite{

		private const API_KEY:String = "ABQIAAAAedQ3nAkgMbszP09U7ilbCRQVeq2w2sZpI_9cEfXv_GqR_rj1-hQH1Zh2pdN5AvGd3SjL_R2v4wczkg"
		private var _map3d:Map3D;
		private var _positionObject:Object={x:35.658517,y:139.701334};

		private var _v:Number = 0.00005;
		private var _r:Number = 0;
		var _mario:MovieClip = new Mario();

		private var _geoClient:ClientGeocoder

		public function map3d(){
			firstFunc();
			_map3d = new Map3D();
			_map3d.key = API_KEY;
			_map3d.sensor = "false";
			_map3d.setSize(new Point(stage.stageWidth, stage.stageHeight));
			_map3d.addEventListener(MapEvent.MAP_READY, mapReadyHandler);
			var _mapOption:MapOptions = new MapOptions();
			_mapOption.zoom = 18;
			_mapOption.center = new LatLng(35.658517, 139.701334);
			_mapOption.viewMode = View.VIEWMODE_PERSPECTIVE;
			_mapOption.attitude = new Attitude(0, 65, 0);
			_map3d.setInitOptions(_mapOption);
			addChild(_map3d);
			stage.addEventListener(Event.RESIZE, stageResizeHandler);
		}

		private function firstFunc():void {
			stage.frameRate=60;
			stage.quality=StageQuality.HIGH;
			stage.align=StageAlign.TOP_LEFT;
			stage.scaleMode=StageScaleMode.NO_SCALE;
			stage.stageFocusRect = false;
		}

		private function stageResizeHandler(e:Event):void {
			_map3d.setSize(new Point(stage.stageWidth, stage.stageHeight));
			_mario.x = stage.stageWidth / 2;
			_mario.y = stage.stageHeight / 2;
		}

		private function mapReadyHandler(e:MapEvent):void {
			addChild(_mario);
			_mario.x = stage.stageWidth / 2;
			_mario.y = stage.stageHeight / 2;

			_geoClient = new ClientGeocoder();
			var LcClientGeoOpt  :ClientGeocoderOptions = new ClientGeocoderOptions;
			LcClientGeoOpt.language = "ja";
			LcClientGeoOpt.viewport = _map3d.getLatLngBounds();
			_geoClient.setOptions(LcClientGeoOpt);
			_geoClient.reverseGeocode(_map3d.getCenter());
			_geoClient.addEventListener(GeocodingEvent.GEOCODING_SUCCESS, onGeocoding_success);
			_geoClient.addEventListener(GeocodingEvent.GEOCODING_FAILURE, onGeocoding_fail);
			_map3d.removeEventListener(MapEvent.MAP_READY, mapReadyHandler);
			stage.addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDownHandler);
			stage.addEventListener(KeyboardEvent.KEY_UP, KeyUpHandler);
		}

		private function onGeocoding_fail(e:GeocodingEvent):void {
			var LcClientGeoOpt  :ClientGeocoderOptions = new ClientGeocoderOptions;
			LcClientGeoOpt.language = "ja";
			LcClientGeoOpt.viewport = _map3d.getLatLngBounds();
			_geoClient.setOptions(LcClientGeoOpt);
			_geoClient.reverseGeocode(_map3d.getCenter());
		}

		private function onGeocoding_success(e:GeocodingEvent):void {
			var LcLatLng:LatLng;
			var sLcAddress  :String;
			LcLatLng = _map3d.getCenter();
			sLcAddress ="現在地"+"\n" + e.response.placemarks[0].address //+ "\n" + "緯度=" + LcLatLng.lat().toString() + "\n" + "経度=" + LcLatLng.lng().toString(); 
			if(_mario.__f.__area.__t.text != sLcAddress){
				_mario.__f.__area.__t.text = sLcAddress;
				Tweener.addTween(_mario.__f, { scaleX:0, scaleY:0 } );
				Tweener.addTween(_mario.__f.__area, { alpha:0 } );
				Tweener.addTween(_mario.__f, { scaleX:1, scaleY:1, transition:"easeOutElastic", time:1.2 } );
				Tweener.addTween(_mario.__f.__area, { alpha:1,transition:"linear",time:0.4,delay:0.8 } );
			}
			/*_map3d.openInfoWindow(LcLatLng, new InfoWindowOptions( { title: "住所は...", content: sLcAddress } )); 
			*/
			var LcClientGeoOpt  :ClientGeocoderOptions = new ClientGeocoderOptions;
			LcClientGeoOpt.language = "ja";
			LcClientGeoOpt.viewport = _map3d.getLatLngBounds();
			_geoClient.setOptions(LcClientGeoOpt);

			var _timer:Timer = new Timer(2000, 1);
			_timer.addEventListener(TimerEvent.TIMER_COMPLETE, function() {
				_geoClient.reverseGeocode(_map3d.getCenter());
			})
			_timer.start();
		}

		var _upFlag:Boolean = false;
		var _downFlag:Boolean = false;
		var _leftFlag:Boolean = false;
		var _rightFlag:Boolean = false;

		// キーボードが押されたときに呼び出されるイベント
		function KeyDownHandler(e:KeyboardEvent):void{
			if(e.keyCode == Keyboard.UP){
				_upFlag = true;
			}else if (e.keyCode == Keyboard.DOWN) {
				_downFlag = true;
			}else if (e.keyCode == Keyboard.LEFT) {
				_leftFlag = true;
			}else if (e.keyCode == Keyboard.RIGHT) {
				_rightFlag = true;
			}
			if(e.keyCode == Keyboard.SPACE) {
				_mario.gotoAndPlay("jump")
			}
		}

		private function KeyUpHandler(e:KeyboardEvent):void {
			if(e.keyCode == Keyboard.UP){
				_upFlag = false;
			}else if (e.keyCode == Keyboard.DOWN) {
				_downFlag = false;
			}else if (e.keyCode == Keyboard.LEFT) {
				_leftFlag = false;
			}else if (e.keyCode == Keyboard.RIGHT) {
				_rightFlag = false;
			}
		}

		private function onEnterFrameHandler(e:Event):void{
			if (_upFlag)_v += 0.00001;
			if (_downFlag)_v -= 0.000005;
			if (_leftFlag)_r -= 1;
			if (_rightFlag)_r += 1;
			_v *= 0.98;
			_positionObject.x += _v * Math.cos(2 * Math.PI / 360 * _r);
			_positionObject.y += _v * Math.sin(2 * Math.PI / 360 * _r);
			_map3d.setAttitude(new Attitude(_r, 65, 0));
			_map3d.setCenter(new LatLng(_positionObject.x, _positionObject.y));
		}
	}

}