Charm47
04-29-2009, 09:30 PM
So I am a student working on an independent study project on AS3 in Flash CS4, which I am very new to. Text books and Lynda.com are what I have been using so far. If someone can please look over what I have and help point me in the right direction, it would be much appreciated.
The project is a hangman game that I have been messing around with. All of the following code is in an .as file called "HangHitlerScript.as". One of the main issues I think I have deals with the linkage bewtween the .as and .fla files. Currently I have not named a document class either, how much of a problem has that created? Thank you ahead of time for reading over this.
package
{
import flash.display.*;
import flash.text.*;
import flash.events.*;
public class HangHitlerScript extends MovieClip
{
public var textDisplay:TextField;
public var phrase:String = "Kein mehr Hitler! ";
public var displayed:String;
public var wrongGuesses:int;
public function replaceLetters()
{
//copy text with underscore char for each letter
displayed = phrase.replace(/[A-Za-z]/g,"_");
wrongGuesses = 0;
}//end replaceLetters()
//constructor => creates visible text field & formatting
public function HangHitlerScript()
{
textDisplay = new TextField();
textDisplay.defaultTextFormat = new TextFormat("Courier",24,0xFF0000,true,false,false,null,null);
textDisplay.height = 250;
textDisplay.wordWrap = true;
textDisplay.selectable = false;
textDisplay.text = displayed;
textDisplay.autoSize = TextFieldAutoSize.LEFT;
addChild(textDisplay);
//listen for key presses
stage.addEventListener(KeyboardEvent.KEY_UP,pressK ey);
}
public function pressKey(event:KeyboardEvent)
{
//get letter pressed on keyboard
var charPressed:String = (String.fromCharCode(event.charCode));
//loop thru letters
var found:Boolean = false;
for(var i:int = 0; i < phrase.length; i++)
{
if (phrase.charAt(i).toLowerCase() == charPressed){
//match found, change shown phrase
displayed = displayed.substr(0,i) + phrase.substr(i,1) + displayed.substr(i + 1);
found = true;
//update display
textDisplay.text = displayed;
//update illustration
if (!found)
{
wrongGuesses++;
gotoAndStop(wrongGuesses + 1);
}//end IF
}//end IF
}//end FOR
}//end pressKey()
}//end CLASS HangHitlerSript
}//end PACKAGE
The project is a hangman game that I have been messing around with. All of the following code is in an .as file called "HangHitlerScript.as". One of the main issues I think I have deals with the linkage bewtween the .as and .fla files. Currently I have not named a document class either, how much of a problem has that created? Thank you ahead of time for reading over this.
package
{
import flash.display.*;
import flash.text.*;
import flash.events.*;
public class HangHitlerScript extends MovieClip
{
public var textDisplay:TextField;
public var phrase:String = "Kein mehr Hitler! ";
public var displayed:String;
public var wrongGuesses:int;
public function replaceLetters()
{
//copy text with underscore char for each letter
displayed = phrase.replace(/[A-Za-z]/g,"_");
wrongGuesses = 0;
}//end replaceLetters()
//constructor => creates visible text field & formatting
public function HangHitlerScript()
{
textDisplay = new TextField();
textDisplay.defaultTextFormat = new TextFormat("Courier",24,0xFF0000,true,false,false,null,null);
textDisplay.height = 250;
textDisplay.wordWrap = true;
textDisplay.selectable = false;
textDisplay.text = displayed;
textDisplay.autoSize = TextFieldAutoSize.LEFT;
addChild(textDisplay);
//listen for key presses
stage.addEventListener(KeyboardEvent.KEY_UP,pressK ey);
}
public function pressKey(event:KeyboardEvent)
{
//get letter pressed on keyboard
var charPressed:String = (String.fromCharCode(event.charCode));
//loop thru letters
var found:Boolean = false;
for(var i:int = 0; i < phrase.length; i++)
{
if (phrase.charAt(i).toLowerCase() == charPressed){
//match found, change shown phrase
displayed = displayed.substr(0,i) + phrase.substr(i,1) + displayed.substr(i + 1);
found = true;
//update display
textDisplay.text = displayed;
//update illustration
if (!found)
{
wrongGuesses++;
gotoAndStop(wrongGuesses + 1);
}//end IF
}//end IF
}//end FOR
}//end pressKey()
}//end CLASS HangHitlerSript
}//end PACKAGE