Multimedia interaction application BASICS (6)

Source: Internet
Author: User

Question 1: How to delete an object?

 

There are many ways to delete objects. Let's do some simple operations step by step.

There is a button in the scenario, we click the button, and then delete a MC

 

Steps:

Put a button named BTN in the scenario

Create a MC object and add it to the display list

 

  1. Package
  2. {
  3. Import flash. display. movieclip;
  4. Import flash. Events .*;
  5. Import flash. display. simplebutton;
  6. Public class example4 extends movieclip
  7. {
  8. Private var mymc: Mc;
  9. Public Function example4 ()
  10. {Init ();
  11. BTN. addeventlistener (mouseevent. Click, onclick );
  12. }
  13. Private function Init (): void
  14. {
  15. Mymc = new MC ();
  16. Addchild (mymc );
  17. Mymc. x = 250;
  18. Mymc. Y = 200;
  19. }
  20. Private function onclick (E: mouseevent): void
  21. {
  22. Removechild (mymc); // Delete the MC object
  23. }
  24. }
  25. }

It's easy to use removechild to delete the MC object, but when we click the button again, we can find that such code will have some errors.

Argumenterror: Error #2025: The provided displayobject must be a sub-level of the caller.
At flash. display: displayobjectcontainer/removechild ()
At example4/: onclick ()

 

The problem is that we have deleted the object, so when we call the delete object, the object does not exist.

To avoid this problem, we need to modify the code

  1. Private function onclick (E: mouseevent): void
  2. {If (mymc! = NULL)
  3. {Removechild (mymc );
  4. Mymc = NULL;
  5. }
  6. }

 

Determines whether the object is null, so that even if you click the button again, the object will remain in the memory after deletion, so we assign null to it to complete some simple deletion.

 

 

Question 2: How to delete multiple objects?

According to the above method, we know that we can use the removechild method to delete objects and see how to delete multiple objects?

What are the following results?

 

  1. Package
  2. {
  3. Import flash. display. movieclip;
  4. Import flash. Events .*;
  5. Import flash. display. simplebutton;
  6. Public class example4 extends movieclip
  7. {
  8. Private var mymc: Mc;
  9. Public Function example4 ()
  10. {
  11. Init ();
  12. BTN. addeventlistener (mouseevent. Click, onclick );
  13. }
  14. Private function Init (): void
  15. {
  16. For (var I: Int = 0; I <5; I ++)
  17. {
  18. Mymc = new MC ();
  19. Addchild (mymc );
  20. Mymc. x = 50 + I * mymc. width;
  21. Mymc. Y = 200;
  22. }
  23. }
  24. Private function onclick (E: mouseevent): void
  25. {
  26. If (mymc! = NULL)
  27. {
  28. Removechild (mymc );
  29. Mymc = NULL;
  30. }
  31. }
  32. }
  33. }

 

Obviously, if you run the code like this, it will only delete an object. How can you delete the object one by trying to change the result?

Note that this code is faulty. Can you solve it by yourself?

 

 

 

 

 

 

 

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.