///////////////////////////////////////////////////////////////////////////////// // Copyright © 2008-2010 W. Kowarschick // // Dieses Werk darf unter den Bedingungen der Creative Common Lizenz by-nc 3.0 de // (http://creativecommons.org/licenses/by-nc/3.0/de/) // vervielfaeltigt, verbreitet, publiziert und modifiziert werden. ///////////////////////////////////////////////////////////////////////////////// package die.view { import flash.display.MovieClip; import flash.events.Event; /** * This MovieClip displays a die (German: Würfel). *

A die 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: int = 20; private static const c_pip_prefix: String = "sp_pip_"; ///////////////////////////////////////////////////////////////////////////// // 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 = [ [Pip.OFF, Pip.OFF, Pip.OFF, Pip.OFF, Pip.OFF, Pip.OFF, Pip.OFF], // 0 [Pip.OFF, Pip.OFF, Pip.OFF, Pip.ON, Pip.OFF, Pip.OFF, Pip.OFF], // 1 [Pip.OFF, Pip.OFF, Pip.ON, Pip.OFF, Pip.ON, Pip.OFF, Pip.OFF], // 2 [Pip.OFF, Pip.OFF, Pip.ON, Pip.ON, Pip.ON, Pip.OFF, Pip.OFF], // 3 [Pip.ON, Pip.OFF, Pip.ON, Pip.OFF, Pip.ON, Pip.OFF, Pip.ON ], // 4 [Pip.ON, Pip.OFF, Pip.ON, Pip.ON, Pip.ON, Pip.OFF, Pip.ON ], // 5 [Pip.ON, Pip.ON, Pip.ON, Pip.OFF, Pip.ON, Pip.ON, Pip.ON ] // 6 ]; ///////////////////////////////////////////////////////////////////////////// // Instance Variables ///////////////////////////////////////////////////////////////////////////// // Instance variable of the attribute "value". private var v_value: int; // An auxiliary variable of the method "rollTheDie". private var v_number_of_rolls: int; ///////////////////////////////////////////////////////////////////////////// // Public Attributes ///////////////////////////////////////////////////////////////////////////// /** The current value of the die: 1, 2, 3, 4, 5, or 6; Error: 0. */ public function get value(): int { return v_value; } public function set value(p_value: int): void { // Do some integrity checks and finally 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[c_pip_prefix + i].value = c_pip_states[v_value][i]; } ///////////////////////////////////////////////////////////////////////////// // Constructor ///////////////////////////////////////////////////////////////////////////// /** * Creates a new die. * * @param p_value The initial value. */ public function Die(p_value: int = 1) { super(); this.value = p_value; // Sets AND displays the value!! } ///////////////////////////////////////////////////////////////////////////// // Public Methods ///////////////////////////////////////////////////////////////////////////// // auxiliary method of "rollTheDie" private function o_enter_frame(p_event: Event): void { if (v_number_of_rolls == 0) this.removeEventListener(Event.ENTER_FRAME, o_enter_frame); else this.value = Math.floor(Math.random() * 6) + 1; v_number_of_rolls--; } /** * 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, o_enter_frame); } ///////////////////////////////////////////////////////////////////////////// // Tracing ///////////////////////////////////////////////////////////////////////////// /** * Converts a Die 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 die. */ override public function toString(): String { return "Die '" + name + "': " + value; } ///////////////////////////////////////////////////////////////////////////// // End of class ///////////////////////////////////////////////////////////////////////////// } } ///////////////////////////////////////////////////////////////////////////////// // End of file /////////////////////////////////////////////////////////////////////////////////