PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/com/lele/Container/EffectContainer.as

https://gitlab.com/Treeky-NULL/LezaiNiubi
ActionScript | 130 lines | 123 code | 3 blank | 4 comment | 13 complexity | 429c063b3971d910ff48677e8f3851c4 MD5 | raw file
  1. package com.lele.Container
  2. {
  3. import com.lele.Plugin.ScreenShoot.ScreenShooter;
  4. import flash.display.DisplayObject;
  5. import flash.display.Sprite;
  6. import flash.events.Event;
  7. /**
  8. * ...
  9. * @author Lele
  10. */
  11. public class EffectContainer extends Sprite
  12. {
  13. private var _screenShooter:ScreenShooter;
  14. private var _resourceHolder:Sprite;
  15. private var _maskHolder:Sprite;
  16. private var _frontDisplay:Sprite;//前景
  17. private var _backDisplay:Sprite;//后景
  18. private var _triggedTime:Number;
  19. public function EffectContainer()
  20. {
  21. _frontDisplay = new Sprite();
  22. _backDisplay = new Sprite();
  23. _resourceHolder = new Sprite();
  24. _screenShooter = new ScreenShooter();
  25. this.addChild(_backDisplay);
  26. this.addChild(_frontDisplay);
  27. _frontDisplay.alpha = 0;
  28. }
  29. public function Shoot(disp1:DisplayObject, disp2:DisplayObject)//截屏
  30. {
  31. var tempContainer:Sprite = new Sprite();
  32. tempContainer.addChild(disp1);
  33. tempContainer.addChild(disp2);
  34. _resourceHolder=_screenShooter.ShootScreen(tempContainer);
  35. }
  36. public function TurnDark()
  37. {
  38. if (_maskHolder!=null&&_maskHolder.hasEventListener(Event.ENTER_FRAME))
  39. {
  40. try { _maskHolder.removeEventListener(Event.ENTER_FRAME, OnTurnDark); } catch(er:Error) { }
  41. try { _maskHolder.removeEventListener(Event.ENTER_FRAME, OnTurnBright); }catch(er:Error){}
  42. }
  43. while (_frontDisplay.numChildren > 0) { _frontDisplay.removeChildAt(0); }
  44. _maskHolder = new Sprite();
  45. _maskHolder.graphics.beginFill(0x000000);
  46. _maskHolder.graphics.drawRect(0, 0, 960, 540);
  47. _maskHolder.graphics.endFill();
  48. _frontDisplay.addChild(_maskHolder);
  49. _frontDisplay.alpha = 0;
  50. _maskHolder.addEventListener(Event.ENTER_FRAME, OnTurnDark);
  51. }
  52. private function OnTurnDark(evt:Event)
  53. {
  54. _frontDisplay.alpha += 0.15;
  55. if (_frontDisplay.alpha >= 0.7)
  56. {
  57. _frontDisplay.alpha = 0.8;
  58. _maskHolder.removeEventListener(Event.ENTER_FRAME, OnTurnDark);
  59. }
  60. }
  61. public function TurnBright()
  62. {
  63. if (_maskHolder!=null&&_maskHolder.hasEventListener(Event.ENTER_FRAME))
  64. {
  65. try { _maskHolder.removeEventListener(Event.ENTER_FRAME, OnTurnDark); } catch(er:Error) { }
  66. try { _maskHolder.removeEventListener(Event.ENTER_FRAME, OnTurnBright); }catch(er:Error){}
  67. }
  68. if (_frontDisplay.numChildren < 1) { return; }
  69. _maskHolder.addEventListener(Event.ENTER_FRAME, OnTurnBright);
  70. }
  71. private function OnTurnBright(evt:Event)
  72. {
  73. _frontDisplay.alpha -= 0.15;
  74. if (_frontDisplay.alpha < 0.2)
  75. {
  76. _frontDisplay.alpha = 0;
  77. if(_frontDisplay.contains(_maskHolder))
  78. _frontDisplay.removeChild(_maskHolder);
  79. _maskHolder.removeEventListener(Event.ENTER_FRAME, OnTurnBright);
  80. }
  81. }
  82. public function UnShowAndTurnDark()
  83. {
  84. _maskHolder = new Sprite();
  85. _maskHolder.graphics.beginFill(0x000000);
  86. _maskHolder.graphics.drawRect(0, 0, _resourceHolder.width, _resourceHolder.height);
  87. _maskHolder.graphics.endFill();
  88. if (_frontDisplay.numChildren>0) { _frontDisplay.removeChildAt(0); }
  89. _frontDisplay.addChild(_maskHolder);
  90. if (_backDisplay.numChildren>0) { _backDisplay.removeChildAt(0); }
  91. _backDisplay.addChild(_resourceHolder);
  92. _triggedTime = (new Date()).time;
  93. this.addEventListener(Event.ENTER_FRAME, UnShowAndTurnDarkEveryFrame);
  94. }
  95. private function UnShowAndTurnDarkEveryFrame(evt:Event)//渐黑
  96. {
  97. var tempTime:Date = new Date();
  98. if (_triggedTime+1000 > tempTime.time)//当在工作范围内
  99. {
  100. _frontDisplay.alpha = (1 - ((_triggedTime+1000) - tempTime.time) / 1000)*0.5;
  101. }
  102. else
  103. {
  104. this.removeEventListener(Event.ENTER_FRAME,UnShowAndTurnDarkEveryFrame);//终止这个函数
  105. }
  106. }
  107. public function ShowAndTurnBright()
  108. {
  109. if (_backDisplay.numChildren>0) { _backDisplay.removeChildAt(0); }
  110. _triggedTime = (new Date()).time;
  111. this.addEventListener(Event.ENTER_FRAME, ShowAndTurnBrightEveryFrame);
  112. }
  113. private function ShowAndTurnBrightEveryFrame(evt:Event)
  114. {
  115. var tempTime:Date = new Date();
  116. if (_triggedTime+1000 > tempTime.time)//当在工作范围内
  117. {
  118. _frontDisplay.alpha = (((_triggedTime+1000) - tempTime.time) / 1000) * 0.5;
  119. }
  120. else
  121. {
  122. this.removeEventListener(Event.ENTER_FRAME, ShowAndTurnBrightEveryFrame);//终止这个函数
  123. if (_frontDisplay.numChildren>0) { _frontDisplay.removeChildAt(0); }
  124. }
  125. }
  126. }
  127. }