PDA

View Full Version : [AS3] hangman help


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

scarce
04-29-2009, 10:16 PM
When you open your fla, in the properties box where it says stage specs and fps, you'll see a text box called document class, put the name of the document class right there. That's should do it

Charm47
04-29-2009, 10:37 PM
.fla file: "hangHitler.fla"
.as file: "HangHitlerScript.as"

My animation covers 7 frames inside of a movie clip; in the properties of the movie clip, "Hanging_mc," I exported to the HangHitlerScript class inside of the HangHitlerScript.as file; when I tried to test movie the following errors occur:

TypeError: Error #2007: Parameter text must be non null.
at flash.text::TextField/set text()
at HangHitlerScript()
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at hangHitler_fla::MainTimeline()

Then I messed with adding in the document class name since I never had one, and someone I talked with mentioned I should have one... so I went to the properties in the .fla and typed "HangHitler". After testing it again the error description says: "5007: An ActionScript file must contain at least one externally visible definition."

Now I have the MC pointing to HangHitlerScript.as with all the code and the document class pointing to HangHitler.as with absolutely nothing in it. I am unsure of what I need to put in that secong .as file.

Sorry for dragging this out and making it confusing. I hope I am explaining things clearly and with enough info. Thanx for your help.

scarce
04-29-2009, 10:49 PM
.fla file: "hangHitler.fla"
.as file: "HangHitlerScript.as"

My animation covers 7 frames inside of a movie clip; in the properties of the movie clip, "Hanging_mc," I exported to the HangHitlerScript class inside of the HangHitlerScript.as file; when I tried to test movie the following errors occur:

TypeError: Error #2007: Parameter text must be non null.
at flash.text::TextField/set text()
at HangHitlerScript()
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at hangHitler_fla::MainTimeline()

Then I messed with adding in the document class name since I never had one, and someone I talked with mentioned I should have one... so I went to the properties in the .fla and typed "HangHitler". After testing it again the error description says: "5007: An ActionScript file must contain at least one externally visible definition."

Now I have the MC pointing to HangHitlerScript.as with all the code and the document class pointing to HangHitler.as with absolutely nothing in it. I am unsure of what I need to put in that secong .as file.

Sorry for dragging this out and making it confusing. I hope I am explaining things clearly and with enough info. Thanx for your help.

There's only one document class and it runs for the whole movie, not just a frame. You need to put the name of the referenced class file I.e: HangHitlerScript. Not the fla name of HangHitler, unless you rename your .as file to HangHitler

scarce
04-29-2009, 10:56 PM
Ps, don't physical link movieclips to the .as file unless it's in the .as file stating "extends movieclip" in the public decliration

Charm47
05-01-2009, 09:24 PM
So my understanding has not gained much ground. In a clueless attempt I am attaching print screens of my project that will hopefully explain things better than I can, and possibly help you guys help explain it to me in a less intelligent way! Apparently I am still clueless.

Charm47
05-05-2009, 11:30 PM
So I managed to switch a few things to run the hangman game. I took the the linkage off the movie clip and in the AS code instead of extends Movie Clip, I changed it to extends Sprite. The other major adjustment was adding "hanging." before the frame is advanced when a guess is wrong. In order for that to call to the instance of the movie clip, of course I had to also name it "hanging"!!

hanging.gotoAndStop(wrongGuesses + 1);

Now I will try to create text fields to take user input as the word/phrase to guess.

Thanx for all the help.