Friday, November 4, 2011

Integrating Flash Sessions With Your Application Cookies

How To Integrate Flash Into Your Website Sessions

The only way to achieve this is to submit the session id as a POST variable from the flash movie using flashvars to bring the session id into the flash movie.

<param name="FlashVars" value="sessionid=7r2t5u793fil9or6ka95cas1q2" />
 
We resume the session using the same id when flash makes a request of it's own. So in the PHP code for the request page we resume as follows:

        session_id($_POST["sessionid"]);
        if ( session_start() == TRUE )


However,  a subsequent call generates the following error:

ErrorException [ 8 ]: A session had already been started - ignoring session_start() ~ SYSPATH/classes/kohana/session/native.php [ 43 ]



It might seem as though the session variables are being clobbered as the error is caused by reading unset session variables.

You can try properly destroying the session at the end of your request:
session_destroy();


You can also try ensuring the correct cookie is used:


        session_name("session");



To check on your session variables use print_r which will type out the contents of the session array.
        print_r($_SESSION);


Checking out cookies in your browser you might see a new cookie appear named PHPSESSID the first time flash makes a request using the session.

This PHPSESSID cookie named is automatically created by a simple session_start call with no frills.


To disable the automatic cookie PHPSESSID and get rid of those errors you can disable the unused PHPSESSID variable using the following two statements before your controller's session instantiation:


        ini_set("session.use_cookies", "off");
        ini_set("session.use_trans_sid", "off");


Wednesday, November 2, 2011

Dynamic Asset & Resource Loading: Flixel Tutorial in Flashdevelop

Flixel SWF Dynamic Asset & File Resource Loading Tutorial in Flashdevelop

A most important subject is that of using loader content as embedded classes in Flixel.

Many forums present pieces of a full solution and so I will present to you your options for For my flixel asset loader I use Bulk-Loader which will be featured in the examples.

Loading Unpackaged Asset Files In Flixel

Necessary for direct file loading as Flixel is used to working with embedded classes and does not support run-time files without modification.

External Image Direct File Loading

ExternalImage.setData(MyStaticClass.staticBulkLoaderInstance.getBitmap(ProfileImg).bitmapData, ProfileImg);
var profile:FlxSprite = new FlxSprite(0, 0);
profile.loadGraphic(ExternalImage); profile.x = profile.y = 5; profile.add(profile);


Loading SWF-packaged Asset Files In Flixel

An easier way to load resources without modifying flixel and in a more organized fashion is to create a separate SWF file with all of your graphic embeds.

Your will have to create a standalone swf with nothing but includes of the public static type in addition to the class extending movieClip.

Here is the structure of the SWF file:

package { import flash.display.MovieClip;
public class MyAssetClassStub extends MovieClip
{ [Embed(source = "data/asset1/asset.png")] public static var BattleScreenEnemy:Class;
[Embed(source = "data/video/assset1/movie.swf", mimeType = "application/octet-stream")] public static var zephalapodKingSlap:Class;
[Embed(source = "data/sfx/sound1.mp3")] public static var AttackSound1:Class; } }

The loading code goes as follows:

bulkLoader = new BulkLoader("mainpageloader");

bulkLoader.add("http://test.com/media/assets/MyAssetClass.swf", { id:"MyAssetClass", priority:100 } );
As long as you've checked that the download progress is 100% then you can do the following to access the packaged asset classes for use in Flixel FlxSprite.
name = "MyAssetClass";
assets = Game.bulkLoader.getMovieClip("http://test.com/media/assets/"+name+".swf"); //BY URL or by ID as the doc says but probably need custom object with .id instantiated
test = assets.loaderInfo.applicationDomain.getDefinition(name + "Stub") as Class;

Now you can either save references to classes (created by embedding assets) in the asset SWF file.
var SoundOne:Class = test.SoundOne as Class;
//this extra step is useful in playing videos using ByteArrays by instantiating from the test.SoundOne class.

Likewise, the classes can be access directly from the class instance with the data from getMovieClip.

Using SWF files to package your assets provides for an excellent way to map and combine assets in logical packages for dynamic loading without creating new file structures for a re-organization of files. Using SWF files is also important for drastically reducing the amount of calls to the server compared to dynamically loading a large group of files individually.