Game assets not loading in Kongregate

110 Views Asked by At

I just created my first game, and am having some problems. I uploaded to Kongregate(but didn't publish yet), and in the preview the external files(music and three pictures) don't load. The rest of the game works fine.

If I try to change it so there is a zip with the assets in it and change the directory in the code, it falls apart.

I'm using FlashDevelop, so everything is programatic. I noticed the swf is only a few kb large, so is there anyway to compile the assets into the swf file and load them that way, or do I need to do something else?

Please help, and thanks in advance.

2

There are 2 best solutions below

1
On

Ok, let me explain then. Lets assume you have your main module game.swf that loads some image asset.jpg from the same folder. You test it locally, it works just fine. You put game.html, game.swf and asset.jpg on your site http://niamke.com/ and it still works. Then you publish your game as http://kongregate.com/games/niamke/myniamkegame/ and it suddenly stops loading asset.jpg.

Why

HTTP requests from Flash Player are handled via browser. So the browser gets a request for "asset.jpg" from Flash Plugin instance. There could be all kinds of content (including several Flash Apps) from several different domains within one page, so browser does not bother to figure the correct address out, and plainly tries to load in relatively to the topmost HTML document, basically http://kongregate.com/games/niamke/myniamkegame/asset.jpg which is not there.

How to avoid it

You should create a small piece of code that figures the correct URLs for the files you load. As soon as your content attached to Stage, any display object can access the loaderInfo object which contains the absolute SWF URL.

Usage

Files.parseURL(loaderInfo.url);

var aLoader:Loader = new Loader;
var aRequest:URLRequest = new URLRequest(Files.baseUrl + "asset.jpg");

Loader.load(aRequest);

Implementation

package
{
    public class Files
    {
        // Long live Bill Gates and Windows and backslashes.
        static public function figureSlash(value:String):String
        {
            var aSplit:Array = value.split("/");
            var oSplit:Array = value.split("\\");

            return (aSplit.length >= oSplit.length)? "/": "\\";
        }

        static public var baseUrl:String;
        static public var systemSlash:String;

        // Supposed to dissect the SWF url in order to
        // process relative resource file urls properly.
        static public function parseURL(value:String):void
        {
            // Figure correct slash.
            systemSlash = figureSlash(value);

            // Split SWF URL into Array and remove SWF name.
            var aSplit:Array = value.split(systemSlash);
            aSplit[aSplit.length - 1] = "";

            // Obtain the SWF root folder.
            baseUrl = aSplit.join(systemSlash);
        }
    }   
}
0
On

A comment brought me to the conclusion below: "I just linked the URLRequest to where the files were hosted online."