Solved

Load PNG into Flash Library for ActionScript

Posted on 2009-05-12
6
1,403 Views
Last Modified: 2013-11-11
Is there a way to load an external PNG file into the Flash library at runtime and then add an identifier to it dynamically so that I could attach the image to various movie clips?  I don't want to loadMovie('file.png',mc) over and over again.  I'd like to just load the PNG file once, give it a reference name, and then use the name to attach the image to numerous movie clips.

I'D LIKE TO LOAD THE PNG FILE ONCE:::

var logo_loader = new MovieClipLoader();
var logoListener:Object = new Object();
logo_loader.addListener(logoListener);
logoListener.onLoadInit = function(target:MovieClip) {target.forceSmoothing = true;}
logo_loader.loadClip( source_url, [some movie clip that I can reference later]  );

AND THEN USE THE LOADED PNG LIKE THIS::

for (i=0; i<10; i++) {
      logo = story_holder.attachMovie('[ movie clip with loaded PNG]','my_png_button',i);
      logo._x = 10;
      logo._y = 5;
}

I'm trying to avoid loading and re-loading the same PNG when it only needs to be loaded once and then referenced.  I'm more familiar with loading PNG assets that are already in the library at runtime, but in this case I need to load the PNG and then attach the reference name when the swf first loads.

Can this be done?
0
Comment
Question by:marcparillo
6 Comments
 
LVL 39

Expert Comment

by:blue-genie
ID: 24372003
I don't think this is possible. what you can do is load it at runtime off stage and access it that way.
0
 
LVL 3

Author Comment

by:marcparillo
ID: 24373266
So, I would load the PNG into a movie clip off stage and then attach that off-stage movie clip containing the PNG to any other movie clip?
0
 
LVL 39

Accepted Solution

by:
blue-genie earned 200 total points
ID: 24375330
yeah you could do that by referencing the movieclip it's loaded into.
0
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.

 
LVL 37

Assisted Solution

by:CyanBlue
CyanBlue earned 100 total points
ID: 24375474
I don't think you can duplicate the movieClip that contains the loaded image...  It'll need to be reloaded everytime you need it...  Glad to be proven wrong...  I'd love to have that feature...

CyanBlue
0
 
LVL 22

Assisted Solution

by:rascalpants
rascalpants earned 200 total points
ID: 24378121
I believe duplicateMovieClip() is what you are looking for in AS 2.0  

for AS 3.0, you can use Senocular's DupilcateDisplayObject class...

http://www.senocular.com/flash/actionscript.php?file=ActionScript_3.0/com/senocular/display/duplicateDisplayObject.as


or you could create a Singleton Class that loads the PNG, and then you call a method in the Singleton class that gets the Movieclip reference, and then you use addChild to push it to the display stack of the individual Movieclip that needs the PNG.


that sounded overly complex, but it really isn't.


more specifics would help...


rp / ZA
0
 
LVL 22

Expert Comment

by:rascalpants
ID: 24379588
also, if you are allowed to take the PNG and turn it into a SWF, then you could use the below code to make it work for you...

Lets say the PNG is loaded into a movieclip and the export for Actionscript is checked... the example assumes you named it "TheLogo"

then you load in the SWF with the normal Loader class, and then you use the applicationDomain by referencing the class name you gave it in the external SWF...  in this case "TheLogo".

then you can create a new instance of that class...  

FYI... this is all AS3.


rp / ZA


import flash.display.Loader;
import flash.system.ApplicationDomain;
 
var logoURL:String = "TheLogo.swf";
var total:int = 10;
 
var logoLoader = new Loader();
logoLoader.load( new URLRequest( logoURL ) );
 
logoLoader.contentLoaderInfo.addEventListener(Event.INIT, logoLoader_OnInit, false, 0, true);
 
function logoLoader_OnInit( evt:Event ):void
{
	logoLoader.contentLoaderInfo.removeEventListener(Event.INIT, logoLoader_OnInit);
	
	// "TheLogo" is the class name given in the library properties panel
	var logo:Class = evt.target.applicationDomain.getDefinition("TheLogo");
 
	for( var i:int = 0; i < total; ++i )
	{
		var tempL:DisplayObject = new logo();
		
		tempL.x = Math.ceil( Math.random() * 400 );
		tempL.y = Math.ceil( Math.random() * 400 );
		
		this.addChild( tempL );
	}
}

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to put actionscript code for a button? 6 129
Download Adobe Flash Player 6 63
PDF: Display my cards game? 8 109
Flash ActionScript 6 52
This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
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.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

816 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now