0

我的 as3 代码有问题,我尝试将影片剪辑链接到不同的场景(测验场景)。所以,我总共有 2 个测验,第一个测验使用外部脚本(as),第二个测验与第一个测验具有相同的脚本,但我将动作脚本放在 fla 中。它有不同的xml。但随后出现此错误消息:

TypeError: Error #2007: Parameter format must be non-null.
at flash.text::TextField/set defaultTextFormat()
at hiragana/createText()[E:\flash\!!!! FLASH JADI\PAK ASHAR FIX\hiragana.as:80]
at hiragana/kuisdua()[hiragana::frame209:48]
at hiragana/frame209()[hiragana::frame209:249]

这是代码:

  // creates a text field
    public function createText(text:String, tf:TextFormat, s:Sprite, x,y:         Number, width:Number): TextField {
        var tField:TextField = new TextField();
        tField.x = x;
        tField.y = y;
        tField.width = width;
        tField.defaultTextFormat = tf; //looks like this is the source of problem (-.-)
        tField.selectable = false;
        tField.multiline = true;
        tField.wordWrap = true;
        if (tf.align == "left") {
            tField.autoSize = TextFieldAutoSize.LEFT;
        } else {
            tField.autoSize = TextFieldAutoSize.CENTER;
        }
        tField.text = text;
        s.addChild(tField);
        return tField;
    }

这是完整的代码

import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;

