Flash Game Study Notes: move the mouse

Source: Internet
Author: User
  1. Package
  2. {
  3. Public class mousegame extends Sprite
  4. {
  5. Private VaR _ sprite: SPRITE;
  6. Private var posion: point;
  7. Public Function mousegame ()
  8. {
  9. _ Sprite = new sprite ();
  10. Addchild (_ sprite );
  11. VaR _ shape: Shape = new shape ();
  12. Addchild (_ shape );
  13. _ Shape. Graphics. beginfill (0 x, 1 );
  14. _ Shape. Graphics. linestyle (1, 0, 1 );
  15. _ Shape. Graphics. drawcircle (250,250, 50 );
  16. _ Shape. Graphics. endfill ();
  17. _ Sprite. addchild (_ shape );
  18. _ Sprite. addeventlistener (mouseevent. mouse_down, startmascotdrag );
  19. Stage. addeventlistener (mouseevent. mouse_up, stopmascotdrag );
  20. _ Sprite. addeventlistener (event. enter_frame, dragmascot );
  21. }
  22. Private function startmascotdrag (E: mouseevent): void
  23. {
  24. Posion = new point (E. localx, E. localy); // save a vertex in this object
  25. }
  26. Private function stopmascotdrag (E: mouseevent): void
  27. {
  28. Posion = NULL;
  29. }
  30. Private function dragmascot (E: Event): void
  31. {
  32. If (posion! = NULL)
  33. {
  34. _ Sprite. x = mousex-posion. X;
  35. _ Sprite. Y = mousey-posion. Y;
  36. }
  37. }
  38. }
  39. }

 

Code parsing:

First, we create a class to continue Sprite, and draw a circle in the constructor. we can listen to the mouse and drag the circle.

  1. Here we mainly use
  2. Import flash. display. Sprite;
  3. Import flash. Events .*;
  4. Import flash. Geom. Point;
  5. Import flash. display. shape;
  6. Four packages. Have you noticed import flash. Geom. Point; which can be recorded?
  7. In fact, it is very useful to discover this carefully. This function saves the coordinates of a vertex.

How to Create a circle:

  1. VaR _ shape: Shape =NewSHAPE ();

  2. Addchild (_ shape );
  3. _ Shape. Graphics. beginfill (0 x, 1); // fill in the black color with a transparency of 1
  4. _ Shape. Graphics. linestyle (, 1); // you can specify the line thickness and transparency.
  5. _ Shape. Graphics. drawcircle (250,250, 50); // circle
  6. _ Shape. Graphics. endfill (); // end Filling

2. Can I drag the circle?

 

First, we perform some listeners.

  1. _ Sprite. addeventlistener (mouseevent. mouse_down, startmascotdrag );
  2. Stage. addeventlistener (mouseevent. mouse_up, stopmascotdrag );
  3. _ Sprite. addeventlistener (event. enter_frame, dragmascot );

Two mouse events, one being pressed, the other being not pressed, and the other being a function for dragging.

 

Iii. Computing location

Subtraction

New coordinates = current mouse value-original coordinates

In this way, you can complete a simple mouse drag

 

Iv. Expansion

 

Try some games that interact with the mouse

Well, I am exhausted.

 

 

 

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.