Показать сообщение отдельно
Старый 09.11.2009, 18:55
orcpochta вне форума Посмотреть профиль Отправить личное сообщение для orcpochta Найти все сообщения от orcpochta
  № 17  
Ответить с цитированием
orcpochta

Регистрация: Feb 2008
Сообщений: 890
Сейчас читаю Мука, немного запутался и у меня вот такой вопрос:
нужно ли перед удалением объекта отписываться от внутренних событий?
т.е. если есть внутренний объект stageDetector, в котором регистрируются приемники addedToStageListener и removedFromStageListener, нужно ли отписывать эти приемники перед удалением объекта?

Код:
package {
	import flash.display.*;
	import flash.ui.*;
	import flash.events.*;
	import flash.geom.*;
	
	public class CustomMousePointer extends Sprite {
		private var stageDetector:StageDetector;
		
		public function CustomMousePointer ( ) {
			graphics.lineStyle(1);
			graphics.beginFill(0x00FF00, 1);
			graphics.lineTo(15, 5);
			graphics.lineTo(5, 15);
			graphics.lineTo(0, 0);
			graphics.endFill( );
			
			stageDetector = new StageDetector(this);
			stageDetector.addEventListener(StageDetector.ADDED_TO_STAGE, addedToStageListener);
			stageDetector.addEventListener(StageDetector.REMOVED_FROM_STAGE, removedFromStageListener);
		}
		
		private function addedToStageListener (e:Event):void {
			visible = false;
			
			Mouse.hide( );
			
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);
			stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveListener);
		}
		
		private function removedFromStageListener (e:Event):void {
			trace(x + ", " + y);
			
			Mouse.show( );
			
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);
			stage.removeEventListener(Event.MOUSE_LEAVE, mouseLeaveListener);
		}
		
		private function mouseLeaveListener(e:Event):void {
			visible = false;
		}
		
		private function mouseMoveListener (e:MouseEvent):void {
			var pointInParent:Point = parent.globalToLocal(new Point(e.stageX, e.stageY));
			
			x = pointInParent.x;
			y = pointInParent.y;
						
			e.updateAfterEvent( );
			
			if (!visible) {
				visible = true;
			}
		}
		
		public function dispose ( ):void {
			Mouse.show( );
			
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);
			stage.removeEventListener(Event.MOUSE_LEAVE, mouseLeaveListener);
			stageDetector.removeEventListener(StageDetector.ADDED_TO_STAGE, addedToStageListener);
			stageDetector.removeEventListener(StageDetector.REMOVED_FROM_STAGE, removedFromStageListener);
		}
	}
}
Код:
package {
	import flash.display.*;
	
	public class CustomMousePointerDemo extends Sprite {
		private var pointer:CustomMousePointer;
		
		public function CustomMousePointerDemo ( ) {			
			pointer = new CustomMousePointer( );
			addChild(pointer);

			//Удаление:
                       /*pointer.dispose( );
                          pointer = null;*/

		}
	}
}
__________________
скриптограф