Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

load data and image in AS3

Posted on 2010-08-26
4
Medium Priority
?
377 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
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.

715 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