flash 绘图API:剑归宗
2010-03-02 00:00:00 来源:WEB开发网vx=speed*Math.cosA;
vy=speed*Math.sinA;
速度是向量,它可以分解成两个不同方向的分速度。根据角度偏移了。
剑。x+=vx;
剑。y+=vy; 在每一帧的刷新下,剑会产生位移改变。
代码清单:
1. package
2. {
3. //剑归宗
4.
5. import flash.display.Sprite;
6. import flash.events.*;
7. import flash.display.DisplayObject;
8.
9. public class Main extends Sprite
10. {
11. private var list:Array=new Array();
12. private var Speed:int=4;//飞行速度
13. private var contain:Sprite=new Sprite();
14. private var num:int=20;
15. public function Main()
16. {
17. addChild(contain);
18. stage.addEventListener(MouseEvent.CLICK,onClick);
19. }
20. //创建一把剑
21. private function onClick(event:MouseEvent):void
22. {
23. while (list.length>0)
24. {
25. list.pop();
26. }
27. for (var i:int=0; i<num; i++)
28. {
29. var mysword:Sword=new Sword();
30. contain.addChild(mysword);
31. mysword.x=mouseX+Math.cos(i*2*Math.PI/num)*150;
32. mysword.y=mouseY+Math.sin(i*2*Math.PI/num)*150;
33. list.push(mysword);
34. var angle:Number=Math.atan2(mouseY-mysword.y,mouseX-mysword.x);//角度
35. mysword.angle=angle;
36. mysword.rotation=angle*180/Math.PI-90;
37. mysword.addEventListener(Event.ENTER_FRAME,Run);//进行运动
38. }
39.
40. }
41. //运动
42. private function Run(event:Event):void
43. {
44. var vx:Number=Math.cos(event.currentTarget.angle)*Speed;
45. var vy:Number=Math.sin(event.currentTarget.angle)*Speed;
46. event.currentTarget.x+=vx;
47. event.currentTarget.y+=vy;
48. if (event.currentTarget.x>stage.stageWidth||event.currentTarget.x<0||event.currentTarget.y<0||
49. event.currentTarget.y>stage.stageHeight
50. )
51. { if(event.currentTarget!=null)
52. {
53. event.currentTarget.removeEventListener(Event.ENTER_FRAME,Run);
54. contain.removeChild(DisplayObject(event.currentTarget));
55. }
56. }
57. }
58. }
59. }
60.
61. import flash.display.Sprite;
62. //描绘一把剑
63. internal class Sword extends Sprite
64. {
65. private var pen:Sprite=new Sprite();
66. public var angle:Number=0;
67. private var array:Array=[ [50,0],
68. [50,50],
69. [20,50],
70. [20,60],
71. [45,60],
72. [45,300],
73. [65,330],
74. [75,300],
75. [75,60],
76. [105,60],
77. [105,50],
78. [70,50],
79. [70,0],
80. [50,0]
81. ];
82. public function Sword()
83. {
84. init();
85. }
86. private function init():void
87. {
88. addChild(pen);
89. pen.graphics.lineStyle(1,0x000000);
90. pen.graphics.moveTo(array[0][0],array[0][1]);
91. for (var i:int=0; i<array.length; i++)
92. {
93. pen.graphics.lineTo(array[i][0],array[i][1]);
94. }
95. pen.graphics.drawCircle(60,-11,12);//绘制剑中心
96. pen.graphics.drawCircle(60,60,14);
97. pen.graphics.beginFill(0xff00ff);
98. pen.graphics.drawCircle(60,60,5);
99. pen.graphics.endFill();
100. pen.graphics.moveTo(60,60);
101. pen.graphics.lineTo(65,330);
102. pen.scaleX=pen.scaleY=0.3;
103. }
104. }
- ››FLASH不等于运算符!=的使用实例
- ››FLASH不全等运算符!==
- ››FLASH字符串分隔符运算符
- ››FLASH% 模运算符
- ››Flash+、++、+= 加法运算符
- ››Flash, 逗号运算符
- ››flash中的-、--、-=减法运算符
- ››Flash的-Infinity 常数、.点运算符、/ 除法运算符...
- ››Flash两种注释方法/*..*/ 和// 注释行分隔符运算符...
- ››Flash的/=除法赋值运算符、=赋值运算符、== 等于运...
- ››Flash之?: 条件运算符、^ 按位 XOR 运算符、^= 按...
- ››Flash的_framesloaded代码示例
更多精彩
赞助商链接