Igli Kadija
Igli Kadija

Reputation: 105

Actionscript : Going through .as with a variable

I got a variable

var Something = "Text";

i want to import Text .as that is placed in abc folder so what can i do? I tried writing :

 import abc.Something;

Doesn't work, any help? Btw the main point again, i want to import Text

Upvotes: 0

Views: 56

Answers (1)

Larusso
Larusso

Reputation: 1151

What you trying to do will not work. The import statement is for classes. You could define your text string in a static variable and then use this variable in your code. Be aware, this is not good design.

package abc{
   public class TextHolder
   {
        public static var something:String = "Text"; 
   }
}

in another class:

package {
   import abc.TextHolder
   public class Main
   {
       var text:String = TextHolder.something;
   }
}

You could also use the include statement. With this statment you can include as files in to your current file

include "abc.script.as"

The variable in this script will then be included in the current script.

What you really want, I guess is to load text and other resources at runtime.

You can simply do that with the URL Loader. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLLoader.html

This example is from the adobe livedocs

package {
import flash.display.Sprite;
import flash.events.*;
import flash.net.*;

public class URLLoaderExample extends Sprite {

    private loader:URLoader;
    public function URLLoaderExample() {
        loader = new URLLoader();
        configureListeners(loader);

        var request:URLRequest = new URLRequest("urlLoaderExample.txt");
        try {
            loader.load(request);
        } catch (error:Error) {
            trace("Unable to load requested document.");
        }
    }

    private function configureListeners(dispatcher:IEventDispatcher):void {
        dispatcher.addEventListener(Event.COMPLETE, completeHandler);
        dispatcher.addEventListener(Event.OPEN, openHandler);
        dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
        dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
        dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
    }

    private function completeHandler(event:Event):void {
        var loader:URLLoader = URLLoader(event.target);
        trace("completeHandler: " + loader.data);

        var vars:URLVariables = new URLVariables(loader.data);
        trace("The answer is " + vars.answer);
    }

    private function openHandler(event:Event):void {
        trace("openHandler: " + event);
    }

    private function progressHandler(event:ProgressEvent):void {
        trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
    }

    private function securityErrorHandler(event:SecurityErrorEvent):void {
        trace("securityErrorHandler: " + event);
    }

    private function httpStatusHandler(event:HTTPStatusEvent):void {
        trace("httpStatusHandler: " + event);
    }

    private function ioErrorHandler(event:IOErrorEvent):void {
        trace("ioErrorHandler: " + event);
    }
}

}

Its a little bit to much but covers the whole process of loading ascii data.

Upvotes: 1

Related Questions