{"id":70,"date":"2010-03-10T20:13:48","date_gmt":"2010-03-10T19:13:48","guid":{"rendered":"http:\/\/www.aymericlamboley.fr\/blog\/?p=70"},"modified":"2014-11-01T15:13:22","modified_gmt":"2014-11-01T14:13:22","slug":"starfield-for-games","status":"publish","type":"post","link":"http:\/\/www.aymericlamboley.fr\/blog\/starfield-for-games\/","title":{"rendered":"Starfield for games"},"content":{"rendered":"<p>On monday&#8217;s afternoon waiting a C++ lesson, I surf on the web. But there aren&#8217;t a lot of news, so in a fully bother I go on a flash game&#8217;s website and I play a game with Starship, asteroid&#8230; I&#8217;m sure you know that kind of game \ud83d\ude09 And I ask myself about it mechanism.<\/p>\n<p>After some research I discover the term &#8220;Starfield&#8221; but no explanation nowhere about how it works, therefore I decide to programme one !<br \/>\nSo in fact a Starfield is a perpetual movement of Stars in a restrected field. I know this a little short \ud83d\ude00<br \/>\nThe 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.<\/p>\n<p>There is all the code below, but look at the <a href=\"http:\/\/www.aymericlamboley.fr\/blog\/wp-contents\/upload\/2010\/03\/Starfield.swf\" rel=\"lightbox[flash 600 500]\" title=\"application\"><strong>application<\/strong><\/a> before !<br \/>\nClick on the animation and use arrow&#8217;s keyboard for moving the Starship. In this example the Starship never moves, it&#8217;s the Starfield !!<\/p>\n<p><!--more--><\/p>\n<p>First Stars&#8217; classes, I know they aren&#8217;t pretty but I don&#8217;t care :p<\/p>\n<pre lang=\"actionscript3\" line=\"1\">package {\r\n\timport flash.display.Shape;\r\n\timport flash.filters.BlurFilter;\r\n\r\n\t\/**\r\n\t * @author Aymeric\r\n\t *\/\r\n\tpublic class Star extends Shape {\r\n\t\t\r\n\t\tprotected var _vitesseX:Number;\r\n\t\tprotected var _vitesseY:Number;\r\n\t\t\r\n\t\tpublic function Star(couleur:uint, opacite:Number) {\r\n\t\t\t\r\n\t\t\tthis.graphics.beginFill(couleur, opacite);\r\n\t\t\tthis.graphics.drawCircle(0, 0, Math.random() * 4);\r\n\t\t\tthis.graphics.endFill();\r\n\t\t\t\r\n\t\t\tthis.filters = [new BlurFilter()];\r\n\t\t\t\r\n\t\t\t_vitesseX = Math.random() * 3;\r\n\t\t\t_vitesseY = Math.random() * 3;\r\n\t\t}\r\n\t\t\r\n\t\tpublic function get vitesseX():Number {\r\n\t\t\treturn _vitesseX;\r\n\t\t}\r\n\t\t\r\n\t\tpublic function get vitesseY():Number {\r\n\t\t\treturn _vitesseY;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>And after, the main class : take a look on Keyboards event how they allow diagonal movement ! Sinus and Cosinus&#8217; function are really useful to change speed depending angle. Sinus for x position because sin\u00b1( 0, 180, 360) = 0 and at first Starship&#8217;s position is vertical with angle = 0. Cosinus for y position : cos\u00b1(0, 180, 360) = \u00b11, max speed at first !<\/p>\n<pre lang=\"actionscript3\" line=\"1\">package {\r\n\timport flash.display.DisplayObject;\r\n\timport flash.display.MovieClip;\r\n\timport flash.display.Sprite;\r\n\timport flash.events.Event;\r\n\timport flash.events.KeyboardEvent;\r\n\timport flash.ui.Keyboard;\r\n\r\n\t\/**\r\n\t * @author Aymeric\r\n\t *\/\r\n\tpublic class GalacticWars extends Sprite {\r\n\t\t\r\n\t\tprivate const MAX_STARS:uint = 250;\r\n\t\t\r\n\t\tprivate var starfield:MovieClip;\r\n\t\t\r\n\t\tprivate var container:Sprite;\r\n\t\tprivate var vitesseEtoileX:Array;\r\n\t\tprivate var vitesseEtoileY:Array;\r\n\t\t\r\n\t\tprivate var gauche:Boolean = false;\r\n\t\tprivate var droite:Boolean = false;\r\n\t\tprivate var haut:Boolean = false;\r\n\t\tprivate var bas:Boolean = false;\r\n\t\tprivate var vitesseX:int = 15;\r\n\t\tprivate var vitesseY:int = 15;\r\n\r\n\t\tpublic function GalacticWars() {\r\n\t\t\t\r\n\t\t\tstarfield = new Starfield();\r\n\t\t\taddChild(starfield);\r\n\t\t\tstarfield.x = stage.stageWidth\/2;\r\n\t\t\tstarfield.y = stage.stageHeight\/2;\r\n\t\t\t\r\n\t\t\taddStars();\r\n\t\t\tstage.addEventListener(KeyboardEvent.KEY_DOWN, toucheDown);\r\n\t\t\tstage.addEventListener(KeyboardEvent.KEY_UP, toucheUP);\r\n\t\t\tstage.addEventListener(Event.ENTER_FRAME, deplacement);\r\n\t\t}\r\n\t\t\r\n\t\tprivate function addStars():void {\r\n\t\t\t\r\n\t\t\tcontainer = new Sprite();\r\n\t\t\taddChild(container);\r\n\t\t\t\r\n\t\t\tvar star:Star;\r\n\t\t\tvitesseEtoileX = new Array();\r\n\t\t\tvitesseEtoileY = new Array();\r\n\t\t\t\r\n\t\t\tfor (var i:uint = 0; i < MAX_STARS; i++) {\r\n\t\t\t\tstar = new Star(Math.random() * 0xFFFFFF, Math.random());\r\n\t\t\t\tstar.name = \"star\" + i;\r\n\t\t\t\tvitesseEtoileX.push(star.vitesseX);\r\n\t\t\t\tvitesseEtoileY.push(star.vitesseY);\r\n\t\t\t\tcontainer.addChildAt(star, i);\r\n\t\t\t\tstar.x = Math.random() * 600;\r\n\t\t\t\tstar.y = Math.random() * 500;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tprivate function toucheDown(k:KeyboardEvent):void {\r\n\t\t\t\r\n\t\t\tif (k.keyCode == Keyboard.LEFT) {\r\n\t\t\t\tgauche = true;\r\n\t\t\t}\r\n\t\t\tif (k.keyCode == Keyboard.RIGHT) {\r\n\t\t\t\tdroite = true;\r\n\t\t\t}\r\n\t\t\tif (k.keyCode == Keyboard.UP) {\r\n\t\t\t\thaut = true;\r\n\t\t\t}\r\n\t\t\tif (k.keyCode == Keyboard.DOWN) {\r\n\t\t\t\tbas = true;\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\tprivate function toucheUP(k:KeyboardEvent):void {\r\n\t\t\t\r\n\t\t\tif (k.keyCode == Keyboard.LEFT) {\r\n\t\t\t\tgauche = false;\r\n\t\t\t}\r\n\t\t\tif (k.keyCode == Keyboard.RIGHT) {\r\n\t\t\t\tdroite = false;\r\n\t\t\t}\r\n\t\t\tif (k.keyCode == Keyboard.UP) {\r\n\t\t\t\thaut = false;\r\n\t\t\t}\r\n\t\t\tif (k.keyCode == Keyboard.DOWN) {\r\n\t\t\t\tbas = false;\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\tprivate function deplacement(e:Event):void {\r\n\t\t\t\r\n\t\t\tif (gauche) {\r\n\t\t\t\tstarfield.rotationZ -= 15;\r\n\t\t\t\tif (starfield.rotationZ < -360) {\r\n\t\t\t\t\tstarfield.rotationZ = 0;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tif (droite) {\r\n\t\t\t\tstarfield.rotationZ += 15;\r\n\t\t\t\tif (starfield.rotationZ > 360) {\r\n\t\t\t\t\tstarfield.rotationZ = 0;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tif (haut) {\r\n\r\n\t\t\t\tcontainer.x -= (vitesseX * Math.sin(starfield.rotationZ * Math.PI \/ 180));\r\n\t\t\t\tcontainer.y += (vitesseY * Math.cos(starfield.rotationZ * Math.PI \/ 180));\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tif (bas) {\r\n\t\t\t\t\r\n\t\t\t\tcontainer.x += (vitesseX * Math.sin(starfield.rotationZ * Math.PI \/ 180));\r\n\t\t\t\tcontainer.y -= (vitesseY * Math.cos(starfield.rotationZ * Math.PI \/ 180));\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t\/\/ Stars' perpetual movement\r\n\t\t\tfor (var i:uint = 0; i < MAX_STARS; i++) {\r\n\t\t\t\tcontainer.getChildByName(\"star\" + i).x -= vitesseEtoileX[i];\r\n\t\t\t\tcontainer.getChildByName(\"star\" + i).y += vitesseEtoileY[i];\r\n\t\t\t\tlimites(container.getChildByName(\"star\" + i));\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\tprivate function limites(etoile:DisplayObject):void {\r\n\t\t\t\r\n\t\t\tif (etoile.x < -container.x) {\r\n\t\t\t\tetoile.x = -container.x + stage.stageWidth;\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tif (etoile.x > stage.stageWidth - container.x) {\r\n\t\t\t\tetoile.x = -container.x;\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tif (etoile.y < -container.y) {\r\n\t\t\t\tetoile.y = -container.y + stage.stageHeight;\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tif (etoile.y > stage.stageHeight - container.y) {\r\n\t\t\t\tetoile.y = -container.y;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>Maybe have you seen the words GalacticWars ? A game is coming ?<br \/>\nStay tuned ! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On monday&#8217;s afternoon waiting a C++ lesson, I surf on the web. But there aren&#8217;t a lot of news, so in a fully bother I go on a flash game&#8217;s website and I play a game with Starship, asteroid&#8230; I&#8217;m sure you know that kind of game \ud83d\ude09 And I ask myself about it mechanism. &hellip; <a href=\"http:\/\/www.aymericlamboley.fr\/blog\/starfield-for-games\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Starfield for games<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[4],"tags":[],"_links":{"self":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/70"}],"collection":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/comments?post=70"}],"version-history":[{"count":8,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"predecessor-version":[{"id":1308,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/70\/revisions\/1308"}],"wp:attachment":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}