<p>I have a large codebase that targetted Flash 7, with a <em>lot</em> of AS2 classes.  I'm hoping that I'll be able to use Flex for any new projects, but a lot of new stuff in our roadmap is additions to the old code.</p>

<p>The syntax for AS2 and AS3 is generally the same, so I'm starting to wonder how hard it would be to port the current codebase to Flex/AS3.  I know all the UI-related stuff would be iffy (currently the UI is generated at runtime with a lot of createEmptyMovieClip() and attachMovie() stuff), but the UI and controller/model stuff is mostly separated.</p>

<p>Has anyone tried porting a large codebase of AS2 code to AS3?  How difficult is it?  What kinds of pitfalls did you run into?  Any recommendations for approaches to doing this kind of project?</p>

[{'answer_id': 46184, 'author': 'lilserf', 'author_id': 4545, 'author_profile': 'https://Stackoverflow.com/users/4545', 'pm_score': 4, 'selected': True, 'text': "<p>Some notable problems I saw when attempting to convert a large number of AS2 classes to AS3:</p>\n\n<h3>Package naming</h3>\n\n<pre><code>class your.package.YourClass\n{\n}\n</code></pre>\n\n<p>becomes</p>\n\n<pre><code>package your.package\n{\n    class YourClass\n    {\n    }\n}\n</code></pre>\n\n<h3>Imports are required</h3>\n\n<p>You must explicitly import any outside classes used -- referring to them by their fully qualified name is no longer enough.</p>\n\n<h3>Interface methods can't be labelled 'public'</h3>\n\n<p>This makes total sense, but AS2 will let you do it so if you have any they'll need to be removed.</p>\n\n<h3>Explicit 'override' keyword</h3>\n\n<p>Any functions that override a parent class function must be declared with the <em>override</em> keyword, much like C#.  Along the same lines, if you have interfaces that extend other interfaces and redeclare functions, those overrides must be removed (again, as with <em>public,</em> this notation didn't make sense anyway but AS2 let you do it).</p>\n\n<h3>All the Flash builtin stuff changed</h3>\n\n<p>You alluded to this above, but it's now <em>flash.display.MovieClip</em> instead of just <em>MovieClip,</em> for example.  There are a lot of specifics in this category, and I didn't get far enough to find them all, but there's going to be a lot of annoyance here.</p>\n\n<h3>Conclusion</h3>\n\n<p>I didn't get to work on this conversion to the point of success, but I was able in a matter of hours to write a quick C# tool that handled every aspect of this except the <em>override</em> keyword.  Automating the imports can be tricky -- in my case the packages we use all start with a few root-level packages so they're easy to detect.</p>\n"}, {'answer_id': 50840, 'author': 'hasseg', 'author_id': 4111, 'author_profile': 'https://Stackoverflow.com/users/4111', 'pm_score': 1, 'selected': False, 'text': '<p>First off, I hope you\'re not using <code>eval()</code> in your projects, since there is no equivalent in AS3.</p>\n\n<p>One of the things I would do is go through <a href="http://livedocs.adobe.com/flex/3/langref/migration.html" rel="nofollow noreferrer">Adobe\'s migration guide</a> (which is basically just an itemized list of what has changed) item by item and try to figure out if each item can be changed via a simple search and replace operation (possibly using a regex) or whether it\'s easier to just manually edit the occurrences to correspond to AS3. Probably in a lot of cases (especially if, as you said, the amount of code to be migrated is quite high) you\'ll be best off scripting the changes (i.e. using regex search &amp; replace) and manually fixing any border cases where the automated changes have failed.</p>\n\n<p>Be prepared to set some time aside for a bit of debugging and running through some test cases as well.</p>\n\n<p>Also, as others have already mentioned, trying to combine AS2 SWFs with AS3 SWFs is not a good idea and doesn\'t really even work, so you\'ll definitely have to migrate all of the code in one project at once.</p>\n'}, {'answer_id': 58218, 'author': 'Antti', 'author_id': 6037, 'author_profile': 'https://Stackoverflow.com/users/6037', 'pm_score': 0, 'selected': False, 'text': "<p>Migrating a bigger project like this from as2 will be more than a simple search and replace. The new syntax is fairly similar and simple to adapt (as lilserf mentioned) but if nothing else the fact that as3 is more strict and the new event model will mostly likely cause a lot of problems. You'll probably be better off by more or less rewriting almost everything from scratch, possibly using the old code as a guide.</p>\n\n<p>Migrating from as2 -> as3 in terms of knowledge is fairly simple though. If you know object oriented as2, moving on to as3 won't be a problem at all.</p>\n\n<p>You still don't have to use mxml for your UI unless you specifically want to. Mxml just provides a quick way to build the UI (etc) but if you want to do it yourself with actionscript there's nothing stopping you (this would also probably be easier if you already have that UI in as2 code). Flex (Builder) is just a quick way to do stuff you may not want to do yourself, such as building the UI and binding data but essentially it's just creating a part of the .swf for you -- there's no magic to it ;)</p>\n"}, {'answer_id': 67571, 'author': 'mikechambers', 'author_id': 10232, 'author_profile': 'https://Stackoverflow.com/users/10232', 'pm_score': 1, 'selected': False, 'text': '<p>Here are some additional references for moving from AS2 to AS3:</p>\n\n<p>Grant Skinners Introductory AS3 Workshop slidedeck\n<a href="http://gskinner.com/talks/as3workshop/" rel="nofollow noreferrer">http://gskinner.com/talks/as3workshop/</a></p>\n\n<p>Lee Brimelow : 6 Reasons to learn ActionScript 3\n<a href="http://www.adobe.com/devnet/actionscript/articles/six_reasons_as3.html" rel="nofollow noreferrer">http://www.adobe.com/devnet/actionscript/articles/six_reasons_as3.html</a></p>\n\n<p>Colin Moock : Essential ActionScript 3 (considered the "bible" for ActionScript developers):\n<a href="https://rads.stackoverflow.com/amzn/click/com/0596526946" rel="nofollow noreferrer" rel="nofollow noreferrer">http://www.amazon.com/Essential-ActionScript-3-0/dp/0596526946</a></p>\n\n<p>mike chambers</p>\n\n<p>mesh@adobe.com</p>\n'}, {'answer_id': 68871, 'author': 'fenomas', 'author_id': 10651, 'author_profile': 'https://Stackoverflow.com/users/10651', 'pm_score': 1, 'selected': False, 'text': '<p>My experience has been that the best way to migrate to AS3 is in two phases - first structurally, and second syntactically.</p>\n\n<p>First, do rounds of refactoring where you stay in AS2, but get as close to AS3 architecture as you can. Naturally this includes moving all your frame scripts and #include scripts into packages and classes, but you can do more subtle things like changing all your event listeners and dispatchers to follow the AS3 flow (using static class properties for event types, and registering by method rather than by object). You\'ll also want to get rid of all your "built-in" events (such as onEnterFrame), and you\'ll want to take a close look at nontrivial mouse interaction (such as dragging) and keyboard interaction (such as detecting whether a key is pressed). This phase can be done incrementally.</p>\n\n<p>The second phase is to convert from AS2 to AS3 - changing "_x" to "x", and all the APIs, and so on. This can\'t be done incrementally, you have to just do as much as you can in one fell swoop and then start fixing all the compile errors. For this reason, the more you can do in the first phase, the more pain you avoid in the second phase.</p>\n\n<p>This process has worked for me on a reasonably large project, but I should note that the first phase requires a solid understanding of how AS3 is structured. If you\'re new to AS3, then you\'ll probably need to try building some of the functionality you\'ll need to be porting. For example, if your legacy code uses dragging and drop targets, you\'ll want to try implementing that in AS3 to understand how your code will have to change structurally. If you then refactor your AS2 with that in mind, the final syntax changes should go smoothly.</p>\n\n<p>The biggest pitfalls for me were the parts that involved a lot of attaching, duplicating and moving MovieClips, changing their depths, and so on. All that stuff can\'t really be rearchitected to look like AS3; you have to just mash it all into the newer way of thinking and then start fixing the bugs.</p>\n\n<p>One final note - I really wouldn\'t worry about stuff like import and override statements, at least not to the point of automating it. If you miss any, it will be caught by the compiler. But if you miss structural problems, you\'ll have a lot more pain.</p>\n'}]
['https://Stackoverflow.com/questions/46136', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1409/']