按钮文本的显示是以不知道单击了哪个答案为代价的

发布于 2024-10-30 11:41:11 字数 3071 浏览 0 评论 0原文

代码运行良好,但我遇到的问题是,应该进入答案按钮的文本框位于按钮本身后面......但是,如果我更改两行代码,答案验证代码不获取单击的答案(它返回未定义)。这太令人气愤了!

查找“//INSERT HERE”行。如果我采取这一行,“var answerField:TextField = createText(answer,answerFormat,answerSprite,0,0,450);”并将其放在那里,它返回正确的答案(不返回未定义),但我再也看不到文本了。我还包含了 createText 函数。

有想法吗?

    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.x = column;
            answerSprite.y = row;

            // make it a button;
            answerSprite.addChild(button);
            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;
    }

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?

    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.x = column;
            answerSprite.y = row;

            // make it a button;
            answerSprite.addChild(button);
            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:::");
    }

createText function:

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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半衾梦 2024-11-06 11:41:11

您将在调用该方法后插入 answerSprite.addChild(button);

尝试将其放在 INSERT HERE 上方,就可以了。

IE

        var answerSprite:Sprite = new Sprite();
        var button:Button = new Button();
        // make it a button;
        answerSprite.addChild(button);
        var answerField:TextField = createText(answer,answerFormat,answerSprite,0,0,450);
            answerSprite.x = column;
            answerSprite.y = row;

You're inserting answerSprite.addChild(button); AFTER you make that method call.

Try putting that ABOVE the INSERT HERE, and you'll be fine.

i.e.

        var answerSprite:Sprite = new Sprite();
        var button:Button = new Button();
        // make it a button;
        answerSprite.addChild(button);
        var answerField:TextField = createText(answer,answerFormat,answerSprite,0,0,450);
            answerSprite.x = column;
            answerSprite.y = row;
愚人国度 2024-11-06 11:41:11

我需要为按钮命名,然后使用该名称来识别按钮。

I needed to give the button a name, then use that name to identify the button.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文