/Nd2d-example/tests/StarFieldTest.as

https://bitbucket.org/HopeSky/mars_nd2d · ActionScript · 85 lines · 40 code · 16 blank · 29 comment · 0 complexity · e5c65eb144859ffa7464a3c362e14bb9 MD5 · raw file

  1. /*
  2. * ND2D - A Flash Molehill GPU accelerated 2D engine
  3. *
  4. * Author: Lars Gerckens
  5. * Copyright (c) nulldesign 2011
  6. * Repository URL: http://github.com/nulldesign/nd2d
  7. * Getting started: https://github.com/nulldesign/nd2d/wiki
  8. *
  9. *
  10. * Licence Agreement
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a copy
  13. * of this software and associated documentation files (the "Software"), to deal
  14. * in the Software without restriction, including without limitation the rights
  15. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. * copies of the Software, and to permit persons to whom the Software is
  17. * furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included in
  20. * all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. * THE SOFTWARE.
  29. */
  30. package tests {
  31. import de.nulldesign.nd2d.display.Scene2D;
  32. import de.nulldesign.nd2d.display.Sprite2D;
  33. import de.nulldesign.nd2d.materials.BlendModePresets;
  34. import de.nulldesign.nd2d.materials.texture.Texture2D;
  35. import flash.events.Event;
  36. public class StarFieldTest extends Scene2D {
  37. [Embed(source="/assets/starfield.jpg")]
  38. private var starFieldTexture:Class;
  39. [Embed(source="/assets/starfield.png")]
  40. private var starFieldTexture2:Class;
  41. private var starfield1:Sprite2D;
  42. private var starfield2:Sprite2D;
  43. public function StarFieldTest() {
  44. addEventListener(Event.ADDED_TO_STAGE, addedToStage);
  45. starfield1 = new Sprite2D(Texture2D.textureFromBitmapData(new starFieldTexture().bitmapData));
  46. addChild(starfield1);
  47. starfield2 = new Sprite2D(Texture2D.textureFromBitmapData(new starFieldTexture2().bitmapData));
  48. starfield2.blendMode = BlendModePresets.ADD_PREMULTIPLIED_ALPHA;
  49. addChild(starfield2);
  50. }
  51. private function addedToStage(e:Event):void {
  52. removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
  53. starfield1.width = stage.stageWidth;
  54. starfield1.scaleY = starfield1.scaleX;
  55. starfield1.x = stage.stageWidth / 2;
  56. starfield1.y = stage.stageHeight / 2;
  57. starfield2.width = stage.stageWidth;
  58. starfield2.scaleY = starfield2.scaleX;
  59. starfield2.x = stage.stageWidth / 2;
  60. starfield2.y = stage.stageHeight / 2;
  61. }
  62. override protected function step(elapsed:Number):void {
  63. starfield1.material.uvOffsetX -= (stage.stageWidth * 0.5 - mouseX) * 0.00002;
  64. starfield1.material.uvOffsetY -= (stage.stageHeight * 0.5 - mouseY) * 0.00002;
  65. starfield2.material.uvOffsetX -= (stage.stageWidth * 0.5 - mouseX) * 0.00004;
  66. starfield2.material.uvOffsetY -= (stage.stageHeight * 0.5 - mouseY) * 0.00004;
  67. }
  68. }
  69. }