void star games

* play * code * learn *

Flash Tutorials

puce Flash Yahtzee Game Tutorial 2 Back Start Over Next

The Game Class. Start a new ActionScript class file and call it Game. Import the module ScoreCell in this class.

Declare a class attribute called scoreArray and one called parent. In the constructor, add the same parameter for the stage as in the class ScoreCell and initialize the parent attribute the same way. Next, we'll need to create the table of score cells. These will need a reference to the Game object to send it information about being selected for scoring. Thus, go back to the ScoreCell class and add a parameter in the constructor after the stage called gameVal. Add a class attribute called gameRef and then initialize it with the value of the parameter in the constructor.

Initialize the array of score cells with the following code. First, we create the array itself. Second, we use a loop to initialize each score cell object in the array. Add combNr as a class attribute.

combNr = 17;
scoreArray = new Array(combNr);
for (var i=1; i<combNr; i++) {
   scoreArray[i] = new ScoreCell(parent, this, 20, 35*i-15, i);

In this piece of code, the x coordinate of the score cells is a constant because they are supposed to be aligned vertically. The y coordinate is computed based on the value of the variable i, and on the height of the box objects written on a piece of paper at the beginning, plus a couple of pixels for padding. You will have to use values based on the size of your own box, and may have to adjust the size of the stage to make sure all the cells fit in.

To test this code, go back to the yahtzee.fla file and replace the code testing a single cell with the following:

var gameRef = new Game(this.stage);

As now we have a better idea about how the largest object will fit on the stage, it's time to add some buttons for the game functionality. Add two buttons to the stage and edit their labels in the Properties section to show New Game and Roll respectively. You can find the button component in the Components menu that can be found between the Properties area and the stage. Give them instance names newGameBtn and rollBtn. Their size and placement is up to you.

Let's implement the functionality of a new game. First, go to the ScoreCell class and add a public function called reset. In this function, set the text of the textScore as an empty string, the value of the attribute score to 0, and the value of the attribute used to false. While you're doing this, also set the text of the textScore attribute in the constructor as an empty string. The value we've used before was only for testing.

Next, go back to the class Game and add the following function:

public function newGame() {
   for (var i=1; i<combNr; i++) {

Then go to the yahtzee.fla file and add the following code:

newGameBtn.addEventListener(MouseEvent.CLICK, restart);
function restart(event) {

To test the functionality of this button, you can temporarily use some random text in the function reset in the class ScoreCell instead of an empty string. Once you see it appear in the cells when running the app, you can erase it and revert to an empty string.

While working on the ScoreCell class, we can add another function that will be useful later, that sets the text of the score for the object. It is necessary to set the format of the text field again after we give the text a new value, because otherwise it will revert to the default format.

public function setScore(val) {
   score = val;
   textScore.text = score;

Next we'll define the Dice class.

Back Start Over Next