If you upgraded to Starling 1.7 and your app is broken, check your assets manager

Categories Actionscript

After updating to Starling 1.7, my game stopped compiling. After a quick debugging, I found the problem in the assets manager. Narrowing it down, it seems that the getTexture returns null if I try to load an atlas texture.

To fix it, change:

ActionScript

  1. var elemTexture:Texture = resources.assets.getTexture("elements");         
  2. var elemXml:XML = new XML(resources.assets.getXml("elements"));
  3. var elemAtlas:TextureAtlas = resources.assets.getTextureAtlas("elements");

To:

ActionScript

  1. var elemAtlas:TextureAtlas = resources.assets.getTextureAtlas("elements");

My assets are named: “elements.png” and “elements.xml”

In my game resources is a separate class that just loads all the elements, and the assets var is set to public, it looks like this:

ActionScript

  1. package com.cosmindolha.mathrun
  2. {
  3.     import flash.events.TimerEvent;
  4.     import flash.filesystem.File;
  5.  
  6.     import starling.events.Event;
  7.     import starling.utils.AssetManager;
  8.  
  9.     /**
  10.      * ...
  11.      * @author ... Cosmin Dolha ~ contact@cosmindolha.com
  12.      */
  13.     public class Resource
  14.     {
  15.         public var assets:AssetManager;
  16.        
  17.         private var disp:DataDispatcher;
  18.                
  19.         public function Resource(dd:DataDispatcher)
  20.         {
  21.             disp = dd;
  22.  
  23.             assets = new AssetManager();
  24.            
  25.             assets.keepAtlasXmls = true;
  26.    
  27.             var appDir:File = File.applicationDirectory;
  28.            
  29.             assets.enqueue(appDir.resolvePath("assets"));
  30.            
  31.             assets.addEventListener(Event.IO_ERROR, onError)
  32.  
  33.             assets.loadQueue(function(ratio:Number):void
  34.             {
  35.             if (ratio == 1.0)
  36.             {
  37.                 //trace("asstes loaded");
  38.                 disp.assetsLoaded();
  39.             }
  40.             });
  41.         }
  42.  
  43.         private function onError(e:Event):void
  44.         {
  45.                 trace(e.data);
  46.         }
  47.        
  48.     }
  49.  
  50. }
Share this
Facebooktwitter

Using JSON and actionscript 3 to display a leaderboard, back-end php and MySQL

Categories Actionscript, JSON

I have worked with XML for many years, now I had a chance to use JSON on a little pice of a project. The documentation might be confusing for many people so here is a simple easy to understand use of JSON with actionscript 3 and php, MySQL.

The idea is to list the top 10 users in a MySQL database based on their score.

Here is how the php part looks like:

  1. < ?php include 'config.php'; include 'opendb.php'; $sql = "SELECT username, score FROM app_table ORDER BY score DESC LIMIT 10"; $result = mysql_query($sql); $objArray = array(); while ($row = mysql_fetch_assoc($result)) { $objArray[] = $row; } echo json_encode($objArray); include 'closedb.php'; ?>

The trick is to save each result in an array $objArray[] so you will be able to properly use the JSON object in actionscript as an array.

In flash you would have to put a data grid component on the stage (ctr+f7 to open the components panel), name it myDataGrid.

ActionScript

  1. var serverAdress:String = "http://yourserver/"
  2. myDataGrid.columns = ["username", "score"];
  3. var request:URLRequest = new URLRequest();
  4. request.url = serverAdress + "top.php?"+Math.random();
  5. request.requestHeaders=[new URLRequestHeader("Content-Type", "application/json")];
  6. request.method=URLRequestMethod.GET;
  7. var loader:URLLoader=new URLLoader();
  8. loader.addEventListener(Event.COMPLETE, receive);
  9. function receive(e:Event):void
  10. {
  11. var myResults:Object = JSON.parse(e.target.data);
  12.  
  13. for (var i:int = 0; i < myResults.length; i++)
  14. {
  15. myDataGrid.addItem(myResults[i]);
  16. }
  17.  
  18. }
  19. loader.load(request);

To see the top username you would do:

ActionScript

  1. trace(myResults[0].username);

Now I think this is a lot more simpler that dealing with XML, although XML might have it’s strong points, JSON is a lot smaller so especially when dealing with large sets of data, JSON will save bandwidth and also might save some processing time on the client (I have to test this out).

Share this
Facebooktwitter