vrijdag 22 januari 2016

Haxe / Stencyl : Playing OGG file from filesystem

How can we play an OGG file directly of the filesystem.

Example: File /Users/admin/a.ogg

//setGameAttribute("SelectedFile", "/Users/admin/a.ogg");

var bas=sys.io.File.getBytes(getGameAttribute("SelectedFile"));
var ba:openfl.utils.ByteArray=openfl.utils.ByteArray.fromBytes(bas);
var abcd=new flash.media.Sound();
abcd.loadCompressedDataFromByteArray(ba,ba.length);
abcd.play();

donderdag 21 januari 2016

Haxe/Stencyl : Rotate Image

Stencyl lhas a block to spin the image instance, but then it is an instance instead of an image that we can upload.

This block code (Advanced -> Flow ) or Extension function can do the trick.

This is the code in an extension:




 public static function RotateImage(bitmapData:BitmapData):BitmapData{
                var degree=90;
                var newBitmap:BitmapData = new BitmapData( bitmapData.height, bitmapData.width, true );
                var matrix:Matrix = new Matrix();
                matrix.rotate( degree*(Math.PI/180) );
                if ( degree == 90 ) {
                        matrix.translate( bitmapData.height, 0 );
                } else if ( degree == -90 || degree == 270 ) {
                        matrix.translate( 0, bitmapData.width );
                } else if ( degree == 180 ) {
                        newBitmap = new BitmapData( bitmapData.width, bitmapData.height, true );
                        matrix.translate( bitmapData.width, bitmapData.height );
                }
                newBitmap.draw( bitmapData, matrix, null, null, null, true );
                return newBitmap;

        }

dinsdag 19 januari 2016

Haxe / Stencyl : Exporting an image to PNG and optionally upload to online server



PHP Server code:

/*
 *
 * Server Side script:
*
<?php
$name = $_GET["name"];
foreach($_POST as $i){
    // If image is found 
    if(strlen($i) > 10) $img=$i;
}
file_put_contents("uploadDirectory/".$name, $img);
file_put_contents("uploadDirectory/debug.log", $debug , FILE_APPEND | LOCK_EX);

?>
*/


Stencyl Code Block (HaXe)  that uses an Image Attribute which will be stored on the server. In this code the Attribute is called Image



var _URL="http://_YourServer_/YourDirectory/AboveScript.php";


var UploadFile=“MyFile.png”;

// Call the encode function of the BitmapData which is the Image
var png = _Image.encode (_Image.rect, new openfl.display.PNGEncoderOptions());

// Get the bytes from the PNG file
var b = haxe.io.Bytes.alloc(png.length);
png.position = 0; 
var bytes:haxe.io.Bytes = haxe.io.Bytes.alloc(png.length);
while (png.bytesAvailable > 0) {
   var position = png.position;  
   bytes.set(position, png.readByte());
}

// You could save it to the local file system on Native (Windows/Mac) 

// In this case we upload the data to a website that has the above mentioned script.          

// bytes variable contains the data that we can send to the serve



var boundary:String = "-----------RANDOMTEXT_GENERATED";
var newline:String = "\r\n";
var str:String="";
var dat="";

boundary="--AaB03x";

var endje="";
endje=endje+"Content-Disposition: form-data; name=\"Upload\"\r\n\r\nSubmit Query\r\n"+boundary+"\r\n";


var req:URLRequest = new URLRequest(_URL+"?name=“+UploadFile));
req.requestHeaders=new Array<URLRequestHeader>();

var hdr:URLRequestHeader=new URLRequestHeader("Accept","*.png");

req.verbose = true;
req.method = URLRequestMethod.POST;

req.data=bytes;

var ldr:URLLoader = new URLLoader(req);
// Probably not needed, but for large files it is nice to know when it is uploaded...
ldr.addEventListener(Event.COMPLETE, UploadDone);

For JPG/JPEG use JPEGEncoderOptions.

maandag 4 januari 2016

Haxe / Sytencyl : Rotate the mobile device

From current position :

var parent = nme.Lib.current;
var sw = parent.stage.stageWidth;
parent.y=0;
parent.x=sw;
parent.rotation=90;


To original:


var parent = nme.Lib.current;
var sw = parent.stage.stageWidth;
parent.y=0;
parent.x=0;
parent.rotation=0;

Unlock fix:
nme.display.Stage.setFixedOrientation( -1);