Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Loading image from library

Guest
Apr 24, 2010 Apr 24, 2010

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

TOPICS
ActionScript
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 24, 2010 Apr 24, 2010

I think you missed the last sentence of my earlier response

Translate
LEGEND ,
Apr 24, 2010 Apr 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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 24, 2010 Apr 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 24, 2010 Apr 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 24, 2010 Apr 24, 2010

Ok, so thats kool, I could do

var tempVar:String = myString.charAt(i);
var tempString = tempVar + ".psd";

So now, on each loop, tempString will hold the image which needs to be displayed.  But how would I now display this?  I cant do

addChild(tempString);

because tempString is not an Object.  Is there any other way I can do this?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 24, 2010 Apr 24, 2010

To load the image dynamically you can use the Loader class, although I don't think psd formatrs are supported.... "The Loader class is used to load SWF files or image (JPG, PNG, or GIF) files"

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 24, 2010 Apr 24, 2010

Kool.  I am so new too completing this, just have one more little problem.  So, I use the charAt to get each letter entered, one by one. I do

for(var i:int=0; i<defaultName.defaultName.length; i++)  //loop the input
{
  var tempVar:String = myString.charAt(i); 
  var tempString:String =  tempVar + ".JPG";
  trace(tempString);  //this will print off n.jpg and i.jpg, which is exactly what it should do
 
 
 
  myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadImage);
  var file:URLRequest= new URLRequest(tempString);  I then use the loader with the String holding the image name
  myLoader.load(file);   //and I load it
 

  myLoader.x = posOne;  //Now I want to set the position
  myLoader.y= posTwo;
  posOne += 30;   //I need to move it along the x axis
}

My function then does

function loadImage(e:Event) {
    addChild(myLoader);
}

Now, the problem is this.  It seems to load the images, but it only displays the last letter entered.  I am unsure if this is because of my loop somehow, or if it is to do with my x axis position not being updated.  I have create vars for posOne and posTwo which hold the initial value where the first letter should be placed.  I then add 30 onto the x axis position for every loop.  This is to get it to place it next to the first letter.

Would this be because I am declaring my vars within the for loop, hence they are recreating every loop.  Or is this because of the position x axis value not being updated?

Any advise on what I should be doing appreciated.  If I dont do the position, it will display all letters ontop of each other.  I just want them displayed how we would read the name, hense trying to change the position on each one.

cheers

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 24, 2010 Apr 24, 2010

First, in what you show you do not declare the loader inside the loop (as in: var myLoader:Loader = new Loader();).  So you are essentially replacing the content of the same loader over and over again.   That's why only the last image appears.  If you do declare it inside it will be a new instance each time.

Using a for loop to load anything in a controlled order won't be reliable... servers will deliver content via whatever path is convenient.  So you can end up having a later loading being loaded ahead of an earlier one.   You can avoid trouble by making better use of that load complete function and use it to load the images sequentially--along with the help of a counter variable.  You essentially create a loading function that intiates the loading, using the counter to determine which image to load, and assign the listener in there as well.  Then use the load complete function to manage placement of the loaded image, incrment the counter, and call the loading function to load the next image if the counter is less than the String length.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 24, 2010 Apr 24, 2010

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 24, 2010 Apr 24, 2010

I think you missed the last sentence of my earlier response

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 24, 2010 Apr 24, 2010

Thanks so much for your help, got it working now by going through your last sentence again.  Its much appreciated.

cheers

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 24, 2010 Apr 24, 2010
LATEST

You're welcome

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines