Флэш-приложения: техника программирования — различия между версиями

Материал из Department of Theoretical and Applied Mechanics
Перейти к: навигация, поиск
м (Замена текста — «</source>» на «</syntaxhighligh>»)
м (Замена текста — «</syntaxhighligh>» на «</syntaxhighlight>»)
 
Строка 99: Строка 99:
 
}
 
}
 
}
 
}
</syntaxhighligh>
+
</syntaxhighlight>
 
</toggledisplay>
 
</toggledisplay>
  

Текущая версия на 13:55, 9 марта 2015

Кафедра ТМ > Программирование > Интернет > Флэш > Техника

На этой странице располагаются простые примеры флэш-приложений с исходными кодами, демонстрирующих различные возможности программирования.

Использование мыши[править]

Флэш-приложение, позволяющее при помощи мыши перетаскивать два графических объекта — квадрат и круг. Кроме того, демонстрируется возможность делать объекты полупрозрачными (свойство alpha, характеризующее "непрозрачность" объекта, при его перетаскивании устанавливается равным 0.5).

play=true|height=320|width=400</flash>


Текст программы на языке ActionScript 3.0 (разработчик А. Кривцов на основе статьи Changing position из ActionScript 3.0 Developer's Guide): <toggledisplay status=hide showtext="Показать↓" hidetext="Скрыть↑" linkstyle="font-size:default">

 1 package 
 2 {
 3 	// This code creates a drag-and-drop interaction using the mouse-following 
 4 	// technique. 
 5 	// circle and square are DisplayObjects (e.g. MovieClip or Sprite  
 6 	// instances). 
 7 	 
 8 	import flash.display.Sprite;
 9 	import flash.display.DisplayObject; 
10 	import flash.events.MouseEvent; 
11 	 
12 	public class Main extends Sprite 
13 	{
14 		public var offsetX:Number; 
15 		public var offsetY:Number; 
16 		public var draggedObject:DisplayObject; 
17 		
18 		public var circle:Sprite = new Sprite(); 
19 		public var square:Sprite = new Sprite(); 
20 		 
21 		// This function is called when the mouse button is pressed. 
22 		public function startDragging(event:MouseEvent):void 
23 		{ 
24 			// remember which object is being dragged 
25 			draggedObject = DisplayObject(event.target); 
26 			 
27 			// Record the difference (offset) between where the cursor was when 
28 			// the mouse button was pressed and the x, y coordinate of the 
29 			// dragged object when the mouse button was pressed. 
30 			offsetX = event.stageX - draggedObject.x; 
31 			offsetY = event.stageY - draggedObject.y; 
32 			 
33 			// move the selected object to the top of the display list 
34 			stage.addChild(draggedObject); 
35 			 
36 			// Tell Flash Player to start listening for the mouseMove event. 
37 			stage.addEventListener(MouseEvent.MOUSE_MOVE, dragObject); 
38 		} 
39 		 
40 		// This function is called when the mouse button is released. 
41 		public function stopDragging(event:MouseEvent):void 
42 		{ 
43 			// Tell Flash Player to stop listening for the mouseMove event. 
44 			stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragObject); 
45 			draggedObject.alpha = 1; 
46 		} 
47 		 
48 		// This function is called every time the mouse moves, 
49 		// as long as the mouse button is pressed down. 
50 		public function dragObject(event:MouseEvent):void 
51 		{ 
52 			// Move the dragged object to the location of the cursor, maintaining  
53 			// the offset between the cursor's location and the location  
54 			// of the dragged object. 
55 			draggedObject.x = event.stageX - offsetX; 
56 			draggedObject.y = event.stageY - offsetY; 
57 			draggedObject.alpha = 0.5; 
58 			 
59 			// Instruct Flash Player to refresh the screen after this event. 
60 			event.updateAfterEvent(); 
61 		} 
62 		 
63 		public function Main():void 
64 		{
65 			var y0:Number = stage.stageHeight / 2;
66 			var r: Number = stage.stageWidth  / 7;
67 			
68 			circle.graphics.beginFill(0x0000ff); 
69 			circle.graphics.drawCircle(5 * r, y0, r); 
70 			circle.graphics.endFill(); 
71 			addChild(circle); 		 
72 
73 			square.graphics.beginFill(0xff0000); 
74 			square.graphics.drawRect(r, y0 - r, 2*r, 2*r); 
75 			square.graphics.endFill(); 
76 			addChild(square); 		 
77 
78 			circle.addEventListener(MouseEvent.MOUSE_DOWN, startDragging); 
79 			circle.addEventListener(MouseEvent.MOUSE_UP, stopDragging); 
80 			 
81 			square.addEventListener(MouseEvent.MOUSE_DOWN, startDragging); 
82 			square.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
83 		}
84 	}
85 }

</toggledisplay>

См. также[править]