{"id":167,"date":"2009-01-25T00:00:29","date_gmt":"2009-01-25T07:00:29","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=590"},"modified":"2009-01-25T00:00:29","modified_gmt":"2009-01-25T07:00:29","slug":"glitch-auto-asteroids","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=167","title":{"rendered":"Glitch Auto-asteroids"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n&#x5B;SWF(width=600, height=400, backgroundColor=0x000000, frameRate=30)]\r\n\/\/ make some ships\r\nfor (var i:int = 0; i&lt;10; i++) makeShip();\r\n\/\/\r\nfunction makeShip():void {\r\n\t\/\/ setup vars\r\n\tvar char:Object ={posX:Math.random()*stage.stageWidth, posY:Math.random()*stage.stageHeight, velX:0, velY:0, rad:4, theta:Math.random()*6.28, thetaDest:Math.random()*6.28, mc: MovieClip(addChild(new MovieClip()))}\r\n\t\/\/ draw and position the char\r\n\twith(char.mc) graphics.lineStyle(0,0xFFFFFF), graphics.moveTo(-15, -5), graphics.lineTo(0, 0), graphics.lineTo(-15, 5), x=char.posX, y=char.posY;\r\n    \/\/ loop\r\n    addEventListener(Event.ENTER_FRAME, function(){\r\n\t\t\/\/ change position based on velocity, .9 friction\r\n\t\twith (char) posX += velX *= .9, posY += velY *= .9;\r\n\t\t\/\/ randomize radius and theta\r\n\t\tif (int(Math.random()*10) == 1) char.thetaDest += Math.random()*2-1;\r\n\t\tif (int(Math.random()*90) == 1) char.rad = Math.random()*4 + 4\r\n\t\t\/\/ apply changes to velocity\r\n\t\tchar.theta += (char.thetaDest - char.theta) \/ 12;\r\n\t\tchar.velX = char.rad * Math.cos(char.theta);\r\n\t\tchar.velY = char.rad * Math.sin(char.theta);\r\n\t\t\/\/ screen bounds\r\n\t\tif (char.posX &gt; 620) char.posX = -20;\r\n\t\tif (char.posX &lt; -20) char.posX = 620;\r\n\t\tif (char.posY &gt; 420) char.posY = -20;\r\n\t\tif (char.posY &lt; -20) char.posY = 420;\r\n\t\t\/\/ rotation\r\n\t\tchar.mc.rotation = (Math.abs(char.mc.y - char.posY)&gt;.7 || Math.abs(char.mc.x - char.posX) &gt; .7) ?  Math.atan2(char.posY - char.mc.y,char.posX -  char.mc.x) \/ Math.PI * 180 : char.mc.rotation;\r\n\t\t\/\/ shoot\r\n\t\tif(int(Math.random()*100) &lt; 20) shootBullet(char, 5 * Math.cos(char.mc.rotation * Math.PI \/ 180)+ char.velX, 5 * Math.sin(char.mc.rotation * Math.PI \/ 180) + char.velY);\r\n\t\t\/\/ apply position\r\n\t\twith(char) mc.x = posX, mc.y = posY;\r\n\t\t});\r\n}\r\n\/\/ \r\nfunction shootBullet(char:Object, vx:Number, vy:Number):void{ \r\n    var b:MovieClip = MovieClip(addChild(new MovieClip()));\r\n\tb.life = 0;\r\n\twith(b) graphics.beginFill(0xFFFFFF), graphics.drawCircle(0,0,2), x = char.mc.x, y = char.mc.y;\r\n\tb.addEventListener(Event.ENTER_FRAME, function(){\r\n\t\t\twith (b) x += vx, y += vy, life++;\r\n\t\t\tif (b.life &gt; 30) b.removeEventListener(Event.ENTER_FRAME, arguments.callee), removeChild(b);\r\n\t});\r\n}\r\n<\/pre>\n<p>An asteroids inspired code snippet. <a data-asruffle-linked=\"true\" href=\"https:\/\/actionsnippet.com\/swfs\/glitch_asteroids.html\" target=\"blank\">See the swf here.<\/a> <span class=\"asruffle-link\" data-swf=\"{&quot;url&quot;:&quot;https:\\\/\\\/actionsnippet.com\\\/swfs\\\/glitch_asteroids.swf&quot;,&quot;title&quot;:&quot;glitch asteroids&quot;,&quot;width&quot;:600,&quot;height&quot;:400,&quot;missing&quot;:false,&quot;allowScriptAccess&quot;:false,&quot;backgroundColor&quot;:&quot;#000000&quot;}\"><button type=\"button\" hidden aria-expanded=\"false\">Play demo<span class=\"screen-reader-text\">: glitch asteroids<\/span><\/button><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#x5B;SWF(width=600, height=400, backgroundColor=0x000000, frameRate=30)] \/\/ make some ships for (var i:int = 0; i&lt;10; i++) makeShip(); \/\/ function makeShip():void { \/\/ setup vars var char:Object ={posX:Math.random()*stage.stageWidth, posY:Math.random()*stage.stageHeight, velX:0, velY:0, rad:4, theta:Math.random()*6.28, thetaDest:Math.random()*6.28, mc: MovieClip(addChild(new MovieClip()))} \/\/ draw and position the char with(char.mc) graphics.lineStyle(0,0xFFFFFF), graphics.moveTo(-15, -5), graphics.lineTo(0, 0), graphics.lineTo(-15, 5), x=char.posX, y=char.posY; \/\/ loop addEventListener(Event.ENTER_FRAME, function(){ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[94,13],"tags":[3,4],"class_list":["post-167","post","type-post","status-publish","format-standard","hentry","category-misc","category-motion","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=167"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/167\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}