Флэш-приложения: техника программирования

Материал из Department of Theoretical and Applied Mechanics
Перейти к: навигация, поиск

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

Использование мыши

Флэш-приложение, позволяющее при помощи мыши перетаскивать два графических объекта — квадрат и круг. Кроме того, демонстрируется возможность делать объекты полупрозрачными (свойство 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">

package 
{
	// This code creates a drag-and-drop interaction using the mouse-following 
	// technique. 
	// circle and square are DisplayObjects (e.g. MovieClip or Sprite  
	// instances). 
	 
	import flash.display.Sprite;
	import flash.display.DisplayObject; 
	import flash.events.MouseEvent; 
	 
	public class Main extends Sprite 
	{
		public var offsetX:Number; 
		public var offsetY:Number; 
		public var draggedObject:DisplayObject; 
		
		public var circle:Sprite = new Sprite(); 
		public var square:Sprite = new Sprite(); 
		 
		// This function is called when the mouse button is pressed. 
		public function startDragging(event:MouseEvent):void 
		{ 
			// remember which object is being dragged 
			draggedObject = DisplayObject(event.target); 
			 
			// Record the difference (offset) between where the cursor was when 
			// the mouse button was pressed and the x, y coordinate of the 
			// dragged object when the mouse button was pressed. 
			offsetX = event.stageX - draggedObject.x; 
			offsetY = event.stageY - draggedObject.y; 
			 
			// move the selected object to the top of the display list 
			stage.addChild(draggedObject); 
			 
			// Tell Flash Player to start listening for the mouseMove event. 
			stage.addEventListener(MouseEvent.MOUSE_MOVE, dragObject); 
		} 
		 
		// This function is called when the mouse button is released. 
		public function stopDragging(event:MouseEvent):void 
		{ 
			// Tell Flash Player to stop listening for the mouseMove event. 
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragObject); 
			draggedObject.alpha = 1; 
		} 
		 
		// This function is called every time the mouse moves, 
		// as long as the mouse button is pressed down. 
		public function dragObject(event:MouseEvent):void 
		{ 
			// Move the dragged object to the location of the cursor, maintaining  
			// the offset between the cursor's location and the location  
			// of the dragged object. 
			draggedObject.x = event.stageX - offsetX; 
			draggedObject.y = event.stageY - offsetY; 
			draggedObject.alpha = 0.5; 
			 
			// Instruct Flash Player to refresh the screen after this event. 
			event.updateAfterEvent(); 
		} 
		 
		public function Main():void 
		{
			var y0:Number = stage.stageHeight / 2;
			var r: Number = stage.stageWidth  / 7;
			
			circle.graphics.beginFill(0x0000ff); 
			circle.graphics.drawCircle(5 * r, y0, r); 
			circle.graphics.endFill(); 
			addChild(circle); 		 

			square.graphics.beginFill(0xff0000); 
			square.graphics.drawRect(r, y0 - r, 2*r, 2*r); 
			square.graphics.endFill(); 
			addChild(square); 		 

			circle.addEventListener(MouseEvent.MOUSE_DOWN, startDragging); 
			circle.addEventListener(MouseEvent.MOUSE_UP, stopDragging); 
			 
			square.addEventListener(MouseEvent.MOUSE_DOWN, startDragging); 
			square.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
		}
	}
}

</toggledisplay>

См. также