- Package
- {
- Public class mousegame extends Sprite
- {
- Private VaR _ sprite: SPRITE;
- Private var posion: point;
- Public Function mousegame ()
- {
- _ Sprite = new sprite ();
- Addchild (_ sprite );
- VaR _ shape: Shape = new shape ();
- Addchild (_ shape );
- _ Shape. Graphics. beginfill (0 x, 1 );
- _ Shape. Graphics. linestyle (1, 0, 1 );
- _ Shape. Graphics. drawcircle (250,250, 50 );
- _ Shape. Graphics. endfill ();
- _ Sprite. addchild (_ shape );
- _ Sprite. addeventlistener (mouseevent. mouse_down, startmascotdrag );
- Stage. addeventlistener (mouseevent. mouse_up, stopmascotdrag );
- _ Sprite. addeventlistener (event. enter_frame, dragmascot );
- }
- Private function startmascotdrag (E: mouseevent): void
- {
- Posion = new point (E. localx, E. localy); // save a vertex in this object
- }
- Private function stopmascotdrag (E: mouseevent): void
- {
- Posion = NULL;
- }
- Private function dragmascot (E: Event): void
- {
- If (posion! = NULL)
- {
- _ Sprite. x = mousex-posion. X;
- _ Sprite. Y = mousey-posion. Y;
- }
- }
- }
- }
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.
- Here we mainly use
- Import flash. display. Sprite;
- Import flash. Events .*;
- Import flash. Geom. Point;
- Import flash. display. shape;
- Four packages. Have you noticed import flash. Geom. Point; which can be recorded?
- In fact, it is very useful to discover this carefully. This function saves the coordinates of a vertex.
How to Create a circle:
- VaR _ shape: Shape =NewSHAPE ();
- Addchild (_ shape );
- _ Shape. Graphics. beginfill (0 x, 1); // fill in the black color with a transparency of 1
- _ Shape. Graphics. linestyle (, 1); // you can specify the line thickness and transparency.
- _ Shape. Graphics. drawcircle (250,250, 50); // circle
- _ Shape. Graphics. endfill (); // end Filling
2. Can I drag the circle?
First, we perform some listeners.
- _ Sprite. addeventlistener (mouseevent. mouse_down, startmascotdrag );
- Stage. addeventlistener (mouseevent. mouse_up, stopmascotdrag );
- _ 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.