Solved

load data and image in AS3

Posted on 2010-08-26
4
370 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Stock Videos 17 332
Adobe Flash Player not supported on my Tablet 7 730
You Tube Videos not playing 9 123
Include Adobe AIR libraries in Flash Builder 4 121
I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

776 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