I have started playing around with Degrafa lately and decided to make a set of tutorials about it. This post is going to give an overview of the series. The entire series will show step by step how to create the Tic Tac Toe game featured below. This will be the first series of tutorials built around creating a full application, so if there are pieces that are incoherent let us know. I hope everyone finds some useful information out of them.
The application was created using a couple of libraries, Degrafa which was used for the graphics and Tweener which was used for the small bit of tweening. Another interesting aspect of the the application that will be explained in detail in some of the tutorials of the series is the architecture used for the game. I following the popular MVC (Model-View-Controller) architectural pattern, specifically hashed out by Jon Baker called Easy MVC. Now take a moment find a buddy and play some Tic Tac Toe. Tic Tac Toe Source Code.
Now that you have played with the game a little bit I will give a quick overview of the application. Understand though that this application is in no way perfect and there are always different ways to handle problems (technical or architectural) in Flex. Ok now that I have the small disclaimer out of the way lets look at a couple items.
First I will give a quick overview of the architecture. As mentioned above the architecture is a based on model-view-controller, which is a design pattern to separate the different functional pieces of the application. The three big pieces in our application are going to be the views which show stuff on the screen. In our cases this will be the following:
- Game surface, GameView.mxml - the entire game area
- Game board, GameBoard.mxml - where the game is played which will have 9 game pieces and the grid
- Game grid, GameGrid.mxml - the actually grid
- Game piece, GamePiece.mxml - each piece is a square that is clicked on to show an O or X
- Top toolbar, TopToolbar.mxml - will have the title and the current player info
- Title label, TitleBar.mxml - have the title of the game
- Current player info, PlayerHud.mxml - tells who's turn it is
- Game over popup, GameOverPopup.mxml - simple popup that shows who won the game
Now that may look like a bunch of components but many of them are very simple. These components should modify data by themselves they just react to the data that is kept in the model. In our case a single class GameModel found in GameModel.as. The main piece is the GameController in GameController.as, this will handle all the data processing and set the data in the model to update the views. We also have a couple other items, events, skins, and css. The events take care of telling the controller when something happens that is important such as a button click. The others are to make our game pretty.
All of the graphics for the game are created using Degrafa, which allows us to take advantage of all the graphics capabilities of Flash and Flex but in a much easier way. With Degrafa we can do all our drawing using mxml which makes our lives much much easier for creating complex shapes, fills, and strokes. I recommend before checking out the rest of the tutorials you should take a look at the Degrafa Samples. All the samples have source code and they will give you an idea of the kinds of things that can be done with Degrafa.
To make sure that our application has a nice look and feel I also used Tweener a little bit in the application. The Tweener library always you to tween (animate) objects very easily in actionscript. This is as simple as Tweener.addTween(object, params...). As you noticed above there aren't any fantastic animations but we are going to have Tweener take care of our fading in and out for our shapes.
Lastly I will give you a quick overview of the tutorials that are going to be in the series, they are subject to change of course. I will make sure to keep this list updated with correct links and all as I post them.
Series Outline
- Basic Interface and Layout - game grid, Degrafa basics
- Advanced Interface and Layout - game pieces, top toolbar, game over popup
- Architecture Components - setting up the model, controller, and events
- Game Code - adding the game smarts to the controller
- Styling and Skinning - making our game shiny
I hope this gives you a good overview of what to expect for the rest of the tutorial series. If anyone has any questions, concerns, worries, or just plain ole complaints drop us a comment. Also check out the references below to learn more about some of the aspects of the technology used to create the Tic Tac Toe game above.
Edit (30 April 2008)
After some chatter in the comments and discussion I have updated the code to take out the BoardLocked variable. Just wanted to let you all know.
04/04/2008 - 22:56
Awesome to see tutorials like this posted. Always good to read about how other people approach design and development and how Flex applications are being structured.
TicTacToe was one of the first applications I ever wrote when I was learning Java, so it was interesting to flip through the code and see how it was done in Flex.
I just perused it quickly and noticed you guys had code to handle a problem I remember from back when I wrote the game in Java years ago: Handling what happens if users click quickly on the game board.
However, I had a question about the need for the boardLock variable when doing this in Flex. It looks like it is being set at the beginning of an event handler and then set back at the end of the same function. I believe in the Flash Player you are guaranteed linear execution of your code. Listener code is executed immediately after the event is dispatched. If this was a network based game I could see the need to lock the board down until you receive the next move from the other player, but since it is all local with no asynchronous network events it does not seem like the boardLock is necessary. Even then it would only be necessary if it was set and unset in requests and responses from the server.
04/04/2008 - 23:09
The boardlock disables the all the pieces on the board to further insure that they can not be quickly clicked. This may be a unnecessary redundancy but I always like to have all my bases covered. Nice find.
04/05/2008 - 10:28
I hear ya. And I could see that var being used if the game was extended further. There is just a lot of confusion about Flex apps being single-threaded and was worried someone would see that and think a MouseEvent could interrupt execution of the function and enter the function again on a different thread.
And again, you guys are doing a great service to the community with posts like this!
04/17/2008 - 09:56
"so if there pieces that are incoherrent let us know"
the irony humored me,
"if there [are] pieces"
"incoherent"
(feel free to delete this comment upon moderation)
04/17/2008 - 11:13
You know, usually a few people read each article that gets published. Somehow everyone missed those typos. Thanks for the heads up Internet Editor.
06/06/2008 - 05:34
nice work dude!
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.