Skip to main content
April 24, 2010
Answered

Loading image from library

  • April 24, 2010
  • 1 reply
  • 1599 views

Hi.  So I imported some images I have into my library.  I then converted them to a movie clip, which I am not sure if I was suppossed to do.  I was just wondering how I would now load it into my as3 code?  My current image is called imageN.psd, and as I say, it is now a movie clip.  Do I use the url loaded or something, or should I keep it as a normal image.  If so, can I use the bitmap call to load a psd file?

Any examples appreciated

cheers

This topic has been closed for replies.
Correct answer Ned Murphy

Kool.  Thanks for the help.  I followed your advise and got it working.  However, I am now trying to follow your suggestion to make it better.  This is what I have done.

function showTxt(e:MouseEvent):void {


outputTxt.text = nameTxt.text;

var myString:String=defaultName.defaultName.text;
loadImage(myString);

}

function loadImage(myString:String):void {

if(counter<myString.length)
{
  var tempVar:String = myString.charAt(counter);
  var tempString:String =  tempVar + ".JPG";
  file = new URLRequest(tempString);
  myLoader = new Loader();
  
  myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadIm);
  myLoader.load(file);
  counter++;
}
}

function loadIm(e:Event) {

  myLoader.x = posOne;
  myLoader.y= posTwo;
  addChild(myLoader);
  posOne =  posOne+ 40;
}

So, the first function is for my button, which is pressed when the user enters their name.  The name is put into a variable, and it is passed to the function loadImage().  Here, I check to see if the counter, which was initialised to 0, is less than the input length.  If it is, it will do the loading, and call up the Event.COMPLETE function, which places the images, and adds them to the stage.

This time however, only the first image is displaying.  I have followed your advise as closely as possible, but I am obviously going wrong somewhere.  Sorry about this, only two weeks into AS3.

Can you advise me on what I am doing wrong now, and what i need to change?

cheers


I think you missed the last sentence of my earlier response

1 reply

Ned Murphy
Legend
April 24, 2010

Right click on the movieclip you created in the library and select the Linkage option.  In the interface that appears select the option to Export for Actionscript and then enter a Class name in the field with that label.  Classes are named starting with capital letters as a standard practice (not a law).

To add that image dynamically using code, you would use the following (assume you gave it a class name of MyImg)...

var myImage:MyImg = new MyImg();

addChild(myImage);

April 24, 2010

Hi.  I just created a post on one of my java forums, so the code is slightly in java, but it should be easy to read.  Its a problem I am having in as3, which hopefully you could help with

Just trying again to do something in as3, but struggling with the logic and hopefully you can help.

Say I get user input, and I store it into an Array.  Say the user enters Nick, the array will be assigned as so
array1[0]="N";
array1[1]="i";
array1[2]="c";
array1[3]="k";

I can then do a standard loop through the array

for(int i = 0; i < array1.length; i++){
...

But what I now want to do is check every position, and pull out an image file based on the input. So, we know position 0 holds a "N", so now it should pull N.Jpg from my library.

Problem is, I wont always know the input. Surely I dont have to do an if statement for every single letter, for every single array element. This would be too much e.g.

if(array1=="A")
display(A.Jpg);
if(array1=="B")
display(B.Jpg);

Any suggestions on the best way to achieve this appreciated.

Cheers

Ned Murphy
Legend
April 24, 2010

If it is user input you can just use a String and loop thru the string using the charAt method to pull out each character.  YOu shouldn't have to use any sequence of if statements to make use of the character, you can just build a string ("N.Jpg") and make use of that.  How you make use of it is a wide open question at this point.