在上讲中我们使用是已创建好影片剪辑并在库中做类链接这对有复杂图形创作是比较好选择如果你能熟练应用绘图API绘制出你想要任意图形就可以不使用库元件直接在类中编写下面我们用这种方式编写类代码:
创建个DocumentClass类(创建100个随机摆放圆)
1. 代码:
2.
3. package {
4.
5. import flash.display.MovieClip;
6. public DocumentClass extends MovieClip {
7. // 属性
8. private var _circle:Drag_circle;
9. private const maxBalls: = 100;
10. // 构造
11. public function DocumentClass {
12.
13. var i:;
14. // 循环创建小球
15. for(i = 0; i<= maxBalls; i) {
16. // 创建可拖动小球例子
17. _circle = Drag_circle;
18. // 设置小球例子些属性
19. _circle.scaleY = _circle.scaleX = Math.random;
20. // 场景中x,y位置
21. _circle.x = Math.round(Math.random *(stage.stageWidth - _circle.width));
22. _circle.y = Math.round(Math.random *(stage.stageHeight - _circle.height));
23. // 在场景上显示
24. addChild(_circle);
25. }
26. }
27. }
28. }
Drag_circle类 (绘制个红色圆有拖拽功能)
1. 代码:
2.
3. package {
4.
5. import flash.display.Sprite;
6. import flash.display.Shape;
7. import flash.events.MouseEvent;
8.
9. public Drag_circle extends Sprite {
10.
11. private var _circle:Sprite;
12.
13. public function Drag_circle {
14.
15. _circle = Sprite;
16. _circle.graphics.beginFill(0xff0000);
17. _circle.graphics.drawCircle(0, 0, 10);
18. _circle.graphics.endFill;
19. _circle.buttonMode = true;
20. addChild(_circle);
21.
22.
23. _circle.addEventListener(MouseEvent.CLICK,onClick);
24. _circle.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
25. _circle.addEventListener(MouseEvent.MOUSE_UP,onUp);
26.
27. }
28.
29.
30. private function onClick(event:MouseEvent):void {
31. trace("circle clicked");
32. }
33.
34. private function onDown(event:MouseEvent):void {
35. _circle.startDrag;
36. }
37.
38. private function onUp(event:MouseEvent):void {
39. _circle.stopDrag;
40. }
41. }
42. }
新建个fla文件保存在Document.as和Drag_.as类同目录中注意:和上回讲元件类区别在于不再需要让场景中有任何内容我们已在主类DocumentClass.as中动态添加和显示了circle_mc在属性面板中文档类输入框中输入类名 DocumentClass就可以测试了(你可以尝试着给小球加入随机颜色或渐变颜色)
最新评论