0

I'm trying to load data from a website in my AS3 program. However, urlLoader.load() never finishes, yet the program runs without any errors.

Here's my code:

Main.as:

import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import ttTextField;

public class Main extends Sprite 
{
    private var textField:ttTextField = new ttTextField("", false);

    public function Main():void
    {
        var urlRequest:URLRequest = new URLRequest("http://itch.io/api/1/API_KEY_REMOVED_FOR_SECURITY/my-games");
        urlRequest.method = URLRequestMethod.GET;

        var urlLoader:URLLoader = new URLLoader();
        urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
        urlLoader.addEventListener(Event.COMPLETE, completeHandler);

        urlLoader.load(urlRequest);

        stage.addChild(textField);
    }

    private function completeHandler(event:Event):void
    {
        textField.text = event.target.data;
    }
}

ttTextField.as:

import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormatAlign;
import flash.text.TextFormat;

public class ttTextField extends TextField
{
    [Embed(source = "../files/Fixedsys500c.ttf", embedAsCFF = "false", fontName = "fixedSys")] private var fontClass:Class;
    private var textFormat:TextFormat = new TextFormat("fixedSys", 24);

    public function ttTextField(string:String, centered:Boolean)
    {
        if (centered)
        {
            textFormat.align = TextFormatAlign.CENTER;
        }
        else
        {
            textFormat.align = TextFormatAlign.LEFT;
        }

        string = string;

        this.autoSize = TextFieldAutoSize.LEFT;
        this.mouseEnabled = false;
        this.embedFonts = true;
        this.textColor = 0xFFFFFF;
        this.defaultTextFormat = textFormat;
        this.text = string;
    }

}

ttTextField can display regular strings, so it doesn't seem like this class is the issue. I just included it for completion's sake.

I'm also building and running using Sublime Text 2. My sublime-build file is as follows:

{
"cmd": ["C:\\Users\\Dan\\Documents\\flex_sdk_4.6\\bin\\mxmlc.exe", "-output=C:\\Users\\Dan\\TTTT\\TTTT.swf", "-default-background-color=#00FF00", "-default-size=800,600", "C:\\Users\\Dan\\TTTT\\src\\Main.as", "-static-link-runtime-shared-libraries=true"],
"file_regex": "(.*)[(](\\d+)[)]:(?: col: (?:\\d+))? *Error: (.*)",
"selector": "source.actionscript",

"variants":
[
    {
        "name": "Run",
        "shell": true,
        "cmd": ["C:\\Users\\Dan\\TTTT\\TTTT.swf"]
    }
]

}

I've tried running the program in both Flash Player and Google Chrome. Every time the output is just a blank screen.

How would I get my URLLoader to actually load the URL? I can provide more information if needed. Thank you for reading.

TrampolineTales
  • 808
  • 3
  • 14
  • 31

1 Answers1

0

I think that simply your Main class should extend from MovieClip and not Sprite :

public class Main extends MovieClip 
{
    //  ...
}

And to debug your project in the browser, you can use a flash player debug version which you can download from here.

Hope that can help.

akmozo
  • 9,829
  • 3
  • 28
  • 44
  • I made Main extend MovieClip, but it didn't change anything. Same with running the SWF in the browser. – TrampolineTales Sep 05 '15 at 17:03
  • @TrampolineTales Try to use a flash player debug version in your browser, and use `ExternalInterface('console.log', event.target.data)` in your `completeHandler()` function. – akmozo Sep 05 '15 at 17:22
  • When I added `ExternalInterface('console.log', event.target.data)` the console output: `col: 50 Error: Incorrect number of arguments. Expected no more than 1.` However, I downloaded the debug version of Flash Player 10.1, and it now output the following error: `Error #2044: Unhandled securityError:. text=Error #2048: Security sandbox violation: file:///C|/Users/Dan/TTTT/TTTT.swf cannot load data from http://itch.io/api/1//my-games. at Main()` – TrampolineTales Sep 05 '15 at 17:59
  • 1
    @TrampolineTales Ah, sorry, it's : `ExternalInterface.call('console.log', event.target.data);`, for the security error, take a look on [my answer for this question](http://stackoverflow.com/a/28719316/2256820), you have all you need ... – akmozo Sep 05 '15 at 18:03
  • Thank you so much! It works now that I changed my security settings in the answer you provided. The control panel method worked for me since I'm using a stand-alone flash player. Thanks again! – TrampolineTales Sep 05 '15 at 18:11