• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

load data and image in AS3

I have these two scripts, both work fine, but I was wondering is there a way to load a data and an image from one script, instead of two like I am doing?

I am trying to consolidate code here.
var request:URLRequest = new URLRequest ("scripts/welcome.php");
request.method = URLRequestMethod.POST;
request.data = true; //POST requests require non-null data
		
var variables:URLVariables = new URLVariables();
variables.username = "username";	
request.data = variables;
				
var loader:URLLoader = new URLLoader (request);
loader.addEventListener(Event.COMPLETE, onComplete);
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(request);
loader.data = true; //POST requests require non-null data

function onComplete (event:Event):void{
username.text = event.target.data.username;
}


///////////////////LOADS IMAGE//////////////////////////////
// create the object to get the return from the php script
var dataLoader:URLLoader = new URLLoader();
// add a handler for when it has the info
dataLoader.addEventListener(Event.COMPLETE, dataLoaded);
// create the php request
var dataRequest:URLRequest = new URLRequest("scripts/welcome.php");
// load it up
dataLoader.load(dataRequest);

// when the data is loaded, fire this event
function dataLoaded(event:Event):void{
  // the data property of the event target will have the text output by the php file
  var path:String = event.target.data;
  // create the request now that you know what to load
  var imageRequest:URLRequest = new URLRequest(path);
  // load it up
  imageLoader.load(imageRequest);
}

// create the loader object to load the image, once we know what image to load
var imageLoader:Loader = new Loader();
// add a handler for when the image is done loading
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
// define the function to fire once the image is loaded
function imageLoaded(event:Event):void{
  // and assuming you want it porportional...
  //imageLoader.scaleY = imageLoader.scaleX;
   // looks like you want it 100px wide
var imageLoader:Bitmap = event.target.content;
 addChild(imageLoader);
 
  imageLoader.smoothing = true;
  imageLoader.x = 6;
  imageLoader.y = 47;
  imageLoader.width = 110;
  imageLoader.height = 110;
  }

Open in new window

0
g0mab2
Asked:
g0mab2
  • 2
  • 2
1 Solution
 
CarnouCommented:
Your code looks pretty good as it is.  You have to load the image after you load the data, because the image path is returned from the data.  Your image loading and data loading go through different processes in different ways.  These facts tell me that they should be totally different code.  Any further consolidation will actually make it harder to understand the program flow.  I suppose you could have a single event listener for complete, switch on e.target, but then you just get a bunch of if statements.  It would be something like this beneath your LOADS IMAGE line (warning, untested code):
///////////////////LOADS IMAGE//////////////////////////////
// create the object to get the return from the php script
dataLoader.addEventListener(Event.COMPLETE, loadComplete);
// create the php request
var dataRequest:URLRequest = new URLRequest("scripts/welcome.php");
// load it up
dataLoader.load(dataRequest);

// create the loader object to load the image, once we know what image to load
var imageLoader:Loader = new Loader();
// add a handler for when the image is done loading
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);

function loadComplete(event:Event):void{
  if (e.target == dataLoader)
  {
    // the data property of the event target will have the text output by the php file
    var path:String = event.target.data;
    // create the request now that you know what to load
    var imageRequest:URLRequest = new URLRequest(path);
    // load it up
    imageLoader.load(imageRequest);
  }
  else if (e.target ==imageLoader)
  {
    // and assuming you want it porportional...
    //imageLoader.scaleY = imageLoader.scaleX;
    // looks like you want it 100px wide
   var imageLoader:Bitmap = event.target.content;
   addChild(imageLoader);

   imageLoader.smoothing = true;
   imageLoader.x = 6;
   imageLoader.y = 47;
   imageLoader.width = 110;
   imageLoader.height = 110;
  }
}
This setup, in my opinion, adds more complexity, rather than reducing it.
If you want to save 2 single lines, you can combine your URLRequest and load lines, e.g.
dataLoader.load( new URLRequest("scripts/welcome.php" );
Generally, my aesthetic rule for coding is to keep it simple.  Smaller functions are better than longer.  Never sacrifice readability to save a simple line or two - months or years down the line, when you're reading the code wondering what you were thinking, you'll appreciate those extra few lines.
0
 
g0mab2Author Commented:
If this is the case, how do I load an image based on the return value of the username?
0
 
CarnouCommented:
Sorry, I was trying to keep my post small by only showing my changes - you would still need lines 1-19 from your post above.  What I posted was mainly to show you that there aren't really any ways to make your code more elegant than it already is.
0
 
g0mab2Author Commented:
I just figured it out thanks for the help.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now