Multimedia interaction application BASICS (8)

Source: Internet
Author: User
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.

 

  1. Package
  2. {
  3. Import flash. display. movieclip;
  4. Import flash. Events .*;
  5. Import flash. display. loader;
  6. Import flash.net. URLRequest;
  7. Public class example extends movieclip
  8. {
  9. Public Function example ()
  10. {
  11. Init ();
  12. }
  13. Private function Init (): void
  14. {
  15. VaR _ Loader: loader = new loader ();
  16. Addchild (_ loader );
  17. _ Loader. Load (New URLRequest ("1.jpg "));
  18. _ Loader. contentloaderinfo. addeventlistener (event. Complete, loadhander );
  19. _ Loader. contentloaderinfo. addeventlistener (ioerrorevent. io_error, iohander );
  20. }
  21. Private function loadhander (E: Event): void
  22. {
  23. Trace ("loaded successfully ");
  24. }
  25. Private function iohander (E: ioerrorevent): void
  26. {
  27. Trace ("loading error ");
  28. }
  29. }
  30. }

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:

 

  1. Private function Init (): void
  2. {
  3. VaR _ Loader: loader = new loader ();
  4. _ Loader. Load (New URLRequest ("1.jpg "));
  5. _ Loader. contentloaderinfo. addeventlistener (event. Complete, loadhander );
  6. _ Loader. contentloaderinfo. addeventlistener (ioerrorevent. io_error, iohander );
  7. }
  8. Private function loadhander (E: Event): void
  9. {
  10. Trace ("loaded successfully ");
  11. Trace(e.tar get. content );
  12. VaR image: bitmap = E. currenttarget. content as bitmap;
  13. // Var bitmap: bitmapdata = image. bitmapdata;
  14. Addchild (image );
  15. }

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.

  1. Package
  2. {
  3. Import flash. display. movieclip;
  4. Import flash. Events .*;
  5. Import flash. display. loader;
  6. Import flash.net. URLRequest;
  7. Import flash. display. Bitmap;
  8. Public class example extends movieclip
  9. {
  10. Public Function example ()
  11. {
  12. Init ();
  13. }
  14. Private function Init (): void
  15. {
  16. VaR _ Loader: loader = new loader ();
  17. _ Loader. Load (New URLRequest ("1.jpg "));
  18. _ Loader. contentloaderinfo. addeventlistener (event. Complete, loadhander );
  19. _ Loader. contentloaderinfo. addeventlistener (ioerrorevent. io_error, iohander );
  20. }
  21. Private function loadhander (E: Event): void
  22. {
  23. Trace ("loaded successfully ");
  24. Trace(e.tar get. content );
  25. VaR image: bitmap = E. currenttarget. content as bitmap;
  26. // Var bitmap: bitmapdata = image. bitmapdata;
  27. VaR MC: movieclip = new movieclip ();
  28. MC. addchild (image );
  29. Addchild (MC );
  30. MC. addeventlistener (mouseevent. Click, onclick );
  31. }
  32. Private function iohander (E: ioerrorevent): void
  33. {
  34. Trace ("loading error ");
  35. }
  36. Private function onclick (E: mouseevent): void
  37. {
  38. Trace ("You clicked on me ");
  39. }
  40. }
  41. }

Here are some simple interaction effects. You can use the mouse or keyboard for richer interaction effects.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.