{"id":457,"date":"2010-01-04T14:16:50","date_gmt":"2010-01-04T21:16:50","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=2559"},"modified":"2010-01-04T14:16:50","modified_gmt":"2010-01-04T21:16:50","slug":"too-many-buttons","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=457","title":{"rendered":"Too Many Buttons"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n&#x5B;SWF (width = 500, height = 500)]\r\n\r\nvar canvas:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xFFFFFF);\r\naddChild(new Bitmap(canvas));\r\n\r\nvar indexCanvas:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xFFFFFF);\r\n\r\nvar btnNum:int = 5000;\r\nvar info:Array = &#x5B;];\r\n\r\nvar brush:BitmapData = new BitmapData(10,10,false, 0xCCCCCC);\r\nvar border:Shape = new Shape();\r\nborder.graphics.lineStyle(2, 0x000000);\r\nborder.graphics.drawRect(0,0,10,10);\r\nbrush.draw(border);\r\n\r\nvar txt:TextField = TextField(addChild(new TextField()));\r\nwith (txt) height = 20, width = 50, background = 0xFFFFFF, selectable = false\r\nvar tf:TextFormat = new TextFormat();\r\ntf.align = TextFormatAlign.RIGHT;\r\ntxt.border= true;\r\ntxt.defaultTextFormat = tf;\r\n\r\nvar redRect:Shape = Shape(addChild(new Shape()));\r\nwith (redRect.graphics) beginFill(0xFF0000), drawRect(0,0,10,10);\r\n\t\t\t\t\t\t\t\t\t\t\t  \r\nvar pnt:Point = new Point();\r\nvar r:Rectangle = new Rectangle(0,0,10,10);\r\nfor (var i:int = 0; i &lt; btnNum; i++){\r\n\tpnt.x = r.x = int(Math.random() * stage.stageWidth);\r\n\tpnt.y = r.y = int(Math.random() * stage.stageHeight); \r\n\tindexCanvas.fillRect(r, i);\r\n\tcanvas.copyPixels(brush, brush.rect, pnt)\r\n\tinfo&#x5B;i] = &#x5B;r.x, r.y, i];\t\r\n}\r\n\r\naddEventListener(Event.ENTER_FRAME, onCheckBtns);\r\nfunction onCheckBtns(evt:Event):void{\r\n   var currentIndex:int = indexCanvas.getPixel(mouseX, mouseY);\r\n   if (currentIndex != 0xFFFFFF){\r\n\t var currentBox:Array = info&#x5B;currentIndex]\r\n\t redRect.visible = true;\r\n\t redRect.x = currentBox&#x5B;0];\r\n\t txt.y = redRect.y = currentBox&#x5B;1];\r\n\t if (mouseX &lt; txt.width){\r\n\t\t tf.align = TextFormatAlign.LEFT;\r\n\t\t txt.defaultTextFormat = tf;\r\n\t\t txt.x = redRect.x + 10;\r\n\t }else{\r\n\t\t tf.align = TextFormatAlign.RIGHT;\r\n\t\t txt.defaultTextFormat = tf;\r\n\t\t txt.x = redRect.x - txt.width;\r\n\t }\r\n\t txt.text = currentBox&#x5B;2];\r\n\t txt.visible = true;\r\n   }else{\r\n\t redRect.visible = false; \r\n\t txt.visible = false;\r\n   }\r\n}\r\n<\/pre>\n<p>This is a simplified example of the technique discussed in <a href=\"https:\/\/actionsnippet.com\/?p=2555\">yesterdays post<\/a>. The idea is to use a BitmapData image to store index values for a large number of elements that need to be able to act as if the have MouseEvents. For a more detailed description of this technique <a href=\"https:\/\/actionsnippet.com\/?p=2555\">see yesterdays post<\/a>.<\/p>\n<p> <a href=\"http:\/\/wonderfl.net\/code\/0eb86c863ce7fbbe0b692df96c8f23d4b78ada5c\" target=\"blank\">Have a look at the swf on wonderfl<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#x5B;SWF (width = 500, height = 500)] var canvas:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xFFFFFF); addChild(new Bitmap(canvas)); var indexCanvas:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xFFFFFF); var btnNum:int = 5000; var info:Array = &#x5B;]; var brush:BitmapData = new BitmapData(10,10,false, 0xCCCCCC); var border:Shape = new Shape(); border.graphics.lineStyle(2, 0x000000); border.graphics.drawRect(0,0,10,10); brush.draw(border); var txt:TextField = TextField(addChild(new TextField())); with (txt) [&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,24,94],"tags":[3,14,4],"class_list":["post-457","post","type-post","status-publish","format-standard","hentry","category-arrays","category-bitmapdata","category-misc","tag-actionscript","tag-as3","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/457","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=457"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/457\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}