I’ve been using Backbone.js and jQuery quite extensively for the past year or so and I’ve only recently gotten back into AS3 dev. And I’ve never actually ever used any MVC framework in AS3/Flex, so I wanted to take a quick break away from my AS3 game engine series to have fun and mess around with MVC in AS3. As I didn’t want to stray too far from my game engine, I decided I wanted to whip up some base Model/View/Controller classes, and then extend those to an EnemyModel/EnemyView/EnemyController class.
The reason this is in “AS3 Experiments” is because this isn’t just straightforward MVC. I’ve added an expandable Model. Imagine you’re loading in JSON from a file or endpoint and you pass the data straight in to the Model’s constructor or some “setPropertiesFromJSON()” type function. I wanted to play around with the idea of, “what happens if that JSON file has properties inside that you don’t have hardcoded class variables for?” In JavaScript, this wouldn’t even be a conversation. But in AS3, while you do have Dynamic classes, I didn’t want to go that route. So, as we’ll see, if the model doesn’t find the specific class property you’re trying to set, it saves the data anyways in an _attributes Object so you don’t lose it. More on that later.
So, I’ve been reading through Rex van der Spuy’s book, AdvancED Game Design with Flash and right from the start, the book just jumps right into MVC… fantastic! I’ve found it’s tough to find “advanced” books and especially “advanced” tutorials online, so this book was very refreshing. There are no sections titled “Data Types in AS3”, or “What is a Function?” I’m really enjoying this book.
In the online tutorial front, I came across a couple good sites worth mentioning on MVC in AS3 whilst googling, so I’ll list those here as well.
- Advance AS3 Game Dev 02: Rectangles and MVC – this is video #2 in a series on advanced game dev. Don’t waste your time looking for Video 1… I spent probably 10 minutes googling before reading the youtube comments (because who reads those?) but most of them were people complaining about video 1 not being public. oh well… I’ll take what I can get. Pretty good series.
- The Model-View-Controller Design Pattern in Actionscript 3 on the Swinburne University of Tech site had a great tutorial that was quite helpful as well.
Ok, enough love, let’s code.
(more…)