///////////////////////////////////////////////////////////////////////////////// // Copyright © 2008-2010 W. Kowarschick // // Permission is granted to copy, distribute and/or modify this document // under the terms of the WK License, Version 1.0 (WKL 1.0) or any later version // published by Wolfgang Kowarschick. ///////////////////////////////////////////////////////////////////////////////// package die { import flash.display.MovieClip; import flash.events.Event; /** * This MovieClip displays a dice (German: Würfel). *

A dice has a value (1, 2, 3, 4, 5, or 6; Error: 0), which * immediatedly is displayed whenever it changes. *

* * @author Wolfgang Kowarschick * @category Class * @langversion 3.0 */ public class Die extends MovieClip { ///////////////////////////////////////////////////////////////////////////// // Constants ///////////////////////////////////////////////////////////////////////////// public static const NUMBER_OF_ROLLS: uint = 20; ///////////////////////////////////////////////////////////////////////////// // Private Constants ///////////////////////////////////////////////////////////////////////////// // An array of arrays, which contains for each of the numbers 0, 1, ..., 6 // the states of the pips. private static const c_pip_states: Array = [ ["off", "off", "off", "off", "off", "off", "off"], // 0 ["off", "off", "off", "on", "off", "off", "off"], // 1 ["off", "off", "on", "off", "on", "off", "off"], // 2 ["off", "off", "on", "on", "on", "off", "off"], // 3 ["on", "off", "on", "off", "on", "off", "on" ], // 4 ["on", "off", "on", "on", "on", "off", "on" ], // 5 ["on", "on", "on", "off", "on", "on", "on" ] // 6 ]; ///////////////////////////////////////////////////////////////////////////// // Instance Variables ///////////////////////////////////////////////////////////////////////////// // Instance variable of the attribute "value". private var v_value: uint; // An auxiliary variable of the method "rollTheDice". private var v_number_of_rolls: int; ///////////////////////////////////////////////////////////////////////////// // Public Attributes ///////////////////////////////////////////////////////////////////////////// /** The current value of the dice: 1, 2, 3, 4, 5, or 6; Error: 0. */ public function get value(): uint { return v_value; } [Inspectable (type="uint", defaultValue="1", enumeration="0,1,2,3,4,5,6")] public function set value(p_value: uint): void { // Do some integrity checks and finaly set the value: if (0 < p_value && p_value < 7) v_value = p_value; else v_value = 0; // A better error handling is missing! // Set the states of all pips by means of a for-loop. for (var i: int = 0; i < 7; i++) this["mc_pip_" + i].value = c_pip_states[v_value][i]; } ///////////////////////////////////////////////////////////////////////////// // Constructor ///////////////////////////////////////////////////////////////////////////// /** * Creates a new dice. * * @param p_value The initial value. */ public function Die(p_value: uint = 1) { super(); value = p_value; // Sets AND displays the value!! } ///////////////////////////////////////////////////////////////////////////// // Public Methods ///////////////////////////////////////////////////////////////////////////// // auxiliary method of "rollTheDice" private function enterFrameHandler(event: Event): void { v_number_of_rolls--; if (v_number_of_rolls == 0) this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler); else value = Math.floor(Math.random() * 6) + 1; } /** * Computes a random number NUMBER_OF_ROOLS * times and displays each new value for a fraction of a second. */ public function rollTheDie(): void { v_number_of_rolls = NUMBER_OF_ROLLS; this.addEventListener(Event.ENTER_FRAME, enterFrameHandler); } ///////////////////////////////////////////////////////////////////////////// // Tracing ///////////////////////////////////////////////////////////////////////////// /** * Converts a WK_Dice object into a string. * * @return Returns the classe name, the object name and the value * (1, 2, 3, 4, * 5, or 6; Error: 0) of the * current dice. */ override public function toString(): String { return "WK_Dice '" + name + "': " + value; } ///////////////////////////////////////////////////////////////////////////// // EOC ///////////////////////////////////////////////////////////////////////////// } } ///////////////////////////////////////////////////////////////////////////////// // EOF /////////////////////////////////////////////////////////////////////////////////