Question 1: how to load external Images
When using flash for multimedia production, it is often necessary to use external loading images, SWF files, and so on. These are common problems that need to be solved. Adobe Flash as provides us with a loader class to help us solve these problems;
The loader class can be used to load SWF files or images (JPG, PNG, or GIF) files.
The loaderinfo class provides information about loaded SWF files or image files (JPEG, GIF, or PNG.
- Package
- {
- Import flash. display. movieclip;
- Import flash. Events .*;
- Import flash. display. loader;
- Import flash.net. URLRequest;
- Public class example extends movieclip
- {
- Public Function example ()
- {
- Init ();
- }
- Private function Init (): void
- {
- VaR _ Loader: loader = new loader ();
- Addchild (_ loader );
- _ Loader. Load (New URLRequest ("1.jpg "));
- _ Loader. contentloaderinfo. addeventlistener (event. Complete, loadhander );
- _ Loader. contentloaderinfo. addeventlistener (ioerrorevent. io_error, iohander );
- }
- Private function loadhander (E: Event): void
- {
- Trace ("loaded successfully ");
- }
- Private function iohander (E: ioerrorevent): void
- {
- Trace ("loading error ");
- }
- }
- }
After loading, let's try to see how to add one more tracking event.
Trace(e.tar get. content );
The output result is:
Loaded successfully
[Object bitmap]
Apparently, a bitmap object is returned.
Modify the code to see how the result is:
- Private function Init (): void
- {
- VaR _ Loader: loader = new loader ();
- _ Loader. Load (New URLRequest ("1.jpg "));
- _ Loader. contentloaderinfo. addeventlistener (event. Complete, loadhander );
- _ Loader. contentloaderinfo. addeventlistener (ioerrorevent. io_error, iohander );
- }
- Private function loadhander (E: Event): void
- {
- Trace ("loaded successfully ");
- Trace(e.tar get. content );
- VaR image: bitmap = E. currenttarget. content as bitmap;
- // Var bitmap: bitmapdata = image. bitmapdata;
- Addchild (image );
- }
The image is also displayed. Only the information returned after loading is a bitmap, so that we can perform some operations and interactive effects on the bitmap.
Question 2: How do I interact with loaded objects?
Let's look at the inheritance relationships of Bitmap classes: bitmap, displayobject, eventdispatcher, and object.
Here, bitmap objects do not support mouse events, so we need to convert them into the supported objects. Therefore, we need to make some settings. Create a movieclip object, add the desired object to the display list, and add it to the sublevel display list of the MC object.
The interactiveobject class is an abstract base class for all the displayed objects that you can interact with by using the mouse and keyboard.
- Package
- {
- Import flash. display. movieclip;
- Import flash. Events .*;
- Import flash. display. loader;
- Import flash.net. URLRequest;
- Import flash. display. Bitmap;
- Public class example extends movieclip
- {
- Public Function example ()
- {
- Init ();
- }
- Private function Init (): void
- {
- VaR _ Loader: loader = new loader ();
- _ Loader. Load (New URLRequest ("1.jpg "));
- _ Loader. contentloaderinfo. addeventlistener (event. Complete, loadhander );
- _ Loader. contentloaderinfo. addeventlistener (ioerrorevent. io_error, iohander );
- }
- Private function loadhander (E: Event): void
- {
- Trace ("loaded successfully ");
- Trace(e.tar get. content );
- VaR image: bitmap = E. currenttarget. content as bitmap;
- // Var bitmap: bitmapdata = image. bitmapdata;
- VaR MC: movieclip = new movieclip ();
- MC. addchild (image );
- Addchild (MC );
- MC. addeventlistener (mouseevent. Click, onclick );
- }
- Private function iohander (E: ioerrorevent): void
- {
- Trace ("loading error ");
- }
- Private function onclick (E: mouseevent): void
- {
- Trace ("You clicked on me ");
- }
- }
- }
Here are some simple interaction effects. You can use the mouse or keyboard for richer interaction effects.