A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Unscramble the word game, please help

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    20

    Unscramble the word game, please help

    Hello all, I am a beginner programmer and I have difficulty figuring the codes for this game. Please help me

    I know there's an error on the on(release) code, do download the file, it is in AS2 thanks!

    PS I have a working file for this in AS3, but I need it in AS2.
    If you need the AS3 file, you can PM me, many thanks.


    Janice
    Last edited by JANNICEE; 01-04-2010 at 11:23 AM.

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    /*listen for mouse click*/
    check_mc.onPress = checkAnswer;
    next_mc.onPress = newWord;

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    20
    hello there, thanks for the reply. I did try both

    check_mc.onRelease = checkAnswer;
    check_mc.onPress = checkAnswer;

    but the problem is that now, once you test movie the question would be marked wrong instead of correct, when it's actually the correct answer.

    Do you know how to solve it?

  4. #4
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    /*load the xml file*/
    var xmlLoader:XML = new XML();
    xmlLoader.load("words.xml");
    xmlLoader.ignoreWhite = true;
    /*on completion of loading invoke the "parseXML" function*/
    xmlLoader.onLoad = parseXML;
    //
    var words = new Array();
    var actualWord;/*the word being displayed in unjumbled form*/
    var input;/*input typed in by the player*/
    /*listen for mouse click*/
    check_mc.onPress = checkAnswer;
    next_mc.onPress = newWord;
    //
    function parseXML() {
    	var i, j;
    	var menuLength = int;
    	/*length of the word array*/
    	menuLength = xmlLoader.firstChild.childNodes.length;
    	/*populate the globally defined array*/
    	for (i=0; i<menuLength; i++) {
    		words[i] = xmlLoader.firstChild.childNodes[i].firstChild.nodeValue;
    	}
    	/*show the jumbled word*/
    	showWord();
    	input_txt.text = "";
    	input_txt.text.setFocus = true;
    }
    /*show a jumbled word to solve*/
    function showWord() {
    	/*choose a random index of the words array and choose the word.
    	Thus we choose a random word everytime*/
    	var randChoice = int(Math.random()*words.length);
    	actualWord = words[randChoice];
    	/*display the jumbled word from the random array index*/
    	jumble_txt.text = jumbled(actualWord);
    	jumble_txt.autoSize = TextFieldAutoSize.LEFT;
    	//trace("--"+actualWord+"--");
    	/*show the button if hidden*/
    	check_mc._visible = true;
    }
    /*receive a string and return it after random jumbling*/
    function jumbled(word:String):String {
    	var len = word.length;
    	var rand;
    	var c;
    	var wordArray:Array = new Array();
    	var jumbledWord:String = new String();
    	/*convert the string to an array to avail 
    	 better indexed access*/
    	for (var i = 0; i<len; i++) {
    		wordArray[i] = word.charAt(i);
    	}/*randomise the array. The algorithm  
    	sequentually picks a letter and swaps it
    	with a random letter coming after it */
    	for (i=0; i<len; i++) {
    		rand = i+int(Math.random()*(len-i));
    		c = wordArray[i];
    		wordArray[i] = wordArray[rand];
    		wordArray[rand] = c;
    	}
    	/*reconvert array to string*/
    	for (i=0; i<len; i++) {
    		jumbledWord += wordArray[i];
    	}
    	/*return the jumbled string*/
    	return jumbledWord;
    }
    /*trim whitespaces from the given string*/
    function trim(word:String):String {
    	var len = word.length;
    	var trimed:String = "";
    	for (var i = 0; i<len; i++) {
    		if (word.charCodeAt(i) != 32) {
    			trimed += word.charAt(i);
    		}
    	}
    	return trimed;
    }
    /*check the answer typed in by the player*/
    function checkAnswer():Void {
    	input = input_txt.text;
    	/*remove any whitespaces entered*/
    	input = trim(input);
    	/*remove anything typed in by the user*/
    	input_txt.text = "";
    	/*hide the clicked button*/
    	check_mc._visible = false;
    	/*input is correct*/
    	if (input.toUpperCase() == actualWord.toUpperCase()) {
    		jumble_txt.text = "Yay that's correct! :)";
    	} else {/*if input is wrong*/
    		jumble_txt.text = "Aw you got it wrong, the answer is "+actualWord;
    	}
    }
    //show a new word
    function newWord():Void {
    	showWord();
    }

  5. #5
    Junior Member
    Join Date
    Dec 2009
    Posts
    20
    Hi thankyou so much. The codes did solve (:

    But I do have another question.

    How do you make the game such that when the user presses the "enter" on the keyboard, the answer will be checked?
    Last edited by JANNICEE; 12-03-2009 at 12:44 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center