更改UrlLoader.load

时间:2013-12-20 19:58:13

标签: actionscript-3 flash-cs5 urlloader urlrequest

package  {

    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent; 
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    import flash.text.StyleSheet; 
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.text.TextFormat;
    import flash.text.*
    import flash.net.*


    public class SpeechBox extends MovieClip{

        public var textLoader:URLLoader = new URLLoader();
        public var box:Sprite = new Sprite();
        public var nextBox:Sprite = new Sprite();
        private var nextText:TextField = new TextField();
        private var textBox:TextField = new TextField();
        private var speechText:String;
        public var _speechBoxCheck:Timer = new Timer(1000);
        public var clickedNext:Boolean = false;

        public function SpeechBox() 
        {
            textLoader.addEventListener(Event.COMPLETE, onLoaded);
            textBox.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownScroll); 
            _speechBoxCheck.addEventListener(TimerEvent.TIMER, speechBoxCheck);
            _speechBoxCheck.start();
            //////////////////SPEECH BOX///////////////////
            box.graphics.lineStyle(3.5,0xffffff);
            box.graphics.beginFill(0x003366, .35);
            box.graphics.drawRoundRect(0,0,650,145,20);
            box.graphics.endFill();
            box.x = 100;
            box.y = 450;
            addChild(box);
            //////////////////SPEECH TEXT///////////////////
            var speechFont = new DataText();
            var textFormat:TextFormat = new TextFormat(); 
            textFormat.font = speechFont.fontName;
            textFormat.align = TextFormatAlign.LEFT;
            textFormat.leading = 3;
            textFormat.color = 0xFFFFFF;
            textFormat.size = 16; 

            textBox.defaultTextFormat = textFormat;
            textBox.width = 620; 
            textBox.height = 115; 
            textBox.x = box.x + 14;
            textBox.y = box.y + 14;
            textBox.multiline = true; 
            textBox.wordWrap = true; 
            textBox.selectable = false;
            addChild(textBox);
            //////////////////NEXT BUTTON///////////////////
            nextBox.graphics.beginFill(0x000000, 0);
            nextBox.graphics.drawRect(0,0,50,30);
            nextBox.graphics.endFill();
            nextBox.x = box.x + 600;
            nextBox.y = box.y + 115;
            nextText.defaultTextFormat = textFormat;
            nextText.text = "Next";
            nextText.textColor = 0xffffff;
            nextText.autoSize = "left";
            nextText.selectable = false;
            nextText.mouseEnabled = false;
            nextText.x = nextBox.x + 2
            nextText.y = nextBox.y + 5
            nextBox.buttonMode = true;
            //nextBox.mouseEnabled = true;

            nextBox.addEventListener(MouseEvent.MOUSE_DOWN, clickNext);
            nextBox.addEventListener(MouseEvent.MOUSE_OVER, moveOver);
            nextBox.addEventListener(MouseEvent.MOUSE_OUT, moveOut);
        }
        function onLoaded(e:Event):void {
            trace(e.target.data);
            textBox.text = e.target.data;
        }
        function mouseDownScroll(event:MouseEvent):void 
        { 
           textBox.scrollV+=4; 
           textBox.addEventListener(MouseEvent.MOUSE_UP,mouseup);
        } 
        function mouseup(event:MouseEvent):void 
        { 
            if(textBox.scrollV == textBox.maxScrollV) 
            {
                addChild(nextBox);
                addChild(nextText);
            }
        }
        function clickNext(event:MouseEvent):void 
        { 
            trace("click");
            clickedNext = true;
            _speechBoxCheck.stop();
            (parent as Main).onTransition.start();
            textBox.scrollV = 0;
            textLoader.removeEventListener(Event.COMPLETE, onLoaded);
            this.parent.removeChild(this);
        }
        function moveOver(event:MouseEvent):void 
        {
            nextText.textColor = 0xffcc00;
        }
        function moveOut(event:MouseEvent):void 
        {
            nextText.textColor = 0xffffff;
        }
        ///////////////////////////////////////////////////////////////
        function speechBoxCheck(event:TimerEvent)
        {
            if ((parent as Main).introduction == true)
            {
                textLoader.load(new URLRequest("Texts/LV1introduction.txt"));
                trace("beginning");
                (parent as Main).onTransition.stop();
            }
            if ((parent as Main).levelNum == 1)
            {
                textLoader.load(new URLRequest("Texts/LV1complete.txt"));
                trace("go to lv 2")
                (parent as Main).onTransition.stop();
            }
            if ((parent as Main).levelNum == 2)
            {
                textLoader.load(new URLRequest("Texts/LV2complete.txt"));
                trace("go to lv 3")
                (parent as Main).onTransition.stop();
            }
        }

    }

}

编辑:当游戏开始时,LV1介绍文本开始。一旦scrollV等于maxScrollV,就会出现下一个按钮。点击它,它将自行删除并开始游戏。一旦你击败第一阶段,levelNum自动等于2,我再次从我的主文档类添加这个类。但是,它会反复显示相同的文本,无论级别如何。

urlLoader也始终保持不变?如果是这样,我该怎么改呢?

1 个答案:

答案 0 :(得分:1)

可以重复使用URLLoader来使用新的URLRequest实例加载其他数据。重复使用相同的URLLoader实例来加载另一个文件是完全可以的。您的问题不在URLLoader中,而是在逻辑或其他地方。您最好尝试使用调试器来确保变量级别的值正确为2。

  1. 为什么每秒都要加载文本文件?加载它们只会改变级别。
  2. 这个textLoader实例是否附加了事件监听器?
  3. ::::::::: EDITED :::::::::

    1. 您正在clickNext()调用中从textLoader中删除事件侦听器。 textLoader将加载文件,但不会运行onLoaded()来更新textBox.text

    2. 你的speechBoxCheck()方法做错了。 1.你必须在speechBox()方法中只进行1次load()调用。方法中的条件不是独占的,并且在进行多次load()调用时可能会导致问题(先前的加载操作将被取消)。考虑“其他如果”链。

    3. 建议不要以这种方式加载文件。应避免不必要的I / O操作,尤其是在运行时。仅在需要时加载文件;在这种情况下,它是在级别更改时。