Solved

load data and image in AS3

Posted on 2010-08-26
4
365 Views
Last Modified: 2013-11-11
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
Comment
Question by:g0mab2
  • 2
  • 2
4 Comments
 
LVL 10

Expert Comment

by:Carnou
ID: 33536924
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
 

Author Comment

by:g0mab2
ID: 33537250
If this is the case, how do I load an image based on the return value of the username?
0
 
LVL 10

Accepted Solution

by:
Carnou earned 500 total points
ID: 33537325
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
 

Author Comment

by:g0mab2
ID: 33537509
I just figured it out thanks for the help.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
The goal of the tutorial is to teach the user how to how to record live broadcast.
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now