Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

haXe MochiAds (See related posts)

   1  
   2  /*
   3     Mochiads.com ActionScript 3 code, version 1.5
   4  
   5     Flash movies should be published for Flash 9 or later.
   6  
   7     Copyright(C) 2006-2007 Mochi Media, Inc. All rights reserved.
   8   */
   9  
  10  import flash.system.Security;
  11  import flash.display.MovieClip;
  12  import flash.display.Loader;
  13  import flash.events.Event;
  14  import flash.events.IOErrorEvent;
  15  import flash.net.URLRequest;
  16  import flash.net.URLRequestMethod;
  17  import flash.net.URLVariables;
  18  import flash.net.LocalConnection;
  19  import flash.Lib;
  20  import Type;
  21  import StringTools;
  22  
  23  class Mochiad{
  24  
  25  	public static function getVersion(): String{
  26  		return "1.5";
  27  	}
  28  
  29  	public static function doOnEnterFrame(mc: Dynamic) {
  30  		var f: Dynamic;
  31  		f = function(ev: Dynamic) {
  32  			if(mc.onEnterFrame != null){
  33  				mc.onEnterFrame();
  34  			} else{
  35  				mc.removeEventListener(Event.ENTER_FRAME, f);
  36  			}
  37  
  38  		}
  39  		mc.addEventListener(Event.ENTER_FRAME, f);
  40  	}
  41  
  42  	public static function createEmptyMovieClip(parent: Dynamic, name: String, depth: Float): MovieClip{
  43  		var mc: MovieClip = new MovieClip();
  44  		if(false){ //&& depth)  //////////// what does "false && depth" mean?
  45  			parent.addChildAt(mc, depth);
  46  		} else{
  47  			parent.addChild(mc);
  48  		}
  49  		Reflect.setField(parent, name, mc);
  50  		Reflect.setField(mc, "_name", name);
  51  		return mc;
  52  	}
  53  
  54  
  55  	public static function showPreloaderAd(options: Dynamic){
  56  		/*
  57  		   This function will stop the clip, load the Mochiad in a
  58  		   centered position on the clip, and then resume the clip
  59  		   after a timeout or when this movie is loaded, whichever
  60  		   comes first.
  61  
  62  options: 
  63  An Dynamic with keys and values to pass to the server.
  64  These options will be passed to Mochiad.load, but the
  65  following options are unique to showPreloaderAd.
  66  
  67  clip is a MovieClip reference to place the ad in.
  68  clip must be dynamic.
  69  
  70  ad_timeout is the Float of milliseconds to wait
  71  for the ad to start loading(default:  2000).
  72  
  73  color is the color of the preloader bar
  74  as a Float(default:  0xFF8A00)
  75  
  76  background is the inside color of the preloader
  77  bar as a Float(default:  0xFFFFC9)
  78  
  79  outline is the outline color of the preloader
  80  bar as a Float(default:  0xD58B3C)
  81  
  82  fadeout_time is the Float of milliseconds to
  83  fade out the ad upon completion(default:  250).
  84  
  85  ad_started is the function to call when the ad
  86  has started(may not get called if network down)
  87  (default:  function(){ this.clip.stop() }).
  88  
  89  ad_finished is the function to call when the ad
  90  has finished or could not load
  91  (default:  function(){ this.clip.play() }).
  92  		 */
  93  		var DEFAULTS: Dynamic = {
  94  			clip:  Lib.current,
  95  			ad_timeout:  3000,
  96  			fadeout_time:  250,
  97  			regpt:  "o",
  98  			method:  "showPreloaderAd",
  99  			color:  0xFF8A00,
 100  			background:  0xFFFFC9,
 101  			outline:  0xD58B3C,
 102  			ad_started:  function(){Lib.current.stop();},
 103  			ad_finished:  function(){Lib.current.play();}
 104  		};
 105  
 106  		options = Mochiad.parseOptions(options, DEFAULTS);
 107  
 108  		var clip: Dynamic = options.clip;
 109  		var ad_msec: Float = 11000;
 110  		var ad_timeout: Float = options.ad_timeout;
 111  		options.ad_timeout = null;
 112  		var fadeout_time: Float = options.fadeout_time;
 113  		options.fadeout_time = null;
 114  
 115  		if(Mochiad.load(options) == null){
 116  			options.ad_finished();
 117  			return;
 118  		}
 119  
 120  		options.ad_started();
 121  
 122  		var mc: Dynamic = clip._mochiad;
 123  		mc.onUnload = function(){
 124  			options.ad_finished();
 125  		}
 126  
 127  		/* Center the clip */
 128  
 129  		var wh: Array<Dynamic> = Mochiad.getRes(options, clip);
 130  
 131  		var w: Float = wh[0];
 132  		var h: Float = wh[1];
 133  		mc.x = w * 0.5;
 134  		mc.y = h * 0.5;
 135  
 136  		var chk: Dynamic = createEmptyMovieClip(mc, "_mochiad_wait", 3);
 137  		chk.x = w * -0.5;
 138  		chk.y = h * -0.5;
 139  
 140  		var bar: MovieClip = createEmptyMovieClip(chk, "_mochiad_bar", 4);
 141  		bar.x = 10;
 142  		bar.y = h - 20;
 143  
 144  		var bar_color: Float = options.color;
 145  		options.color = null;
 146  		var bar_background: Float = options.background;
 147  		options.background = null;
 148  		var bar_outline: Float = options.outline;
 149  		options.outline = null;
 150  
 151  		var backing_mc: MovieClip = createEmptyMovieClip(bar, "_outline", 1);
 152  		var backing: Dynamic = backing_mc.graphics;
 153  
 154  		backing.beginFill(bar_background);
 155  		backing.moveTo(0, 0);
 156  		backing.lineTo(w - 20, 0);
 157  		backing.lineTo(w - 20, 10);
 158  		backing.lineTo(0, 10);
 159  		backing.lineTo(0, 0);
 160  		backing.endFill();
 161  
 162  		var inside_mc: MovieClip = createEmptyMovieClip(bar, "_inside", 2);
 163  		var inside: Dynamic = inside_mc.graphics;
 164  		inside.beginFill(bar_color);
 165  		inside.moveTo(0, 0);
 166  		inside.lineTo(w - 20, 0);
 167  		inside.lineTo(w - 20, 10);
 168  		inside.lineTo(0, 10);
 169  		inside.lineTo(0, 0);
 170  		inside.endFill();
 171  		inside_mc.scaleX = 0;
 172  
 173  		var outline_mc: MovieClip = createEmptyMovieClip(bar, "_outline", 3);
 174  		var outline: Dynamic = outline_mc.graphics;
 175  		outline.lineStyle(0, bar_outline, 100);
 176  		outline.moveTo(0, 0);
 177  		outline.lineTo(w - 20, 0);
 178  		outline.lineTo(w - 20, 10);
 179  		outline.lineTo(0, 10);
 180  		outline.lineTo(0, 0);
 181  
 182  		chk.ad_msec = ad_msec;
 183  		chk.ad_timeout = ad_timeout;
 184  		chk.started = Lib.getTimer();
 185  		chk.showing = false;
 186  		chk.last_pcnt = 0.0;
 187  		chk.fadeout_time = fadeout_time;
 188  
 189  		chk.fadeFunction = function(){
 190  			var p: Float = 100 *(1 - ((Lib.getTimer() - chk.fadeout_start) / chk.fadeout_time));
 191  
 192  			if(p > 0){
 193  				chk.parent.alpha = p * 0.01;
 194  			} else{
 195  				var _clip: MovieClip = chk.parent.parent;
 196  				Mochiad.unload(_clip);
 197  				chk.onEnterFrame = null;
 198  			}
 199  		};
 200  
 201  		mc.unloadAd = function(){
 202  			Mochiad.unload(clip);
 203  		}
 204  
 205  		mc.adjustProgress = function(msec: Float){
 206  			var _chk: Dynamic = mc._mochiad_wait;
 207  			_chk.server_control = true;
 208  			_chk.started = Lib.getTimer();
 209  			_chk.ad_msec = msec;
 210  		};
 211  
 212  		chk.onEnterFrame = function(){
 213  			var _clip: Dynamic = chk.parent.parent.root;
 214  			if(!_clip){
 215  				chk.onEnterFrame = null;
 216  				return;
 217  			}
 218  			var ad_clip: Dynamic = chk.parent._mochiad_ctr;
 219  			var elapsed: Float = Lib.getTimer() - chk.started;
 220  			var finished: Bool = false;
 221  			var clip_total: Float = _clip.loaderInfo.bytesTotal;
 222  			var clip_loaded: Float = _clip.loaderInfo.bytesLoaded;
 223  			var clip_pcnt: Float = (100.0 * clip_loaded) / clip_total;
 224  			var ad_pcnt: Float = (100.0 * elapsed) / chk.ad_msec;
 225  			var _inside: Dynamic = chk._mochiad_bar._inside;
 226  			//var pcnt: Float = Math.min(100.0, Math.min((clip_pcnt || 0.0), ad_pcnt)); // what is "clip_pcnt || 0.0"?
 227  			var pcnt: Float = Math.min(100.0, Math.min((clip_pcnt), ad_pcnt));
 228  			pcnt = Math.max(chk.last_pcnt, pcnt);
 229  			chk.last_pcnt = pcnt;
 230  			_inside.scaleX = pcnt * 0.01;
 231  
 232  			if(!chk.showing){
 233  				var total: Float = ad_clip.loaderInfo.bytesTotal;
 234  				if(total > 0 || Type.typeof(total) == ValueType.TUnknown){
 235  					chk.showing = true;
 236  					chk.started = Lib.getTimer();
 237  				}else if(elapsed > chk.ad_timeout){
 238  					finished = true;
 239  				}
 240  			}
 241  
 242  			if(elapsed > chk.ad_msec || chk.parent._mochiad_ctr_failed){
 243  				finished = true;
 244  			}
 245  
 246  			if(clip_total > 0 && clip_loaded >= clip_total && finished){
 247  				if(chk.server_control){
 248  					chk.onEnterFrame = null;
 249  				}else{
 250  					chk.fadeout_start = Lib.getTimer();
 251  					chk.onEnterFrame = chk.fadeFunction;
 252  				}
 253  			}
 254  		};
 255  		doOnEnterFrame(chk);
 256  	}
 257  
 258  	public static function load(options: Dynamic): MovieClip{
 259  		/*
 260  		   Load a Mochiad into the given MovieClip
 261  				options: 
 262  				An Dynamic with keys and values to pass to the server.
 263  
 264  				clip is a MovieClip reference to place the ad in.
 265  
 266  				id should be the unique identifier for this Mochiad.
 267  
 268  				server is the base URL to the Mochiad server.
 269  
 270  				res is the resolution of the container clip or movie
 271  				as a string, e.g. "500x500"
 272  
 273  				no_page disables page detection.
 274  		 */
 275  		var DEFAULTS: Dynamic = {
 276  			server: "http://x.mochiads.com/srv/1/",
 277  			method: "load",
 278  			depth: 10333,
 279  			id: "_UNKNOWN_"
 280  		};
 281  		options = Mochiad.parseOptions(options, DEFAULTS);
 282  		// This isn't accessible yet for some reason: 
 283  		// options.clip.loaderInfo.swfVersion;
 284  		options.swfv = 9;
 285  		options.mav = Mochiad.getVersion();
 286  
 287  		var clip: Dynamic = options.clip;
 288  
 289  		if(!(Security.sandboxType != "localWithFile")){
 290  			return null;
 291  		}
 292  
 293  		if(clip._mochiad_loaded != null && clip._mochiad_loaded){
 294  			return null;
 295  		}
 296  
 297  		var depth: Float = options.depth;
 298  		options.depth = null;
 299  		var mc: Dynamic = createEmptyMovieClip(clip, "_mochiad", depth);
 300  
 301  		var wh: Array<Int> = Mochiad.getRes(options, clip);
 302  		options.res = wh[0] + "x" + wh[1];
 303  
 304  		options.server += options.id;
 305  		options.id = null;
 306  
 307  		clip._mochiad_loaded = true;
 308  
 309  		var lv: Dynamic = new URLVariables();
 310  		for(k in Reflect.fields(options)){
 311  			var v: Dynamic = Reflect.field(options,k);
 312  			if(!(Reflect.isFunction(v))){
 313  				Reflect.setField(lv,k,v);
 314  			}
 315  		}
 316  
 317  		if(clip.loaderInfo.loaderURL.indexOf("http") != 0){
 318  			options.no_page = true;
 319  		}
 320  
 321  		var server: String = lv.server;
 322  		lv.server = null;
 323  		var hostname: String = allowDomains(server);
 324  
 325  		mc.onEnterFrame = function(){
 326  			if(!mc._mochiad_ctr){
 327  				mc.onEnterFrame = null;
 328  				Mochiad.unload(mc.parent);
 329  			};
 330  		};
 331  		doOnEnterFrame(mc);
 332  
 333  		var lc: LocalConnection = new LocalConnection();
 334  		lc.client = mc;
 335  		var name: String = [
 336  			"", Math.floor((Date.now()).getTime()), Math.floor(Math.random() * 999999)
 337  			].join("_");
 338  		lc.allowDomain("*", "localhost");
 339  		lc.allowInsecureDomain("*", "localhost");
 340  		lc.connect(name);
 341  		mc.lc = lc;
 342  		lv.lc = name;
 343  
 344  		lv.st = Lib.getTimer();
 345  		var loader: Loader = new Loader();
 346  
 347  		var f: Dynamic = function(ev: Dynamic){
 348  			mc._mochiad_ctr_failed = true;
 349  		}
 350  		loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, f);
 351  
 352  		var req: URLRequest = new URLRequest(server + ".swf");
 353  		req.contentType = "application/x-www-form-urlencoded";
 354  		req.method = URLRequestMethod.POST;
 355  		req.data = lv;
 356  		var context = new flash.system.LoaderContext(true);
 357  		loader.load(req, context);
 358  		mc.addChild(loader);
 359  		mc._mochiad_ctr = loader;
 360  
 361  		return mc;
 362  	}
 363  
 364  
 365  	public static function unload(clip: Dynamic): Bool{
 366  		/*
 367  		   Unload a Mochiad from the given MovieClip
 368  
 369  clip: 
 370  a MovieClip reference(e.g. this.stage)
 371  		 */
 372  		if(clip.clip && clip.clip._mochiad){
 373  			clip = clip.clip;
 374  		}
 375  		if(!clip._mochiad){
 376  			return false;
 377  		}
 378  		if(clip._mochiad.onUnload){
 379  			clip._mochiad.onUnload();
 380  		}
 381  		clip.removeChild(clip._mochiad);
 382  		clip._mochiad_loaded = null;
 383  		clip._mochiad = null;
 384  		return true;
 385  	}
 386  
 387  	private static function allowDomains(server: String): String{ 
 388  		// I believe this whole function is unnecessary, but am keeping it around anyway.
 389  		var hostname: String = server.split("/")[2].split(": ")[0];
 390  		flash.system.Security.allowDomain("*");
 391  		flash.system.Security.allowDomain(hostname);
 392  		flash.system.Security.allowInsecureDomain("*");
 393  		flash.system.Security.allowInsecureDomain(hostname);
 394  		return hostname;
 395  	}
 396  
 397  	private static function getRes(options: Dynamic, clip: Dynamic): Array<Int>{
 398  		var b: Dynamic = clip.getBounds(clip.root);
 399  		var w: Int = 0;
 400  		var h: Int = 0;
 401  		if(Type.typeof(options.res) != ValueType.TUnknown){
 402  			var xy: Array<Dynamic> = options.res.split("x");
 403  			w = Std.parseInt(xy[0]);
 404  			h = Std.parseInt(xy[1]);
 405  		} else{
 406  			w = b.xMax - b.xMin;
 407  			h = b.yMax - b.yMin;
 408  		}
 409  		if(w == 0 || h == 0){
 410  			w = clip.stage.stageWidth;
 411  			h = clip.stage.stageHeight;
 412  		}
 413  
 414  		return [w, h];
 415  	}
 416  
 417  	public static function parseOptions(options: Dynamic, defaults: Dynamic): Dynamic{
 418  		var optcopy = Reflect.empty();
 419  		var k: String;
 420  		for(k in Reflect.fields(defaults)){
 421  			Reflect.setField(optcopy,k,Reflect.field(defaults,k));
 422  		}
 423  		if(options){
 424  			for(k in Reflect.fields(options)){
 425  				Reflect.setField(optcopy,k,Reflect.field(options,k));
 426  			}
 427  		}
 428  		options = Reflect.field(optcopy,"clip.loaderInfo.parameters.mochiad_options");
 429  		if(options){
 430  			var pairs: Array<String> = options.split("&");
 431  			for(i in 0...pairs.length){
 432  				var kv: Array<String> = pairs[i].split("=");
 433  				Reflect.setField(optcopy,StringTools.htmlUnescape(kv[0]),StringTools.htmlUnescape(kv[1]));
 434  			}
 435  		}
 436  		return optcopy;
 437  	}
 438  
 439  	public static function showTimedAd(options: Dynamic){
 440  		/*
 441  		   This function will stop the clip, load the Mochiad in a
 442  		   centered position on the clip, and then resume the clip
 443  		   after a timeout.
 444  
 445  options: 
 446  An object with keys and values to pass to the server.
 447  These options will be passed to Mochiad.load, but the
 448  following options are unique to showTimedAd.
 449  
 450  clip is a MovieClip reference to place the ad in.
 451  
 452  ad_timeout is the number of milliseconds to wait
 453  for the ad to start loading(default:  2000).
 454  
 455  fadeout_time is the number of milliseconds to
 456  fade out the ad upon completion(default:  250).
 457  		 */
 458  		var DEFAULTS ={
 459  ad_timeout:  2000,
 460  			fadeout_time:  250,
 461  			regpt:  "o",
 462  			method:  "showTimedAd",
 463  			ad_started:  function(){untyped{this.clip.stop();} },
 464  			ad_finished:  function(){untyped{this.clip.play();} }
 465  		};
 466  
 467  		options = Mochiad.parseOptions(options, DEFAULTS);
 468  
 469  		var clip = options.clip;
 470  		var ad_msec = 11000;
 471  		var ad_timeout = options.ad_timeout;
 472  		Reflect.deleteField(options,"ad_timeout");
 473  		var fadeout_time = options.fadeout_time;
 474  		Reflect.deleteField(options,"fadeout_time");
 475  
 476  		if(Mochiad.load(options)==null){
 477  			options.ad_finished();
 478  			return;
 479  		}
 480  
 481  		options.ad_started();
 482  
 483  		var mc = clip._mochiad;
 484  		Reflect.setField(mc,"onUnload", function(){
 485  				options.ad_finished();
 486  				});
 487  
 488  
 489  		/* Center the clip */
 490  		var wh = Mochiad.getRes(options, clip);
 491  		var w = wh[0];
 492  		var h = wh[1];
 493  		mc.x = w * 0.5;
 494  		mc.y = h * 0.5;
 495  
 496  		var chk = createEmptyMovieClip(mc, "_mochiad_wait", 3);
 497  
 498  		untyped{
 499  			chk.ad_msec = ad_msec;
 500  			chk.ad_timeout = ad_timeout;
 501  			chk.started = Lib.getTimer();
 502  			chk.showing = false;
 503  			chk.fadeout_time = fadeout_time;
 504  			chk.fadeFunction = function(){
 505  				var p = 100 *(1 - 
 506  						((Lib.getTimer() - this.fadeout_start) / this.fadeout_time));
 507  				if(p > 0){
 508  					this.parent.alpha = p * 0.01;
 509  				} else{
 510  					var _clip = this.parent.parent;
 511  					Mochiad.unload(_clip);
 512  					Reflect.deleteField(this,"onEnterFrame");
 513  				}
 514  			};
 515  		}
 516  
 517  		mc.unloadAd = function(){
 518  			Mochiad.unload(clip);
 519  		}
 520  
 521  		mc.adjustProgress = function(msec: Float){
 522  			var _chk = mc._mochiad_wait;
 523  			_chk.server_control = true;
 524  			_chk.started = Lib.getTimer();
 525  			_chk.ad_msec = msec - 250;
 526  		};
 527  
 528  		untyped{
 529  			Reflect.setField(chk,"onEnterFrame", function(){
 530  					var ad_clip = this.parent._mochiad_ctr;
 531  					var elapsed = Lib.getTimer() - this.started;
 532  					var finished = false;
 533  					if(!chk.showing){
 534  						var total = ad_clip.loaderInfo.bytesTotal;
 535  						if(total > 0 || Type.typeof(total) == ValueType.TUnknown){
 536  							chk.showing = true;
 537  							chk.started = Lib.getTimer();
 538  						}else if(elapsed > chk.ad_timeout){
 539  							finished = true;
 540  						}
 541  					}
 542  					if(elapsed > chk.ad_msec || this.parent._mochiad_ctr_failed){
 543  						finished = true;
 544  					}
 545  					if(finished){
 546  					if(this.server_control){
 547  					Reflect.deleteField(this,"onEnterFrame");
 548  					} else{
 549  					this.fadeout_start = Lib.getTimer();
 550  					this.onEnterFrame = this.fadeFunction;
 551  					}
 552  					}
 553  			});
 554  		}
 555  		doOnEnterFrame(chk);
 556  
 557  
 558  	}
 559  
 560  	//public static function fetchHighScores(options: Dynamic, callbackObj: Dynamic, ?callbackMethod: Dynamic): Bool{
 561  	/*
 562  	   Fetch the high scores from Mochiads. Returns false if a connection
 563  	   to Mochiads can not be established due to the security sandbox.
 564  
 565  options: 
 566  An object with keys and and values to pass to the
 567  server.
 568  
 569  clip is a MovieClip reference to place the(invisible)
 570  communicator in.
 571  
 572  id should be the unique identifier for this Mochiad.
 573  
 574  callback(scores): 
 575  
 576  scores is an array of at most 50 high scores, highest score
 577  first, with a millisecond epoch timestamp(for the Date
 578  constructor).  [[name, score, timestamp], ...]
 579  	 */
 580  	/*    var lc: Dynamic = Mochiad._loadCommunicator({clip:  options.clip, id:  options.id});
 581  		  if(!lc){
 582  		  return false;
 583  		  }
 584  
 585  		  lc.doSend(['fetchHighScores', options], callbackObj, callbackMethod);
 586  		  return true;
 587  		  }*/
 588  
 589  
 590  	//public static function sendHighScore(options: Dynamic, callbackObj: Dynamic, ?callbackMethod: Dynamic): Bool{
 591  	/*
 592  	   Send a high score to Mochiads. Returns false if a connection
 593  	   to Mochiads can not be established due to the security sandbox.
 594  
 595  options: 
 596  An object with keys and and values to pass to the
 597  server.
 598  
 599  clip is a MovieClip reference to place the(invisible)
 600  communicator in.
 601  
 602  id should be the unique identifier for this Mochiad.
 603  
 604  name is the name to be associated with the high score, e.g.
 605  "Player Name"
 606  
 607  score is the value of the high score, e.g. 100000.
 608  
 609  callback(scores, index): 
 610  
 611  scores is an array of at most 50 high scores, highest score
 612  first, with a millisecond epoch timestamp(for the Date
 613  constructor).  [[name, score, timestamp], ...]
 614  
 615  index is the array index of the submitted high score in
 616  scores, or -1 if the submitted score did not rank top 50.
 617  	 */
 618  	/*var lc: Dynamic = Mochiad._loadCommunicator({clip:  options.clip, id:  options.id});
 619  	  if(!lc){
 620  	  return false;
 621  	  }
 622  
 623  	  lc.doSend(['sendHighScore', options], callbackObj, callbackMethod);
 624  	  return true;
 625  	  }*/        
 626  
 627  	/*public static function _loadCommunicator(options: Dynamic): Dynamic{
 628  	  var DEFAULTS ={
 629  com_server:  "http://x.mochiads.com/com/1/",
 630  method:  "loadCommunicator",
 631  depth:  10337,
 632  id:  "_UNKNOWN_"
 633  };
 634  options = Mochiad.parseOptions(options, DEFAULTS);
 635  options.swfv = 9;
 636  options.mav = Mochiad.getVersion();
 637  
 638  var clip = options.clip;
 639  var clipname: String = '_mochiad_com_' + options.id;
 640  
 641  if(!(Security.sandboxType != "localWithFile")){
 642  return null;
 643  }
 644  
 645  if(Reflect.hasField(clip,clipname)){
 646  return clip.clipname;
 647  }
 648  
 649  var server: String = options.com_server + options.id;
 650  Mochiad.allowDomains(server);
 651  Reflect.deleteField(options,"id");
 652  Reflect.deleteField(options,"com_server");
 653  
 654  var depth = options.depth;
 655  Reflect.deleteField(options,"depth");
 656  var mc: MovieClip = createEmptyMovieClip(clip, clipname, depth);
 657  var lv: URLVariables = new URLVariables();
 658  for(k in Reflect.fields(options)){
 659  Reflect.setField(lv,k, Reflect.field(options,k));
 660  }
 661  
 662  var lc: LocalConnection = new LocalConnection();
 663  lc.client = mc;
 664  var name: String = [
 665  "", Math.floor((Date.now()).getTime()), Math.floor(Math.random() * 999999)
 666  ].join("_");
 667  lc.allowDomain("*", "localhost");
 668  lc.allowInsecureDomain("*", "localhost");
 669  lc.connect(name);
 670  
 671  #if flash9
 672  
 673  untyped
 674  {
 675  mc.name = name;
 676  mc.lc = lc;
 677  lv.lc = name;
 678  mc._id = 0;
 679  mc._queue = [];
 680  mc.rpcResult = function(cb: Dynamic){
 681  
 682  	// __arguments__ is "magic" and may change in later versions of haxe
 683  	// __typeof__ is also "magic"
 684  	untyped
 685  	{
 686  	cb = Std.parseInt(cb.toString());
 687  	var cblst: Array<Dynamic> = mc._callbacks[cb];
 688  	if(__typeof__(cblst) == "undefined"){
 689  	return;
 690  	}
 691  	Reflect.deleteField(mc._callbacks,cb);
 692  	var args: Array<Dynamic> = [];
 693  	for(i in 2...cblst.length){
 694  	args.push(cblst[i]);
 695  	}
 696  	for(i in 1...__arguments__.length){
 697  	args.push(__arguments__[i]);
 698  }
 699  var method : Dynamic = cblst[1];
 700  var obj : Dynamic = cblst[0];
 701  if(obj && __typeof__(method) == "string"){
 702  	method = obj[method];
 703  }
 704  if(__typeof__(method) == "function"){
 705  	method.apply(obj, args);
 706  }
 707  }
 708  }
 709  mc._didConnect = function(endpoint: String){
 710  	Lib.eval("
 711  			mc._endpoint = endpoint;
 712  			var q: Array = mc._queue;
 713  			delete mc._queue;
 714  			var ds: Function = mc.doSend;
 715  			for(var i: Number = 0; i < q.length; i++){
 716  			var item: Array = q[i];
 717  			ds.apply(this, item);
 718  			}
 719  			");
 720  }
 721  mc.doSend = function(args: Array<Dynamic>, cbobj: Dynamic, cbfn: Dynamic){
 722  	Lib.eval("
 723  			if(mc._endpoint == null){
 724  			var qargs: Array = [];
 725  			for(var i: Number = 0; i < arguments.length; i++){
 726  			qargs.push(arguments[i]);
 727  			}
 728  			mc._queue.push(qargs);
 729  			return;
 730  			}
 731  			mc._id += 1;
 732  			var id: Number = mc._id;
 733  			mc._callbacks[id] = [cbobj, cbfn || cbobj];
 734  			var slc: LocalConnection = new LocalConnection();
 735  			slc.send(mc._endpoint, 'rpc', id, args);
 736  			");
 737  }
 738  #end
 739  }
 740  
 741  untyped
 742  {
 743  
 744  	mc._callbacks = Reflect.empty();
 745  	mc._callbacks[0] = [mc, '_didConnect'];
 746  
 747  	lv.st = Lib.getTimer();
 748  	var req: URLRequest = new URLRequest(server + ".swf");
 749  	req.contentType = "application/x-www-form-urlencoded";
 750  	req.method = URLRequestMethod.POST;
 751  	req.data = lv;
 752  	var loader: Loader = new Loader();
 753  	loader.load(req);
 754  	mc.addChild(loader);
 755  	mc._mochiad_com = loader;
 756  }
 757  
 758  return mc;
 759  
 760  }*/
 761  
 762  }
 763  

You need to create an account or log in to post comments to this site.


Click here to browse all 5521 code snippets

Related Posts