1

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.

  • Sounds a lot like path problem. If you are using the relative URLs, you are to keep in mind that they are processed relatively to the top document address, which is kongregate.com something. If this is the problem, you should parse the SWF URL and compose absolute resource URLs out of it. – Organis Apr 17 '17 at 15:06
  • Thanks for the tip. I don't exactly understand what you are talking about, but a bit of research led me to a better conclusion. I just linked the URLRequest to where the files were hosted online. Thanks! –  Apr 17 '17 at 17:47
  • I posted an explanation below, while you're at it you can also read about absolute and relative URLs: http://stackoverflow.com/questions/2005079/absolute-vs-relative-urls Using absolute URLs might have fixed your imminent problem but it is considered a bad practice, for if you move your content to another folder, or domain, or anything else, you'll get extra work out of it, which also couldn't be done without access to source code and build tools. Far too much trouble. – Organis Apr 17 '17 at 18:47
  • On the other side, yes there is a way to embed assets into a SWF with FlashDevelop. You declare `[embed src="localpath\file.jpg"] static var AssetName:Class;` and then you can instantiate stuff with `new Outerclass.AssetName()`. It might be better to embed several assets this way, not just a whole zip file. Check directive syntax tho, I don't have my sources handy to verify myself. – Vesper Apr 19 '17 at 08:29
  • _"is there anyway to compile the assets into the swf file"_ what @Vesper said... Just `embed` them by code & the output SWF will hold the asset content. – VC.One Apr 20 '17 at 03:51

2 Answers2

1

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);
        }
    }   
}
Organis
  • 7,243
  • 2
  • 12
  • 14
  • It makes sense now, thanks for the explanation. As I see there's a lot I still need to learn. –  Apr 18 '17 at 15:41
0

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