{"id":447,"date":"2009-12-01T11:20:01","date_gmt":"2009-12-01T18:20:01","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=2489"},"modified":"2009-12-01T11:20:01","modified_gmt":"2009-12-01T18:20:01","slug":"quickbox2d-mini-poly-editor","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=447","title":{"rendered":"QuickBox2D Mini-Poly Editor"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n&#x5B;SWF(width = 800, height = 600, frameRate = 60)]\r\nimport com.actionsnippet.qbox.*;\r\nstage.frameRate = 60;\r\n\r\nvar sim:QuickBox2D = new QuickBox2D(this);\r\n\r\nsim.createStageWalls();\r\n\r\nsim.start();\r\n\r\nvar output:TextField = new TextField();\r\noutput.text = &quot;Click anywhere to add points to a polygon. Hit any key to test.nn&quot;;\r\noutput.x = output.y = 50;\r\nwith(output) width = 300, height = 400, border = true, selectable = true, wordWrap = true, multiline = true;\r\naddChild(output);\r\n\r\nfunction display(str:*):void{\r\n\toutput.appendText(str.toString() + &quot;n&quot;);\r\n}\r\n\t\t\t\t\t\t\t\t\r\nvar points:Array = &#x5B;];\r\nvar poly:Shape = new Shape();\r\naddChild(poly);\r\n\r\nstage.addEventListener(MouseEvent.CLICK, onClick);\r\nstage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);\r\n\r\nfunction onClick(evt:MouseEvent):void {\r\n\tif (points.length == 0){\r\n\t\tpoly.graphics.beginFill(0xCCCCCC);\r\n\t\tpoly.graphics.lineStyle(1, 0xFF0000);\r\n\t\tpoly.graphics.moveTo(mouseX, mouseY);\r\n\t}else{\r\n\t\tpoly.graphics.lineTo(mouseX, mouseY);\r\n\t}\r\n\tpoly.graphics.drawCircle(mouseX, mouseY, 2);\r\n\t \r\n\tpoints.push(mouseX \/ 30.0, mouseY \/ 30.0);\r\n}\r\n\r\nfunction onKeyPressed(evt:KeyboardEvent):void {\r\n\t \/\/ average all points\r\n\t var avgX:Number=0\r\n\t var avgY:Number = 0;\r\n\t \r\n\t for (var i:int = 0; i&lt;points.length; i+=2){\r\n\t     avgX += points&#x5B;i];\r\n\t\t avgY += points&#x5B;i + 1];\r\n\t }\r\n\t\r\n\t avgX \/= points.length\/2;\r\n\t avgY \/=  points.length\/2;\r\n\t avgX = avgX;\r\n\t avgY = avgY;\r\n\t \r\n\t \/\/ subtract averages and fix decimal place\r\n\t  for (i = 0; i&lt;points.length; i+=2){\r\n\t\t  var yp:int = i + 1;\r\n\t      points&#x5B;i] -= avgX;\r\n\t\t  points&#x5B;yp] -= avgY;\r\n\t\t  points&#x5B;i] = Number(points&#x5B;i].toFixed(2));\r\n\t\t  points&#x5B;yp] = Number(points&#x5B;yp].toFixed(2));\r\n\t }\r\n\t \r\n\t display(&quot;points array:&quot;);\r\n\t display(points);\r\n\t \r\n\t try{\r\n\t\t var p:QuickObject = sim.addPoly({x:avgX, y:avgY, points:points});\r\n\t\t p.userData.graphics.beginFill(0xFF0000);\r\n\t\t p.userData.graphics.drawCircle(0,0,5);\r\n\t }catch(e:*){\r\n\t\tdisplay(&quot;Invalid polygon data!&quot;); \r\n\t }\r\n\t \r\n\t poly.graphics.clear();\r\n\t points = &#x5B;];\r\n}\r\n<\/pre>\n<p>This snippet shows the basic concepts needed to go about creating a polygon editor for QuickBox2D. I have an unreleased editor that I use for my QuickBox2D projects, at some point I may release it&#8230; but for now I figured I&#8217;d post this extremely simplified version for people to expand on.<\/p>\n<p><a data-asruffle-linked=\"true\" href=\"http:\/\/www.actionsnippet.com\/swfs\/polygon_editor.html\" target=\"blank\"><br \/>\nHave a look at the swf here&#8230;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/poly_editor.gif\" \/><\/a> <span class=\"asruffle-link\" data-swf=\"{&quot;url&quot;:&quot;https:\\\/\\\/actionsnippet.com\\\/swfs\\\/polygon_editor.swf&quot;,&quot;title&quot;:&quot;polygon editor&quot;,&quot;width&quot;:800,&quot;height&quot;:600,&quot;missing&quot;:false,&quot;allowScriptAccess&quot;:false,&quot;backgroundColor&quot;:&quot;#ffffff&quot;}\"><button type=\"button\" hidden aria-expanded=\"false\">Play demo<span class=\"screen-reader-text\">: polygon editor<\/span><\/button><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#x5B;SWF(width = 800, height = 600, frameRate = 60)] import com.actionsnippet.qbox.*; stage.frameRate = 60; var sim:QuickBox2D = new QuickBox2D(this); sim.createStageWalls(); sim.start(); var output:TextField = new TextField(); output.text = &quot;Click anywhere to add points to a polygon. Hit any key to test.nn&quot;; output.x = output.y = 50; with(output) width = 300, height = 400, border = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,12],"tags":[3,14,15,4,16],"class_list":["post-447","post","type-post","status-publish","format-standard","hentry","category-box2d","category-quickbox2d","tag-actionscript","tag-as3","tag-box2d","tag-flash","tag-quickbox2d"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/447","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=447"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/447\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}