Button text appears at the cost of knowing which answer was clicked

The code runs fine, but the problem I'm having is that the text box that is suppose to go onto of my answer buttons is behind the button itself... however, if I change two lines of code around, the answer validation code doesn't pick up the which answer was clicked (it returns undefined). This is infuriating!

Look for the line "//INSERT HERE." If I take the line, "var answerField:TextField = createText(answer,answerFormat,answerSprite,0,0,450);" and put it there instead, it returns the right answers (doesn't return undefined), but I can no longer see the text. I've included the createText function as well.

Ideas?

If uploading a .zip of the project would really help, I can arrange for that.
private function askQuestion()
    {
        trace("asking question...");

        // prepare new question sprite
        qBox = new QuestionBox();
        questionSprite = new Sprite();
        gameSprite.addChild(qBox);
        gameSprite.addChild(questionSprite);
        qBox.x = 240;
        qBox.y = 45;

        // create text field for question
        var question:String = dataXML.item[questionNum].question;
        questionField = createText(question,questionFormat,questionSprite,120,30,250);

        // create sprite for answers, get correct answer and shuffle all
        correctAnswer = dataXML.item[questionNum].answers.answer[0];
        trace("----" + correctAnswer + "----");
        answers = shuffleAnswers(dataXML.item[questionNum].answers);

        // put each answer into a new sprite with a circle icon
        answerSprites = new Sprite();

        for (var i:int=0; i<answers.length; i++)
        {
            trace("generating answers...");
            switch (i)
            {
                case 0 :
                    column = 135;
                    row = 150;
                    break;
                case 1 :
                    column = 135;
                    row = 260;
                    break;
                case 2 :
                    column = 340;
                    row = 150;
                    break;
                case 3 :
                    column = 340;
                    row = 260;
                    break;
            }
            var answer:String = answers[i];
            var answerSprite:Sprite = new Sprite();
            //INSERT HERE
            var button:Button = new Button();
            answerSprite.addChild(button);
            answerSprite.x = column;
            answerSprite.y = row;
            
            //TAKE THE LINE BELOW
            var answerField:TextField = createText(answer,answerFormat,answerSprite,0,0,450);
            
            answerSprite.addEventListener(MouseEvent.CLICK,clickAnswer);
            answerSprite.buttonMode = true;
            answerSprites.addChild(answerSprite);
        }
        trace("answers generated!");
        questionSprite.addChild(answerSprites);
        trace("question asked!");
        trace(":::awaiting user input:::");
    }

public function createText(text:String, tf:TextFormat, sprite:Sprite, x,y: Number, width:Number):TextField
    {
        var tField:TextField = new TextField();
        tField.x = x;
        tField.y = y;
        tField.width = width;
        tField.wordWrap = true;
        tField.multiline = true;
        tField.selectable = false;
        tField.defaultTextFormat = tf;
        tField.text = text;
        sprite.addChild(tField);
        return tField;
    }

Open in new window

cdeforgeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dgofmanCommented:
Will be nice to get Full Archive file. You can download winrar http://www.win-rar.com/download.html
Archive your project as RAR file change extension to PNG and upload to your thread.

cdeforgeAuthor Commented:
Woah, nice trick there. Here you go! The file in question is test.as and the issue is around line 195. You can just control+f "Insert the line" and you should find it.
brainbuster.png
dgofmanCommented:
Sorry cdeforge,
I forgot to mention I am using CS4 can you export your FLA to CS4 and you will able to upload FLA into this thread without my trick :)
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

cdeforgeAuthor Commented:
No problem, here you go.
BrainBuster.fla
dgofmanCommented:
Nice, one more request can you create a screenshot of your problem I don't know where exactly I need to look your textbox (may be you can draw a line using MS Paint Brash or SnagIt)
dgofmanCommented:
By the way what is better do not answer on any questions (timeout) and get 0 score or provide wrong answers and get -600 :)
cdeforgeAuthor Commented:
Okay, in the image, you see how in the top picture the button names are there but when I click it the answer validation comes back and say's the answer was undefined? It's not seeing what button I've clicked for some reason...

If you take that line of code that's commented on, and then move it up to where it says to insert it, you'll get what happens in the bottom frame. The text won't appear any longer, but the answer actually validates. In this instance, I got it right.

Yeah, the scoring isn't right by any means, lol. But I figured I'd tackle this issue first then move on to proper scoring.
example.jpg
dgofmanCommented:
Hi,
Your main problem you are using index 0 whats is a button instance.
Your button doesn't store an answer, its in your textField what is child index - 1 !!!!
To avoid this problem in the future I will recommend
1) Give name for your instances
2) Very important cast, if after casting you will get null, something wrong in the code

var answer:String = answers[i];
var answerSprite:Sprite = new Sprite();
var button:Button = new Button();
button.name = "myButton";

//Insert the line in question here to see a different result
answerSprite.addChild(button);

//Below is line in question
var answerField:TextField = createText(answer,answerFormat,answerSprite,0,0,450);
answerField.name = "myLabel";

Open in new window



// player selects an answer
		public function clickAnswer(event:MouseEvent)
		{
			trace(":::Input entered!:::");
			trace("validating answer...");
			var answerField:TextField = event.currentTarget.getChildByName("myLabel");
			// get selected answer text, and compare
			var selectedAnswer = answerField.text;
			trace("XXXXX " + selectedAnswer + " XXXXX");

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cdeforgeAuthor Commented:
Brilliant! I understand now... I'll definitely be using labels in the future. Thanks for posting the solution and explaining how to upload a .rar file as a .png as well!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.