{"id":173,"date":"2009-01-31T00:00:13","date_gmt":"2009-01-31T07:00:13","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=642"},"modified":"2009-01-31T00:00:13","modified_gmt":"2009-01-31T07:00:13","slug":"non-snippet-wandering-robots","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=173","title":{"rendered":"Not a Snippet (wandering robots)"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\npackage {\r\n\timport flash.display.Sprite;\r\n    \r\n    &#x5B;SWF(width=500, height=500, backgroundColor=0xEFEFEF, frameRate=30)]\r\n\tpublic class Main extends Sprite{\r\n\t\tprivate var _robotManager:RobotManager;\r\n\t\tpublic function Main(){\r\n\t\t  _robotManager = new RobotManager(this, 12);\r\n\t\t}\r\n\t}\r\n}\r\n\r\nclass RobotManager{\r\n\t\r\n\tprivate var _robots:Array;\r\n\tprivate var _top:Sprite;\r\n\tprivate var _robotNum:int;\r\n\t\r\n    public function RobotManager(top:Sprite, robotNum:int){\r\n    \t_top = top;\r\n    \t_robotNum = robotNum;\r\n    \t_robots = new Array();\r\n    \tsetupRobots();\r\n    \ttop.addEventListener(Event.ENTER_FRAME, onRun);\r\n    }\r\n    \r\n    private function setupRobots():void{\r\n    \tfor (var i:int = 0; i&lt;_robotNum; i++){\r\n    \t\t_robots&#x5B;i] = new Robot();\r\n    \t\t_robots&#x5B;i].x = 100+(i % 3) * 150;\r\n    \t\t_robots&#x5B;i].y = 100+int(i \/ 3) * 100;\r\n    \t\t_robots&#x5B;i].addEventListener(&quot;death&quot;, onRobotDie);\r\n    \t\t_top.addChild(_robots&#x5B;i]);\r\n    \t}\r\n    }\r\n    private function onRobotDie(evt:Event):void{\r\n    \tfor (var i:int = 0; i&lt;_robotNum; i++){\r\n    \t\tif (_robots&#x5B;i] == evt.currentTarget){\r\n    \t\t\t_robots.splice(i, 1);\r\n    \t\t}\r\n    \t}\r\n    \t_robotNum--;\r\n    }\r\n    \r\n    private function onRun(evt:Event):void{\r\n    \tcollisions();\r\n    }\r\n    \r\n    private function collisions():void{\r\n    \tvar currBot:Robot;\r\n    \tfor (var i:int = 0; i&lt;_robotNum; i++){\r\n    \t\tcurrBot = _robots&#x5B;i];\r\n    \t\tfor (var j:int = 0; j&lt;_robotNum; j++){\r\n    \t\t\tif (currBot != _robots&#x5B;j]){\r\n    \t\t\t\tvar dx:Number = currBot.x - _robots&#x5B;j].x;\r\n    \t\t\t\tvar dy:Number = currBot.y - _robots&#x5B;j].y;\r\n    \t\t\t\tif (Math.sqrt((dx * dx) + (dy * dy)) &lt; 40){\r\n    \t\t\t\t\tcurrBot.die();\r\n    \t\t\t\t}\r\n    \t\t\t}\r\n    \t\t}\r\n    \t}\r\n    }\r\n}\r\n\r\nimport flash.display.*\r\nimport flash.events.*;\r\n\r\nclass Robot extends Sprite{\r\n\tprivate var _inc:Number\r\n\tprivate var _td:Number;\r\n\tprivate var _theta:Number;\r\n\tprivate var _rad:Number;\r\n\t\r\n\tpublic function Robot(){\r\n\t\t_theta = Math.random() * (Math.PI * 2);\r\n\t\t_td = _theta;\r\n\t\t_inc = Math.random() * .2 + .05;\r\n\t\t_rad = _inc * 10;\r\n\t\twith(graphics){\r\n\t\t\tbeginFill(0x666666);\r\n\t\t\tdrawRect(-10,-10,20,20);\r\n\t\t\tendFill();\r\n\t\t\tbeginFill(0x333333);\r\n\t\t\tdrawRect(-5, -5, 20, 10);\r\n\t\t\tlineStyle(0,0xAAAAAA);\r\n\t\t\tendFill();\r\n\t\t\tdrawCircle(0,0,20);\r\n\t\t}\r\n\t\taddEventListener(Event.ENTER_FRAME, onRun);\r\n\t}\r\n\t\r\n\tprivate function onRun(evt:Event):void{\r\n\t\t_td  += _inc;\r\n\t\t_theta += _inc * Math.cos(_td);\r\n\t\tif (x &lt; 0 || y &lt; 0 || x &gt; 500 || y &gt; 500){\r\n\t\t\t_theta += Math.PI;\r\n\t\t}\r\n\t\tx += _rad * Math.cos(_theta);\r\n\t\ty += _rad * Math.sin(_theta);\r\n\t\trotation = _theta \/ Math.PI * 180;\r\n\t}\r\n\t\r\n\tpublic function die():void{\r\n\t\tremoveEventListener(Event.ENTER_FRAME, onRun);\r\n\t\taddEventListener(Event.ENTER_FRAME, onDie);\r\n\t}\r\n\t\r\n\tprivate function onDie(evt:Event):void{\r\n\t\tscaleX = scaleY += .1;\r\n\t\talpha -= .1;\r\n\t\tif (scaleX &gt; 2){\r\n\t\t\tremoveEventListener(Event.ENTER_FRAME, onDie);\r\n\t\t\tif (parent){\r\n\t\t\t\tparent.removeChild(this);\r\n\t\t\t\tdispatchEvent(new Event(&quot;death&quot;));\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p><i>This code should be placed in a document class&#8230; not the timeline (hopefully you already know that)<\/i><\/p>\n<p>The above creates 12 wandering robots that die when they collide with one another.<\/p>\n<p>This is not a snippet really&#8230; but it&#8217;s only slightly over 100 lines and it contains a few tricks I find myself using from time to time.<\/p>\n<p>You can <a href=\"https:\/\/actionsnippet.com\/swfs\/bots\/\" target=\"blank\">view the swf here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>package { import flash.display.Sprite; &#x5B;SWF(width=500, height=500, backgroundColor=0xEFEFEF, frameRate=30)] public class Main extends Sprite{ private var _robotManager:RobotManager; public function Main(){ _robotManager = new RobotManager(this, 12); } } } class RobotManager{ private var _robots:Array; private var _top:Sprite; private var _robotNum:int; public function RobotManager(top:Sprite, robotNum:int){ _top = top; _robotNum = robotNum; _robots = new Array(); setupRobots(); top.addEventListener(Event.ENTER_FRAME, onRun); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,35,13,2],"tags":[3,119,4],"class_list":["post-173","post","type-post","status-publish","format-standard","hentry","category-arrays","category-events","category-motion","category-oop","tag-actionscript","tag-collision","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/173","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=173"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/173\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}