On monday’s afternoon waiting a C++ lesson, I surf on the web. But there aren’t a lot of news, so in a fully bother I go on a flash game’s website and I play a game with Starship, asteroid… I’m sure you know that kind of game 😉 And I ask myself about it mechanism.
After some research I discover the term “Starfield” but no explanation nowhere about how it works, therefore I decide to programme one !
So in fact a Starfield is a perpetual movement of Stars in a restrected field. I know this a little short 😀
The idea is to have a movement in your field and only there, so if a Star go away it should be deleted or puted back. And in fact that the field itself which is moving on the stage.
There is all the code below, but look at the application before !
Click on the animation and use arrow’s keyboard for moving the Starship. In this example the Starship never moves, it’s the Starfield !!
First Stars’ classes, I know they aren’t pretty but I don’t care :p
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package { import flash.display.Shape; import flash.filters.BlurFilter; /** * @author Aymeric */ public class Star extends Shape { protected var _vitesseX:Number; protected var _vitesseY:Number; public function Star(couleur:uint, opacite:Number) { this.graphics.beginFill(couleur, opacite); this.graphics.drawCircle(0, 0, Math.random() * 4); this.graphics.endFill(); this.filters = [new BlurFilter()]; _vitesseX = Math.random() * 3; _vitesseY = Math.random() * 3; } public function get vitesseX():Number { return _vitesseX; } public function get vitesseY():Number { return _vitesseY; } } } |
And after, the main class : take a look on Keyboards event how they allow diagonal movement ! Sinus and Cosinus’ function are really useful to change speed depending angle. Sinus for x position because sin±( 0, 180, 360) = 0 and at first Starship’s position is vertical with angle = 0. Cosinus for y position : cos±(0, 180, 360) = ±1, max speed at first !
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | package { import flash.display.DisplayObject; import flash.display.MovieClip; import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; import flash.ui.Keyboard; /** * @author Aymeric */ public class GalacticWars extends Sprite { private const MAX_STARS:uint = 250; private var starfield:MovieClip; private var container:Sprite; private var vitesseEtoileX:Array; private var vitesseEtoileY:Array; private var gauche:Boolean = false; private var droite:Boolean = false; private var haut:Boolean = false; private var bas:Boolean = false; private var vitesseX:int = 15; private var vitesseY:int = 15; public function GalacticWars() { starfield = new Starfield(); addChild(starfield); starfield.x = stage.stageWidth/2; starfield.y = stage.stageHeight/2; addStars(); stage.addEventListener(KeyboardEvent.KEY_DOWN, toucheDown); stage.addEventListener(KeyboardEvent.KEY_UP, toucheUP); stage.addEventListener(Event.ENTER_FRAME, deplacement); } private function addStars():void { container = new Sprite(); addChild(container); var star:Star; vitesseEtoileX = new Array(); vitesseEtoileY = new Array(); for (var i:uint = 0; i < MAX_STARS; i++) { star = new Star(Math.random() * 0xFFFFFF, Math.random()); star.name = "star" + i; vitesseEtoileX.push(star.vitesseX); vitesseEtoileY.push(star.vitesseY); container.addChildAt(star, i); star.x = Math.random() * 600; star.y = Math.random() * 500; } } private function toucheDown(k:KeyboardEvent):void { if (k.keyCode == Keyboard.LEFT) { gauche = true; } if (k.keyCode == Keyboard.RIGHT) { droite = true; } if (k.keyCode == Keyboard.UP) { haut = true; } if (k.keyCode == Keyboard.DOWN) { bas = true; } } private function toucheUP(k:KeyboardEvent):void { if (k.keyCode == Keyboard.LEFT) { gauche = false; } if (k.keyCode == Keyboard.RIGHT) { droite = false; } if (k.keyCode == Keyboard.UP) { haut = false; } if (k.keyCode == Keyboard.DOWN) { bas = false; } } private function deplacement(e:Event):void { if (gauche) { starfield.rotationZ -= 15; if (starfield.rotationZ < -360) { starfield.rotationZ = 0; } } if (droite) { starfield.rotationZ += 15; if (starfield.rotationZ > 360) { starfield.rotationZ = 0; } } if (haut) { container.x -= (vitesseX * Math.sin(starfield.rotationZ * Math.PI / 180)); container.y += (vitesseY * Math.cos(starfield.rotationZ * Math.PI / 180)); } if (bas) { container.x += (vitesseX * Math.sin(starfield.rotationZ * Math.PI / 180)); container.y -= (vitesseY * Math.cos(starfield.rotationZ * Math.PI / 180)); } // Stars' perpetual movement for (var i:uint = 0; i < MAX_STARS; i++) { container.getChildByName("star" + i).x -= vitesseEtoileX[i]; container.getChildByName("star" + i).y += vitesseEtoileY[i]; limites(container.getChildByName("star" + i)); } } private function limites(etoile:DisplayObject):void { if (etoile.x < -container.x) { etoile.x = -container.x + stage.stageWidth; } if (etoile.x > stage.stageWidth - container.x) { etoile.x = -container.x; } if (etoile.y < -container.y) { etoile.y = -container.y + stage.stageHeight; } if (etoile.y > stage.stageHeight - container.y) { etoile.y = -container.y; } } } } |
Maybe have you seen the words GalacticWars ? A game is coming ?
Stay tuned ! 🙂