/Nd2d-example/tests/StarFieldTest.as
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
31package tests {
32
33 import de.nulldesign.nd2d.display.Scene2D;
34 import de.nulldesign.nd2d.display.Sprite2D;
35 import de.nulldesign.nd2d.materials.BlendModePresets;
36 import de.nulldesign.nd2d.materials.texture.Texture2D;
37
38 import flash.events.Event;
39
40 public class StarFieldTest extends Scene2D {
41
42 [Embed(source="/assets/starfield.jpg")]
43 private var starFieldTexture:Class;
44
45 [Embed(source="/assets/starfield.png")]
46 private var starFieldTexture2:Class;
47
48 private var starfield1:Sprite2D;
49
50 private var starfield2:Sprite2D;
51
52 public function StarFieldTest() {
53
54 addEventListener(Event.ADDED_TO_STAGE, addedToStage);
55
56 starfield1 = new Sprite2D(Texture2D.textureFromBitmapData(new starFieldTexture().bitmapData));
57 addChild(starfield1);
58
59 starfield2 = new Sprite2D(Texture2D.textureFromBitmapData(new starFieldTexture2().bitmapData));
60 starfield2.blendMode = BlendModePresets.ADD_PREMULTIPLIED_ALPHA;
61 addChild(starfield2);
62 }
63
64 private function addedToStage(e:Event):void {
65 removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
66
67 starfield1.width = stage.stageWidth;
68 starfield1.scaleY = starfield1.scaleX;
69 starfield1.x = stage.stageWidth / 2;
70 starfield1.y = stage.stageHeight / 2;
71
72 starfield2.width = stage.stageWidth;
73 starfield2.scaleY = starfield2.scaleX;
74 starfield2.x = stage.stageWidth / 2;
75 starfield2.y = stage.stageHeight / 2;
76 }
77
78 override protected function step(elapsed:Number):void {
79 starfield1.material.uvOffsetX -= (stage.stageWidth * 0.5 - mouseX) * 0.00002;
80 starfield1.material.uvOffsetY -= (stage.stageHeight * 0.5 - mouseY) * 0.00002;
81 starfield2.material.uvOffsetX -= (stage.stageWidth * 0.5 - mouseX) * 0.00004;
82 starfield2.material.uvOffsetY -= (stage.stageHeight * 0.5 - mouseY) * 0.00004;
83 }
84 }
85}