zondag 20 december 2015

Stencyl / HaXe : Loading files using URL

==Loading files from URLRequest==

All the targets accept URLRequest, but the iOS and Android devices like HTTPS and the
location seem to be different than the site they loaded from.

Flash has to reside on the same Base-URL in order to work.

The Alpha-stages of the game was posted on the Dropbox folder Public
The .SWF and the HTML with embeded code was posted here as well.

The APK (android) and API (iOS) were deployed using a website and iTunes.


First turn on the Tile API with Settings => Extentions => Tile API enable

We create a scene event/behavior

Add Event : Advanced : Custom Import
// Change bitmap
import nme.display.BitmapData;
import nme.display.Bitmap; // when converting data
import com.stencyl.Data;
import com.stencyl.models.scene.Tileset;
import com.stencyl.models.scene.Tile; // for empty Tileset creattion
// URL loading
import nme.display.Loader;
import nme.net.URLRequest;


Create a Code Block:
+ Add Event
 Advanced : Custom Code Block
//public variables:

var bitmapData:BitmapData;
var theBitmap:Bitmap;
var loader:Loader;

public function onComplete(event:Event){
   

// We will receive some data from the event
// Convert the event.currentTarget.loader.content (bitmap)
var bimg:Bitmap=new Bitmap(event.currentTarget.loader.content.bitmapData);
//draw the bitmap onto the bitmapdata we casted from the tileset
bitmapData.draw(bimg.bitmapData);

// If you know the TilesetID you can avoid this find-resource thing.
// The ID number of the sheet can be found in the Tile-editor in Stencyl.
// The number will increase if you add/remove tilesets.


// find the resource where the bitmap is going to be replaced into
var i:Int = 0;

for( i in 0 ... 100){

if(Data.get().resources.get(i)  != null ){
           
          // Temporary Tileset to compare the found resource with
          var ati:Array<Tile>=new Array();
      var tt:Tileset=new Tileset(0,0,"ats1_351",0,0,0,0,ati);
          if(""+Type.typeof(Data.get().resources.get(i) ) == ""+Type.typeof(tt)  ){
           // Replace the tileset datasource with the new bitmap
         cast(Data.get().resources.get(i), Tileset).pixels=bitmapData;
             // tell the devices it has a new tileset
          cast(Data.get().resources.get(i), Tileset).setupTilesheet();
      }
 }
}
   
}

In the start event we will create the bitmap:
var height:Int = 1024;
var width:Int = 1024;
bitmapData=new BitmapData(width,height, false,0x000000);

When we load the data:
Stencyl block

loader = new Loader();
loader.load(new URLRequest(_theURL));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);

IMPORTANT:
Add a Custom block from the Tile API:
[set tile at row [1] col: [1] layerID [0] using tileID [5] from tilesetID [3] ]


Geen opmerkingen:

Een reactie posten