{"id":279,"date":"2009-05-09T09:32:13","date_gmt":"2009-05-09T16:32:13","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1312"},"modified":"2009-05-09T09:32:13","modified_gmt":"2009-05-09T16:32:13","slug":"static-bitmapdata-references","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=279","title":{"rendered":"static BitmapData References"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\npackage{\r\n\t\r\n\timport flash.display.*;\r\n\timport flash.events.*;\r\n\timport flash.geom.*;\r\n\t\r\n\tpublic class QuickCheckBox extends Sprite{\r\n\t\t\r\n\t\tprivate var _checked:Boolean;\r\n\t\tprivate var _bitmap:Bitmap;\r\n\t\tprivate var _canvas:BitmapData;\r\n\t\t\r\n\t\tprivate static var checked:BitmapData;\r\n\t\tprivate static var unchecked:BitmapData;\r\n\t\tprivate static var bg:Shape;\r\n\t\tprivate static var ex:Shape;\r\n\t\tprivate static var pnt:Point;\r\n\t\t\r\n\t\t\/\/ static init for BitmapData and drawing\r\n\t\t{\r\n\t\t\ttrace(&quot;only render your graphics once&quot;);\r\n\t\t\tpnt = new Point(0,0);\r\n\t\t\tchecked = new BitmapData(10,10, true, 0x00000000);\r\n\t\t\tunchecked = new BitmapData(10, 10, true, 0x00000000);\r\n\t\t\tbg = new Shape();\r\n\t\t\twith(bg.graphics) lineStyle(0,0x000000), beginFill(0xEFEFEF), drawRect(0,0,9,9);\r\n\t\t\tunchecked.draw(bg);\r\n\t\t\tex = new Shape();\r\n\t\t\twith(ex.graphics) {\r\n\t\t\t\tlineStyle(2,0x333333), moveTo(3, 3), \r\n\t\t\t    lineTo(7, 7), moveTo(7, 3), lineTo(3, 7);\r\n\t\t\t}\r\n\t\t\tchecked.draw(bg);\r\n\t\t\tchecked.draw(ex);\r\n\t\t}\r\n\t\t\r\n\t\tpublic function QuickCheckBox(value:Boolean = false):void{\r\n\t\t\t_checked = value;\r\n\t\t\t_canvas =  new BitmapData(10,10, true, 0x00000000);\r\n\t\t\t_bitmap = new Bitmap(_canvas);\r\n\t\t\taddChild(_bitmap);\r\n\t\t\tbuttonMode = true;\r\n\t\t\trender();\r\n\t\t\taddEventListener(MouseEvent.CLICK, onClick);\r\n\t\t}\r\n\t\t\r\n\t\tpublic function render():void{\r\n\t\t\t if (_checked){\r\n\t\t\t\t _canvas.copyPixels(QuickCheckBox.checked, _canvas.rect, pnt);\r\n\t\t\t }else{\r\n\t\t\t\t _canvas.copyPixels(unchecked, _canvas.rect, pnt);\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\tprivate function onClick(evt:Event):void{\r\n\t\t\t_checked = !_checked;\r\n\t\t\trender();\r\n\t\t\tthis.dispatchEvent(new Event(Event.CHANGE, true));\r\n\t\t}\r\n\t\t\r\n\t\tpublic function get checked():Boolean{\r\n\t\t\treturn _checked;\r\n\t\t}\r\n\t\t\r\n\t\tpublic function set checked(val:Boolean):void{\r\n\t\t\t_checked = val;\r\n\t\t\trender();\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>This checkbox class uses a static initializer. Static initializers can come in handy to initialize some static variables for all class instances to make use of. In this case I&#8217;m using the static initializer to create checked and unchecked BitmapData objects &#8211; all instances of QuickCheckBox use these two static BitmapData objects rather than create their own unique ones. <\/p>\n<p>Here is some client code if you want to test this class out:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nfor (var i:int = 0; i&lt;100; i++){\r\n var checkBox:QuickCheckBox = new QuickCheckBox(true);\r\n checkBox.x =100 +  i % 10 * 12;\r\n checkBox.y = 100 + int(i \/ 10) * 12;\r\n \/\/ uncheck a few checkboxes\r\n if (checkBox.x == checkBox.y){\r\n     checkBox.checked = false;\r\n }\r\n addChild( checkBox);\r\n \/\/ checkBox dispatches a change event\r\n checkBox.addEventListener(Event.CHANGE, onChange);\r\n}\r\nfunction onChange(evt:Event):void{\r\n\ttrace(evt.currentTarget.checked);\r\n}\r\n\/*outputs \r\nonly render your graphics once\r\n*\/\r\n<\/pre>\n<p>This draws 100 checkboxes, &#8211; you&#8217;ll also notice in the output window that the trace statement from QuickCheckBox only runs once.<\/p>\n<p>Here are 100 instances of QuickCheckBox:<br \/>\n<img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/checkboxes.gif\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>package{ import flash.display.*; import flash.events.*; import flash.geom.*; public class QuickCheckBox extends Sprite{ private var _checked:Boolean; private var _bitmap:Bitmap; private var _canvas:BitmapData; private static var checked:BitmapData; private static var unchecked:BitmapData; private static var bg:Shape; private static var ex:Shape; private static var pnt:Point; \/\/ static init for BitmapData and drawing { trace(&quot;only render your graphics once&quot;); pnt [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,2,98],"tags":[3,4],"class_list":["post-279","post","type-post","status-publish","format-standard","hentry","category-bitmapdata","category-oop","category-ui","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/279","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=279"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/279\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}