
{"id":794,"date":"2009-02-11T09:00:10","date_gmt":"2009-02-11T08:00:10","guid":{"rendered":"http:\/\/www.matthijskamstra.nl\/blog\/?p=794"},"modified":"2009-01-22T17:31:29","modified_gmt":"2009-01-22T16:31:29","slug":"document-class-in-as3","status":"publish","type":"post","link":"https:\/\/www.matthijskamstra.nl\/blog\/2009\/02\/11\/document-class-in-as3\/","title":{"rendered":"Document Class in AS3"},"content":{"rendered":"<p>A while back a read this post about the <a href=\"http:\/\/www.fuoridalcerchio.net\/wordpress\/2008\/12\/17\/as3pills-01\/\">AS3 Pills #1 &#8211; Document class<\/a> and it reminded me to write something about the &#8220;Document Class&#8221;. The title of this post is a little misleading: there is no &#8220;Document Class&#8221; in Actionscript 2 but this is probably the way you are searching for this problem.<\/p>\n<p>A good explanation can be found on <a href=\"http:\/\/www.kirupa.com\/forum\/showthread.php?p=1950401\">kirupa.com<\/a> which has a ton of examples, explanations and tutorials!<\/p>\n<p>But this is what it comes down to:<\/p>\n<ol>\n<li>create a folder with the name &#8220;Matthijs tutorials&#8221; (or use your own name, it&#8217;s not important)<\/li>\n<li>create in the folder &#8220;Matthijs tutorials&#8221;, another folder &#8220;source&#8221;<\/li>\n<li>save a flash file in the &#8220;source&#8221; folder: &#8220;documentClassTest.fla&#8221;<\/li>\n<li>now we start making <code>packages<\/code>: create a folder &#8220;nl&#8221;<\/li>\n<li>and save in folder &#8220;nl&#8221; a folder &#8220;matthijskamstra &#8220;<\/li>\n<\/ol>\n<figure id=\"attachment_821\" aria-describedby=\"caption-attachment-821\" style=\"width: 318px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.matthijskamstra.nl\/blog\/wp-content\/uploads\/flashdevelop_folderstructure.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.matthijskamstra.nl\/blog\/wp-content\/uploads\/flashdevelop_folderstructure.jpg\" alt=\"Folder structure (FlashDevelop)\" title=\"flashdevelop_folderstructure\" width=\"318\" height=\"228\" class=\"size-full wp-image-821\" srcset=\"https:\/\/www.matthijskamstra.nl\/blog\/wp-content\/uploads\/flashdevelop_folderstructure.jpg 318w, https:\/\/www.matthijskamstra.nl\/blog\/wp-content\/uploads\/flashdevelop_folderstructure-300x215.jpg 300w\" sizes=\"auto, (max-width: 318px) 100vw, 318px\" \/><\/a><figcaption id=\"caption-attachment-821\" class=\"wp-caption-text\">Folder structure (FlashDevelop)<\/figcaption><\/figure>\n<p>Goto Adobe Flash, and create a AS3 Flash file and save it in the <code>source<\/code> folder.<\/p>\n<p>Fill in the document Class: <code>nl.matthijskamstra.Main<\/code><br \/>\n<em>(Flash will complain somewhat, you can ignore it for now&#8230;)<\/em><\/p>\n<figure id=\"attachment_820\" aria-describedby=\"caption-attachment-820\" style=\"width: 676px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.matthijskamstra.nl\/blog\/wp-content\/uploads\/documentclass.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.matthijskamstra.nl\/blog\/wp-content\/uploads\/documentclass.jpg\" alt=\"Document Class\" title=\"Document Class\" width=\"676\" height=\"111\" class=\"size-full wp-image-820\" srcset=\"https:\/\/www.matthijskamstra.nl\/blog\/wp-content\/uploads\/documentclass.jpg 676w, https:\/\/www.matthijskamstra.nl\/blog\/wp-content\/uploads\/documentclass-300x49.jpg 300w\" sizes=\"auto, (max-width: 676px) 100vw, 676px\" \/><\/a><figcaption id=\"caption-attachment-820\" class=\"wp-caption-text\">Document Class<\/figcaption><\/figure>\n<p>Create a new class in the folder <code>matthijskamstra<\/code>, default (at least the default <a href=\"http:\/\/www.flashdevelop.org\/\">Flashdevelop<\/a> class) it will look something like this.<br \/>\n[as]<br \/>\npackage nl.matthijskamstra {<\/p>\n<p>\t\/**<br \/>\n\t * @author Matthijs Kamstra aka [mck]<br \/>\n\t *\/<br \/>\n\tpublic class Main {<\/p>\n<p>\t\tpublic function Main ()  {<\/p>\n<p>\t\t}<\/p>\n<p>\t} \/\/ end class<\/p>\n<p>} \/\/ end package<br \/>\n[\/as]<\/p>\n<p>If you would compile the .FLA now you will get an error (Compiler Errors):<\/p>\n<blockquote><p>Location: Main.as, Line 1<br \/>\nDescription: 5000: The class &#8216;nl.matthijskamstra.Main&#8217; must subclass &#8216;flash.display.MovieClip&#8217; since it is linked to a library symbol of that type.<\/p><\/blockquote>\n<p><em>(if you compile using flex you will get this error)<br \/>\n<\/em><\/p>\n<blockquote><p>Error #2023: Class Main$ must inherit from Sprite to link to the root.<\/p><\/blockquote>\n<p>The first time I got this error I couldn&#8217;t understand it, luckily I do now and the answer isn&#8217;t that difficult, but if you don&#8217;t know&#8230; \ud83d\ude41<\/p>\n<ul>\n<li><strong>The class &#8216;nl.matthijskamstra.Main&#8217; must subclass &#8216;flash.display.MovieClip&#8217;<\/strong>: the class that we just made needs to extend (be a subclass) of Movieclip (the root has a timeline)<\/li>\n<li><strong>since it is linked to a library symbol of that type<\/strong>: this is strange, this says that the class is linked to a library symbol. This what puzzled me the most: I (we) didn&#8217;t link this to a library item, we linked it to the document class. The Flex error helps more, but you can see the root as a very big movie (timeline) and let the error be the error.<\/li>\n<li><strong>Error #2023: Class Main$ must inherit from Sprite to link to the root.<\/strong>: why does flex say you need a to extend Sprite? Flex programmers don&#8217;t use the timeline and a Sprite is a Movieclip with just one frame.<\/li>\n<\/ul>\n<p>So to sum things up: there are some stuff that you need to do with a document class that you don&#8217;t have to do to an other class:<br \/>\nFirst you need to extend it to Sprite or MovieClip. If you create more then one frame in the timeline you need to extend to MovieClip because MovieClip supports frames and Sprite doesn&#8217;t. I always choose to extend to MovieClip, because it covers all the bases&#8230; (a Sprite is a MovieClip with just one frame: a Sprite is not a MovieClip but a MovieClip is a Sprite)<\/p>\n<p><em>Another error that seems to happen sometime (not if you use the default class from FlashDevelop)<\/em><br \/>\nSecond a document class should be public (the constructor is always public).<br \/>\nA Constructor is a function or method that is called whenever the Class (in our case the document class) gets instantiated, it must never have a return type such as &#8220;void&#8221;<\/p>\n<p>Don&#8217;t forget you need to import the MovieClip class: <code>import flash.display.MovieClip;<\/code> (see the error created by Flash)<\/p>\n<p>So the Document Class looks now something like this:<\/p>\n<p>[as]<br \/>\npackage nl.matthijskamstra {<\/p>\n<p>\timport flash.display.MovieClip;<\/p>\n<p>\t\/**<br \/>\n\t * @author Matthijs Kamstra aka [mck]<br \/>\n\t *\/<br \/>\n\tpublic class Main extends MovieClip {<\/p>\n<p>\t\t\/\/ constructor<br \/>\n\t\tpublic function Main ()  {<br \/>\n\t\t\ttrace( &#8220;Main.Main&#8221; );<br \/>\n\t\t\tinit ();<br \/>\n\t\t}<\/p>\n<p>\t\tprivate function init ():void  {<br \/>\n\t\t\ttrace( &#8220;Main.init&#8221; );<br \/>\n\t\t}\t\t<\/p>\n<p>\t} \/\/ end class<\/p>\n<p>} \/\/ end package<br \/>\n[\/as]<\/p>\n<p>Export you Flash file and you will have two traces in you output panel<\/p>\n<p>My default class looks similar, I add two group-imports:<\/p>\n<pre><code>\t\r\nimport flash.display.*;\r\nimport flash.events.*;\r\n<\/code><\/pre>\n<p>with the &#8220;*&#8221; you import everything in that package (MovieClip, Sprite, etc)<br \/>\nbecause AS3 is event based, I import everything in the events-package<\/p>\n<p>[as]<br \/>\npackage nl.matthijskamstra {<\/p>\n<p>\timport flash.display.*;<br \/>\n\timport flash.events.*;<\/p>\n<p>\t\/**<br \/>\n\t * @author Matthijs Kamstra aka [mck]<br \/>\n\t *\/<br \/>\n\tpublic class Main extends MovieClip {<\/p>\n<p>\t\t\/\/ constructor<br \/>\n\t\tpublic function Main ()  {<br \/>\n\t\t\ttrace( &#8220;Main.Main&#8221; );<br \/>\n\t\t\tinit ();<br \/>\n\t\t}<\/p>\n<p>\t\tprivate function init ():void  {<br \/>\n\t\t\ttrace( &#8220;Main.init&#8221; );<br \/>\n\t\t}\t\t<\/p>\n<p>\t} \/\/ end class<\/p>\n<p>} \/\/ end package<br \/>\n[\/as]<\/p>\n<p>That&#8217;s it, I hope this clears the mystery called Document Class<br \/>\nQuestions? You know where to place them!<\/p>\n<p><strong>Linkdump:<\/strong><br \/>\n<em>another post that I never finished, but it fits here&#8230;<\/em><\/p>\n<p><a href='http:\/\/blog.andrewpaulsimmons.com\/2007\/07\/referenceerror-error-1056-caused-by.html'>Andrew Paul Simmons: Blog: ReferenceError: Error #1056: Caused by Declaring Stage Instances Private<\/a>.<\/p>\n<p>Ever got this error: <href=\"http:\/\/www.google.nl\/search?q=ReferenceError%3A+Error+%231056%3A+Cannot+create+property>ReferenceError: Error #1056: Cannot create property<\/a>? No, then someone has explained it to you before you made this mistake.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A while back a read this post about the AS3 Pills #1 &#8211; Document class and it reminded me to write something about the &#8220;Document Class&#8221;. The title of this post is a little misleading: there is no &#8220;Document Class&#8221; in Actionscript 2 but this is probably the way you are searching for this problem. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,172,3],"tags":[68,249,408,251,248,398,159,250,237,245,410],"class_list":["post-794","post","type-post","status-publish","format-standard","hentry","category-as3","category-as3-migration","category-flash","tag-actionscript","tag-actionscript-2","tag-as3","tag-compiler-errors","tag-document-class","tag-flash","tag-flashdevelop","tag-library-symbol","tag-programmers","tag-tutorial","tag-tutorials"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts\/794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/comments?post=794"}],"version-history":[{"count":10,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts\/794\/revisions"}],"predecessor-version":[{"id":830,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts\/794\/revisions\/830"}],"wp:attachment":[{"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/media?parent=794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/categories?post=794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/tags?post=794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}