Question

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

    }

}

EDIT: When the game starts, the LV1 introduction text starts. Once the scrollV equals maxScrollV, a next buttons appears. Click that, it will delete itself and the game starts. Once you beat stage one, levelNum automatically equals 2 and I add this class again from my main document class. However, it will show the same text over and over, regardless of what level.

So does the urlLoader always stay the same? If so, how can I change it?

Was it helpful?

Solution

URLLoader can be reused to load another data with new URLRequest instance. It is completely OK to reuse same URLLoader instance to load another file. Your problem is not in URLLoader, but in logics or somewhere else. You'd better try debuggers to make sure variable level has correct value of 2.

  1. Why are you loading text file EVERY second? It would be ok to load them only level is changed.
  2. does this textLoader instance have event listeners attached?

:::::::::EDITED:::::::::

  1. You are removing event Listeners from textLoader in clickNext() call. textLoader will load file, but will not run onLoaded() to update textBox.text

  2. Your speechBoxCheck() method is doing it wrong. 1. You must make only 1 load() call in speechBox() method. Your conditionals in the method are not exclusive, and it may cause trouble when multiple load() calls are made (previous loading operation will be canceled). consider "else if" chain.

  3. it is not recommended to do something like loading files in this fashion. Unnecessary I/O operations, especially in runtime, should be avoided. Only load files when it is needed; In this case, it is when level changes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top