PDA

View Full Version : [AS2] Problems with hangman game


the_frog
01-06-2009, 10:56 AM
Hi!
I have tried to create a simple Hangman game, it works great,:) but I have two problems. Number one, the course I´m taking requires that there should be an if else statement in the actionscript programming (in an appropriate manner in connection with the user interaction) and I realy don’t know how to fix that. I´m tearing my hair.:confused: Number 2 Preferably I would like the program, alternating between several different words. How do I do? Some kind of array? :o Please can someone help a woman in need? ;)Maybe I better say that I am a beginner at actionscript to. This is the code as it looks today.

stop();
okey._visible=false;
akey._visible=false;
pkey._visible=false;
rkey._visible=false;
r2key._visible=false;
skey._visible=false;
wkey._visible=false;

var wrongletters:Array=["b","c","d","e","f","g","h","i","j","k","l","m","n","q","t","u","v","x","y","z"];
for (i=0; i<wrongletters.length; i++) {
my_btn=_root[wrongletters[i]+"_btn"];
my_btn.onRelease=function() {
nextFrame();
this._visible=false;
};
}

s_btn.onRelease=function() {
skey._visible=true;
this._visible=false;
checkAnswers();
};

p_btn.onRelease=function() {
pkey._visible=true;
this._visible=false;
checkAnswers();
};

a_btn.onRelease=function() {
akey._visible=true;
this._visible=false;
checkAnswers();
};

r_btn.onRelease=function() {
rkey._visible=true;
r2key._visible=true;
this._visible=false;
checkAnswers();
};

o_btn.onRelease=function() {
okey._visible=true;
this._visible=false;
checkAnswers();
};

w_btn.onRelease=function() {
wkey._visible=true;
this._visible=false;
checkAnswers();
};


function checkAnswers() {
if(skey._visible && pkey._visible && akey._visible && rkey._visible && r2key._visible && okey._visible && wkey._visible)
{ gotoAndStop(10);
}
};

rrh
01-06-2009, 10:25 PM
Man, I don't know about forcing in an 'else' where it isn't needed. Maybe just do an else {
//do nothing
}

As for making it have more words, that's not a simple thing to answer. You have a verbose but functional method going, I guess you could plop all that code into a giant switch statement, and based on a random number, run that code. Also attach a different batch of "akey, bkey, etc." using attachMovieClip();

A more adaptable version could also be put together using a lot of for loops and arrays, if you give all the *_btn attributes that contain an array of the associated *key, then you can make a generic function for all those buttons. Like, w_btn.letters = ['w'] etc. You looped through the wrongletters and assigned a generic function, so you have the basic concepts there.

the_frog
01-07-2009, 10:50 AM
I was thinking the same, that it felt wrong to force in an else, but you know what the teacher say.... :)

Thank you for the tip