///*public class*/ kuisduaa extends MovieClip {

    // question data
    /*private*/ var dataXML2:XML;

    // text formats
    /*private*/ var questionFormat2:TextFormat;
    /*private*/ var answerFormat2:TextFormat;
    /*private*/ var scoreFormat2:TextFormat;

    // text fields
    /*private*/ var messageField2:TextField;
    /*private*/ var questionField2:TextField;
    /*private*/ var scoreField2:TextField;

    // sprites and objects
    /*private*/ var gameSprite2:Sprite;
    /*private*/ var questionSprite2:Sprite;
    /*private*/ var answerSprites2:Sprite;
    /*private*/ var gameButton2:GameButton;

    // game state variables
    /*private*/ var questionNum2:int;
    /*private*/ var correctAnswer2:String;
    /*private*/ var numQuestionsAsked2:int;
    /*private*/ var numCorrect2:int;
    /*private*/ var answers2:Array;

    /*public*/ function kuisdua() {

        // create game sprite
        gameSprite2 = new Sprite();
        addChild(gameSprite2);

        // set text formats
        questionFormat2 = new TextFormat("Arial",80,0xffffff,true,false,false,null,null,"center");
        answerFormat2 = new TextFormat("Arial",50,0xffffff,true,false,false,null,null,"left");
        scoreFormat2 = new TextFormat("Arial",30,0xffffff,true,false,false,null,null,"center");

        // create score field and starting message text
        scoreField2 = createText("",scoreFormat,gameSprite,-30,550,550);
        messageField2 = createText("Loading Questions...",questionFormat,gameSprite,0,50,550);

        // set up game state and load questions
        questionNum2 = 0;
        numQuestionsAsked2 = 0;
        numCorrect2 = 0;
        showGameScore2();
        xmlImport2();
    }

    // start loading of questions
    /*public*/ function xmlImport2() {
        var xmlURL:URLRequest = new URLRequest("kuis2.xml");
        var xmlLoader:URLLoader = new URLLoader(xmlURL);
        xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
    }

    // questions loaded
    /*public*/ function xmlLoaded2(event:Event) {
        dataXML  = XML(event.target.data);
        gameSprite.removeChild(messageField);
        messageField = createText("Tap Untuk Memulai",scoreFormat,gameSprite,-10,250,500);
        showGameButton("mulai");
    }

    // creates a text field
    /*public*/ function createText2(text:String, tf:TextFormat, s:Sprite, x,y: Number, width:Number): TextField {
        var tField2:TextField = new TextField();
        tField2.x = x;
        tField2.y = y;
        tField2.width = width;
        tField2.defaultTextFormat = tf;
        tField2.selectable = false;
        tField2.multiline = true;
        tField2.wordWrap = true;
        if (tf.align == "left") {
            tField2.autoSize = TextFieldAutoSize.LEFT;
        } else {
            tField2.autoSize = TextFieldAutoSize.CENTER;
        }
        tField2.text = text;
        s.addChild(tField2);
        return tField2;
    }

    // updates the score
    /*public*/ function showGameScore2() {
        scoreField2.text = "Soal: "+numQuestionsAsked2+"   Benar: "+numCorrect2;
    }

    // ask player if they are ready for next question
    /*public*/ function showGameButton2(buttonLabel:String) {
        gameButton = new GameButton();
        gameButton.label.text = buttonLabel;
        gameButton.x = 240;
        gameButton.y = 480;
        gameSprite2.addChild(gameButton);
        gameButton.addEventListener(MouseEvent.CLICK,pressedGameButton2);
    }

    // player is ready
    /*public*/ function pressedGameButton2(event:MouseEvent) {
        // clean up question
        if (questionSprite2 != null) {
            gameSprite2.removeChild(questionSprite2);
        }

        // remove button and message
        gameSprite2.removeChild(gameButton);
        gameSprite2.removeChild(messageField2);

        // ask the next question
        if (questionNum >= dataXML.child("*").length()) {
            gotoAndStop(6);
        } else {
            askQuestion2();
        }
    }

    // set up the question
    /*public*/ function askQuestion2() {
        // prepare new question sprite
        questionSprite2 = new Sprite();
        gameSprite2.addChild(questionSprite2);

        // create text field for question
        var question2:String = dataXML.item[questionNum].question2;
        if (dataXML.item[questionNum].question.@type == "text") {
            questionField2 = createText(question2,questionFormat2,questionSprite2,50,150,300);
        } else {
            var questionLoader2:Loader = new Loader();
            var questionRequest2:URLRequest = new URLRequest("triviaimages/"+question2);
            questionLoader2.load(questionRequest2);
            questionLoader2.y = 150;
            questionLoader2.x = 180;
            questionSprite2.addChild(questionLoader2);
        }

        // create sprite for answers, get correct answer and shuffle all
        correctAnswer2 = dataXML.item[questionNum2].answers.answer[0];
        answers2 = shuffleAnswers(dataXML.item[questionNum2].answers);

        // put each answer into a new sprite with a icon
        answerSprites2 = new Sprite();
        var xpos:int = 0;
        var ypos:int = 0;
        for(var i:int=0;i<answers2.length;i++) {
            var answerSprite2:Sprite = new Sprite();
            if (answers2[i].type == "text") {
                var answerField2:TextField = createText(answers2[i].value,answerFormat2,answerSprite2,30,-35,200);
            } else {
                var answerLoader2:Loader = new Loader();
                var answerRequest2:URLRequest = new URLRequest("triviaimages/"+answers2[i].value);
                answerLoader2.load(answerRequest2);
                answerLoader2.y = -22;
                answerLoader2.x = 25;
                answerSprite2.addChild(answerLoader2);
            }
            var letter:String = String.fromCharCode(65+i); // A-D
            var circle:Circle = new Circle(); // from Library
            circle.letter.text = letter;
            circle.answer = answers[i].value;
            answerSprite2.x = 100+xpos*250;
            answerSprite2.y = 350+ypos*100;
            xpos++
            if (xpos > 1) {
                xpos = 0;
                ypos += 1;
            }
            answerSprite2.addChild(circle);
            answerSprite2.addEventListener(MouseEvent.CLICK,clickAnswer); // make it a button

            // set a larger click area
            answerSprite2.graphics.beginFill(0x000000,0);
            answerSprite2.graphics.drawRect(-50, 0, 200, 80);

            answerSprites2.addChild(answerSprite2);
        }
        questionSprite2.addChild(answerSprites2);
    }

    // take all the answers and shuffle them into an array
    /*public*/ function shuffleAnswers2(answers:XMLList) {
        var shuffledAnswers2:Array = new Array();
        while (answers2.child("*").length() > 0) {
            var r:int = Math.floor(Math.random()*answers.child("*").length());
            shuffledAnswers2.push({type: answers2.answer[r].@type, value: answers2.answer[r]});
            delete answers2.answer[r];
        }
        return shuffledAnswers2;
    }

    // player selects an answer
    /*public*/ function clickAnswer2(event:MouseEvent) {

        // get selected answer text, and compare
        var selectedAnswer2 = event.currentTarget.getChildAt(1).answer;
        if (selectedAnswer2 == correctAnswer2) {
            numCorrect++;
            messageField2 = createText("Hai, kamu benar ! ",scoreFormat2,gameSprite2,-30,280,550);
        } else {
            messageField2 = createText("Iie, Jawabanmu Salah, yang benar adalah:",scoreFormat2,gameSprite2,53,280,370);
        }

        finishQuestion();
    }

    /*public*/ function finishQuestion2() {
        // remove all but the correct answer
        for(var i:int=0;i<4;i++) {
            answerSprites2.getChildAt(i).removeEventListener(MouseEvent.CLICK,clickAnswer);
            if (answers2[i].value != correctAnswer2) {
                answerSprites2.getChildAt(i).visible = false;
            } else {
                answerSprites2.getChildAt(i).x = 200;
                answerSprites2.getChildAt(i).y = 400;
            }
        }

        // next question
        questionNum2++;
        numQuestionsAsked2++;
        showGameScore2();
        showGameButton2("Lanjutkan");
    }

    // clean up sprites
    /*public*/ function CleanUp2() {
        removeChild(gameSprite);
        gameSprite2 = null;
        questionSprite2 = null;
        answerSprites2 = null;
        dataXML2 = null;
    }

第一个测验完美地使用了该代码,我不知道为什么会出现这个错误,

我是as3的初学者,所以我在很多方面仍然缺乏,有人可以帮助我吗?我真的很感激它.. :)

4

2 回答 2

1

您确定将非空TextFormat实例解析为 的第二个参数createText()吗?

该错误意味着您正在nulltf:TextFormat.

于 2013-03-13T00:00:59.673 回答
0

除非我遗漏了什么,否则您的问题就在这里:

messageField2 = createText("Loading Questions...",questionFormat,gameSprite,0,50,550);

&

messageField = createText("Tap Untuk Memulai",scoreFormat,gameSprite,-10,250,500);

我看到questionFormat2并被scoreFormat2实例化,但我从未看到questionFormator scoreFormat。您的错误表明defaultTextFormat(或TextFormat使用 提供setTextFormat(),以供将来参考)不能为空。Null 表示该对象尚未被实例化/创建。questionFormat 和 scoreFormat 永远不会被实例化。地狱,他们的变量甚至不存在。如果您使用的是适当的开发 IDE(FlashBuilder、FlashDevelop 等),您将永远无法编译此代码。

切换这两行以使用正确的格式,你应该没问题。

于 2013-03-13T00:59:59.927 回答