mnet flash addition

Monkey Targets Forums/Flash/mnet flash addition

Fryman(Posted 2012) [#1]
I attempted to add a flash target to mnet but I have never programmed in as3 ive had a few difficulties..

I have code which works provided you call it twice, as the first time you call it, it doesn't wait long enough for the process to finish to
return the correct data.

Anyone able to add to this a way of making it wait?

flash.mnet.as

import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;

class mnet{
private static var newstring:String = "";
private static var timeout:int = 5000;

static public function HttpGet(Url:String,timeoutConnection:int, timeoutSocket:int):String{
	var reqURL:URLRequest = new URLRequest(Url);
	var loader:URLLoader = new URLLoader(reqURL);
	loader.addEventListener(Event.COMPLETE, handleComplete);
	loader.addEventListener(ProgressEvent.PROGRESS , progressHandler);
	loader.dataFormat = URLLoaderDataFormat.TEXT;
	timeout = timeoutConnection;
function progressHandler(event:ProgressEvent):void {
//Do nothing while the url is loading

}

function handleComplete( event:Event):void
{
var loader:URLLoader = URLLoader(event.target);
newstring = loader.data;

}

return newstring;
}
}



Beaker(Posted 2012) [#2]
Can't you do something like this?: (untested)
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;

class mnet{
private static var newstring:String = "";
private static var timeout:int = 5000;
public static var isDone:bool = false;

static public function HttpGet(Url:String,timeoutConnection:int, timeoutSocket:int):String{
	isDone = false;
	var reqURL:URLRequest = new URLRequest(Url);
	var loader:URLLoader = new URLLoader(reqURL);
	loader.addEventListener(Event.COMPLETE, handleComplete);
	loader.addEventListener(ProgressEvent.PROGRESS , progressHandler);
	loader.dataFormat = URLLoaderDataFormat.TEXT;
	timeout = timeoutConnection;
function progressHandler(event:ProgressEvent):void {
//Do nothing while the url is loading

}

function handleComplete( event:Event):void
{
var loader:URLLoader = URLLoader(event.target);
newstring = loader.data;
isDone = true;
}

return newstring;
}
}



Fryman(Posted 2012) [#3]
No effect :(


import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;

class mnet{
private static var newstring:String = "";
private static var timeout:int = 5000;
public static var isDone:Boolean = false;

static public function HttpGet(Url:String,timeoutConnection:int, timeoutSocket:int):String{
	isDone = false;
	var reqURL:URLRequest = new URLRequest(Url);
	var loader:URLLoader = new URLLoader(reqURL);
	loader.addEventListener(Event.COMPLETE, handleComplete);
	loader.addEventListener(ProgressEvent.PROGRESS , progressHandler);
	loader.dataFormat = URLLoaderDataFormat.TEXT;
	timeout = timeoutConnection;
function progressHandler(event:ProgressEvent):void {
//Do nothing while the url is loading

}

function handleComplete( event:Event):void
{
var loader:URLLoader = URLLoader(event.target);
newstring = loader.data;
isDone = true;
}

return newstring;
}
}



Beaker(Posted 2012) [#4]
You have to manually check the isDone flag yourself.


Fryman(Posted 2012) [#5]
I've tried doing that but if you stick it in any kind of loop the complete event never gets called..

My next idea is to set a new function to return the data and have it accessed during a future onupdate based on time passed... But my gut tells me this will be unsuccessful..

I also read a forum post with someone having a similar problem and
The suggestion was to change the layout some what I need to read more
Into it

Its really bugging me


Landon(Posted 2014) [#6]
Yea Flash just will not allow you to do it Synchronously, i ended up adding tot he flash version and using the #IF target = "flash" blah blah in my code to handle that situation differently. This was the only way i could get it to work..

mnet.flash.as
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;

class MNet_Http{
private var newstring:String = "";
private var timeout:int = 5000;
private var complete:int = 0;

public function Get(Url:String,timeoutConnection:int, timeoutSocket:int):String{
	var reqURL:URLRequest = new URLRequest(Url);
	var loader:URLLoader = new URLLoader();
	
	
	configureListeners(loader);
	//loader.addEventListener(Event.COMPLETE, handleComplete);
	//loader.addEventListener(ProgressEvent.PROGRESS , progressHandler);
	loader.dataFormat = URLLoaderDataFormat.TEXT;
	timeout = timeoutConnection;
	loader.load(reqURL);

 	
	
	
	return newstring;
}


private function progressHandler(event:ProgressEvent):void {
//Do nothing while the url is loading

}


private function handleComplete( event:Event):void
{
var loader:URLLoader = URLLoader(event.target);
newstring = loader.data;
complete = 1;
}

private function configureListeners(dispatcher:IEventDispatcher):void {
	dispatcher.addEventListener(Event.COMPLETE, handleComplete);
	dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);

}

public function GetComplete():int
{
	return complete;
}

public function GetResult():String
{
	return newstring;
}

public function AGet( Url:String,timeoutConnection:int, timeoutSocket:int ):String
{
	return newstring;
}

}



Then in my code using diddy i'd call it like this

	Method New()
		name = "GameScreen"
		MAINGAME = New GameMode
		#If TARGET="flash"
			If flashvars[1] <> "" Then
				Print "FlashVars: "+flashvars[1]
				
				ht = New Http
				
				ht.Get( flashvars[1], 3000 )

				

			Else
				'MAINGAME.LoadPlay("monkey://data/games/demo1.vcf")
			Endif
		#Else
			MAINGAME.LoadPlay("monkey://data/games/demo1.vcf")	
		#Endif
		MAINGAME.Mode = 2
		MAINGAME.showscore = True
		
	End


	Method Update:Void()
		If loaded =0 Then
		#If TARGET="flash"		
			If ht.GetComplete() > 0 Then
				Print "Loaded"
				Local parser:XMLParser = New XMLParser
				Local xmldoc:XMLDocument = parser.ParseString(ht.GetResult())
				LoadRemotePlaylist(xmldoc,0)			
				loaded = 1
			Endif
		#Endif			
		Endif
		MAINGAME.Update()
		
		If KeyHit(KEY_ESCAPE)
			FadeToScreen(titleScreen)
		End
	End