As promised here is the second lesson in ActionScript 3.0! Today we begin the analysis of a simple project that reproduces the play of Tris or Tic Tac Toe. I tried to enter in this example some special features of ActionScript 3.0, trying to give space to the understanding and not the style. It follows that if I had to write the "game" really, I probably would have structured very differently, but in this case I tried to mediate between a classical programming and hermetic compact with a more understandable for newbies. I made a single document class, procedure is not necessary, but useful to keep in line with the previous lesson .
Tic Tac Toe
The sources
In addition to the ActionScript file (. Pbuh) document class, we will begin to analyze in detail here, the project is equipped with a classic Flash movie (. Fla) that provides the interface to the game. All the files needed to follow the comments to the code are available here .
Scheme of the game
In this version of the game I have not added to the computer as an opponent, activities you can do if you feel you readers. The game, therefore, alternates two players, just as if we were playing on a blackboard or on a sheet of paper. The block diagram that will follow is as follows:

Bird's-eye View
e il suo interno: Before starting the actual analysis of the code is well see the outlines of a generic class, made up of the package , class and its interior:

Manufacturer Class Document
As we advance the last time , when we link to a Flash document - a movie - a document class, the latter being a real object to be instantiated, the system will initiate the manufacturer.
1 2 3 4 5 6 7 8 9 | / ** * Constructor of the class * / TicTacToe ( ) : void { public function TicTacToe (): void { / / Set scale mode internship and internship align scaleMode = StageScaleMode . NO_SCALE internship. scaleMode = StageScaleMode . NO_SCALE align = StageAlign . TOP_LEFT ; internship. align = StageAlign . TOP_LEFT; Event . ADDED_TO_STAGE , init ) ; addEventListener ( Event . ADDED_TO_STAGE, init); } |
quando la nostra classe documento è aggiunta allo stage , cioè quando sarà visualizzata! The manufacturer in this case sets the stage , which is the container within a browser and adds the call to the function / method init() when our document class is added to the stage , that is when you will see! This is a standard procedure that I adopt often. In some cases it is completely useless, but if you use components, that is, those visual objects in the interface of Flash (like TextArea, TreeView, etc ...), these will not be available (such as pointers) until the event ADDED_TO_STAGE has not been released! So, to get to safety, it is always good practice to follow this path, in anticipation of any new release.
For those who own a dry object oriented programming, I recommend at least reading Classes, Objects and Instances !
: The function, or method, init() has been defined as private (see Actionscript 3.0: public, protected, private and internal ) using the keyword private :
1 2 3 4 5 6 7 8 9 10 |
For now, as you can see, this function does nothing more than another chiamarne, initCell() . Often in the method init() are inserted all the initialization functions, rather than enter the code directly open. This can prove handy just in case re-initialize some parameters. Since this is an example of a relatively simple game, it may be strange to have this double jump almost useless, but in other cases the method init() can be much larger, calling a series of init functions such as:
1 2 3 4 5 6 7 8 9 |
. The method init() is invoked (that is called) when the system releases the event ADDED_TO_STAGE . Precisely for this reason it has been added the parameter e:Event .
Tricks note: it can often happen to have methods (such as
init()) that are called from both events, both within the code. It is precisely for this reason that the parameter of this method has been set toe:Event = null. (a meno di “simularlo” con un notevole spreco di energie). In this way, in fact, should you ever need, we can callinit()without passing any parameters, as we are not an event, and then we could not pass the parametere:Event(less than "fake it" with considerable waste of energy ). For details on handling parameters in Actionscript 3.0, see also: Topics variables and default in Javascript, Actionscript and PHP and Topics variables in Actionscript 3.0 ,
Initialization of the game
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | / ** * Initialize a linear array that will represent our grid. * This "grid" is defaulted to zero (0), a value that indicates that no * The symbol (o / x) has been done. * The player 1 (o) will be associated with a value of one (1). While the player 2 (x) will be * Associated with the value ten (10). By this stratagem when for example the * Sum of the values in row 1 is equal to 3, we will know who won the player 1. * If the sum instead of column 1 is 30, then won the player 2 ... and so on. * * 0 | 0 | 0 * - + --- + --- * 0 | 0 | 0 * - + --- + --- * 0 | 0 | 0 * * @ Param void * @ Return void * @ Private * / initCell ( ) : void { initCell private function (): void { / / Preimposto the array of play to zero , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ; __cell = [0, 0, 0, 0, 0, 0, 0, 0, 0]; / / Always starts with the player (player) number 1 __player = 1; / / Draw the grid on the screen createGrid (); / / Monster video player's turn (in this case 1) = 'PLAYER ' + __player ; turno_txt. text = 'PLAYER' + __ player; / / This variable is used to put the game on pause if true / / Start-game __stop = false; } |
This method initCell() takes care of initializing all variables and create all the objects of the game.
Tricks notes: almost all video games programmers are perfectly familiar with the technique of mapping the playing area in an array of one or more dimensions. This technique, used in the so called Tile Game , allows to perform a whole series of controls in a logical released from graphics to video. In this specific case I used a simple one-dimensional array composed of 9 elements representing the 9 squares of the game. The fact that the elements in the array are sequential, compared to the 3 x 3 grid, it is not a problem as we will see, but only a matter of point of view!
For those who want to explore the interesting topic, I recommend reading Tile Based Games
All variables for internal use global class level, indicate use with the double underscore in front. indicano rispettivamente il giocatore attivo e lo stato del gioco. These, in fact, during the first draft of a class may subsequently become public property, taking advantage of its encapsulation of OO programming (see Writing good OO code in Adobe Flash ). __player and __stop denote the active player and the game state.
For now I'll stop here! Next time we will analyze the method createGrid() and the rest of the game, if you have doubts or questions about this part commented.










[...] The analysis of the example of the game Tic Tac Toe, presented in Actionscript 3.0 for beginners: lesson # 2. We came to the function that creates the grid of play: PLAIN TEXT [...]
Even though I do not like tic tac toe, Because The first player always wins, if he Makes His marks on at the right places, your tut is great! There are some points unclear to me yet, but I hope you will translate the site fastly. Kind regards
[...] Our example code TicTacToe (found in full on Google Code) and begin to analyze it in [...]
ontinuo do not understand how to insert the code .... gives me errors when I copy and paste in the file normally AS ....... bha!