/////////////////////////////////////////////////////////////////////////////////
// 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
{
import fl.controls.Button;
import fl.controls.TextInput;
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
import wk.example.butterfly.WKEvent_Butterfly;
import wk.example.butterfly.WK_ButterflyFlying;
/**
* The main class of the butterfly example.
*
* @author Wolfgang Kowarschick
*
* @langversion 3.0
* @playerversion Flash 10
*/
public class Butterfly5 extends MovieClip
{
/////////////////////////////////////////////////////////////////////////////
// Components of the Main Movie
/////////////////////////////////////////////////////////////////////////////
public var c_input_counter: TextInput;
public var c_button_start: Button;
public var mc_butterfly: WK_ButterflyFlying;
/////////////////////////////////////////////////////////////////////////////
// State Variables
/////////////////////////////////////////////////////////////////////////////
private var v_rounds_to_fly: int;
///////////////////////////////////////////////////////////////////////////
// Private Methods
/////////////////////////////////////////////////////////////////////////////
/**
* This keyboard event handler calls handle_start(),
* if the key code passed via p_event
* is equal to the ENTER character.
*
*
As a side effect, it deactivates key event listening.
* * @param p_event The keyboard event object passed by the event dispatcher. */ private function handle_key_down(p_event: KeyboardEvent): void { if (p_event.keyCode == Keyboard.ENTER) { // Remove the event listener to deactive key event listening. c_input_counter. removeEventListener(KeyboardEvent.KEY_DOWN, handle_key_down); // Start the Animation handle_start(); }; } /** * This mouse event handler starts the animation of the movie clip. * The action depends on the content of the text input field *c_input counter. That field must contain an integer value
* which is greater or equal to zero.
*
* The butterfly flies c_input counter.text rounds
* before it flies away.
null
* may be passed as well.
*/
private function handle_start(p_event: MouseEvent = null): void
{
// Fetch the user input from the text field "c_input_counter"
// and transform it into an integer value.
var l_rounds_to_fly: int = parseInt(c_input_counter.text);
//trace("user input is a number: " + !isNaN(l_rounds_to_fly));
// If l_rounds_to_fly contains an integer value greater 0,
// store the number of rounds to fly within the "global"
// variable g_rounds_to_fly.
if (!isNaN(l_rounds_to_fly) && l_rounds_to_fly > 0)
v_rounds_to_fly = l_rounds_to_fly
else
v_rounds_to_fly = 0;
// Display the number of rounds to fly.
// A wrong user input is replaced by zero!
c_input_counter.text = String(v_rounds_to_fly);
// Ensure that the value is displayed even if the user input
// has exceded the width of the text input field.
c_input_counter.horizontalScrollPosition = 0;
// Don't display the focus any further.
c_input_counter.drawFocus(false);
// Disable further user inputs.
c_input_counter.enabled = false;
// Hide the start button.
c_button_start.visible = false;
// Start the animation.
handle_flying_around();
}
/**
* This method handles butterfly events.
* * It reduces the number of rounds to fly by one, modifys the * value of the counter text field and checks whether * to fly a further round or not. *
*/ private function handle_flying_around(p_event: WKEvent_Butterfly = null): void { c_input_counter.text = String(v_rounds_to_fly); if (v_rounds_to_fly == 0) mc_butterfly.butterflyState = WK_ButterflyFlying.FLUTTERING_AWAY; else mc_butterfly.butterflyState = WK_ButterflyFlying.FLUTTERING_AROUND; v_rounds_to_fly--; } ///////////////////////////////////////////////////////////////////////////// // Constructor ///////////////////////////////////////////////////////////////////////////// /** * The constructor of the document class initializes the butterfly application. */ public function Butterfly5() { // Stop the movie. It is started by pressing the start button. stop(); v_rounds_to_fly = 0; c_input_counter.text = ""; c_input_counter.setFocus(); // Event listeners: On a mouse click on the // button "c_button_start" the method "handle_start" is invoked. // If a key is pressed (when the focus is on c_input_counter) // the method "handle_key_down" is called. c_button_start.addEventListener(MouseEvent.CLICK, handle_start); c_input_counter. addEventListener(KeyboardEvent.KEY_DOWN, handle_key_down); // If the butterfly has finished flying around, we have to // decide how to go on. mc_butterfly.addEventListener (WKEvent_Butterfly.FINISHED_FLUTTERING_AROUND, handle_flying_around); } ///////////////////////////////////////////////////////////////////////////// // EOF ///////////////////////////////////////////////////////////////////////////// } }