Solved

load data and image in AS3

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adobe flash player fails to install 12 330
Google Chrome pepflashplayer.dll 8 232
flash in chrome 4 141
PDF edit software 2 63
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,…
This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
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.

734 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