Archive for January, 2008

FLVPlayer common Problems (remote file access and instancing compiler-time problem)

Sunday, January 13th, 2008

FLV player is cool component that provides easy video streaming and displaying management, but, there are some common problems that new users may experience:

1. - Can’t display remote file (FLV stored on some web server, even if file is nest to caller SWF)

2. - When instancing FLAPlayer from fl.video.FLVPlayback; you are getting compiling-time error that fl.video.FLVPlayback definition couldn’t be found.

1. Problem is about security handling.

The simplest way to avoid this problem, is to put

crossdomain.xml file with content like this:

<?xml version=”1.0″?>
<!– http://www.adobe.com/crossdomain.xml –>
<cross-domain-policy>
<allow-access-from domain=”*” />
</cross-domain-policy>

this is example from Adobe’s site. Like this you’re allowing access from all domains to resource files in folder where this XML is. (this will allow .FLV, .SWF .XML and other files to be loaded from domain with crossdomain.xml). In case where you don’t have access to domain’s root, you may include such code in SWF file that is being loaded (alternativly, you can use SWF files with FVL player integrated inside it)

Security
.allowDomain(”www.example.com”)

where www.example.com is domain which contains resource files, with remote files.

and simpler solution could be to load crossdomain.xml file from alternative location inside domain with:

Security.loadPolicyFile(”http://socketServerHost.com/crossdomain.xml”)

2. It’s really strange that you can’t instance FLVPlayer object from code if you don’t have instanced component from Flash Component library and set it somewhere to stage or in asset library. So, solution is to check:

1. if you have proper  import fl.video.FLVPlayback;
statement at begining of your code

2. and if you have FLVPlayer in library asset.

Package and Classes reorganization - import paths replacement horror

Friday, January 4th, 2008

How your project grows, you are becoming aware that your starting package/class organization isn’t the best one and when you decide to change package organization you are facing fact that you have to open every single .as file and to manually replace import class path.

Same thing when you decide to change name of some class or method. Newer Visual Studios (like 2005 and 2008) have automatic replacment tool that sometimes can do great job, sometimes it replaces some property names just because you copy’n'pasted old definition, then started to change it, and without reading popup question clicked ok.

 Flash CS3 doesn’t have such ability but good thing is that there is small Open Source Java tool for quick and simple multi file string replacement.

First tool that I found worked nicely for me:

Tool’s name is SandR
and project’s url:
http://sourceforge.net/projects/sandr/

It can even scan folder and subfolders and replaces all files inside.

But, later I realized that it has problem with UTF-8 file encoding so it puts some bytes at begining of .as file, so you have same situation where you have to open each .as manually.

Better solution is another open source java tool: JReplace

http://sourceforge.net/projects/jreplace/

it worked very good in many cases (in all ;) )

Very handy.

Extended setter methods for DisplayObject3D (PV3D 1.5)

Friday, January 4th, 2008

We have extended a little bit PV3D 1.5 DisplayObject3D.as code to have some handy methods to set position and orientation for scene objects. You can insert this snippet at about 927th line of DisplayObject3D.as.

// IFS Upgrade
public function setPos(_x:Number, _y:Number, _z:Number, rotX:Number=NaN, rotY:Number=NaN, rotZ:Number=NaN):void {
this.x = _x;
this.y = _y;
this.x = _z;
if (isNaN(rotX)) { } else {
this.rotationX = rotX;
this.rotationY = rotY;
this.rotationZ = rotZ;
}

}

setPos function simply set xyz and rotation variables of particular DisplayObject3D instance.

public function posFromStr(input:String):void {
var tmpArr:Array = input.split(”,”);

this.x = Number(tmpArr[0]);
this.y = Number(tmpArr[1]);
this.z = Number(tmpArr[2]);

if (tmpArr.length > 3) {

this.rotationX = Number(tmpArr[0]);
this.rotationY = Number(tmpArr[1]);
this.rotationZ = Number(tmpArr[2]);

}

}

We added this function in order to make setting position and rotate params from XML easier. Function uses setting from string formatet like: “49,50,30,30,40,20″ - where first 3 params are coords and second 3 rotation per axis.

public function getPos():Number3D {
return new Number3D(this.x, this.y, this.z);
}

getPos return Number3D object with position of object

public function setRotation3D(input:Number3D, axis:String=”xyz”):void {

if (axis.indexOf(”x”) > -1) {
this.rotationX = input.x; }

if (axis.indexOf(”y”) > -1) {
this.rotationY = input.y; }

if (axis.indexOf(”z”) > -1) {
this.rotationZ = input.z; }

}
//////////////

SetRotation3D sets rotation of object using values from Number3D. in axis input you’re choosing on which axis to apply change…

I simply couldn’t surrve developing without these methods, they are simple but usefull.
I’m distributing it to you in this form to make it easier to integrate in future versions of PV3D/Away3D.

XML Scene Decription 0.1 for PV3D

Friday, January 4th, 2008

Here comes just first version of code that we’re using to describe PV3D scene in our own XML format.

public function sceneSetup(myXML:XML, sceneType:String=”Scene3D”):void
{
// new scene creation based on sceneType param
switch (sceneType) {
case “Scene3D”:
mainScene = new Scene3D(mainDisplay);
break;
case “MovieScene3D”:
mainScene = new MovieScene3D(mainDisplay);
break;
}

var entry:XML;

// materials creation
for each (entry in myXML.mats.ent) {

var tmpMat:MaterialObject3D;
var tmpType:String = entry.@type;

switch (tmpType) {

case “ColorMaterial” :
tmpMat = new ColorMaterial(Number(entry.@color), Number(entry.@alpha));
materialsArray.push(tmpMat);
mainScene.materials.addMaterial(tmpMat, entry.@name);
break;

case “BitmapFileMaterial” :
tmpMat = new BitmapFileMaterial(entry.@url);
materialsArray.push(tmpMat);
mainScene.materials.addMaterial(tmpMat, entry.@name);
break;

}

}

// creation 3D objects
for each (entry in myXML.genObj.ent) {

var tmpMat:MaterialObject3D = mainScene.materials.getMaterialByName(entry.@mName);
var tmpArr:Array = entry.@size.split(”,”);
var tmpArrSeg:Array = entry.@segs.split(”,”);

var tmpObj:DisplayObject3D;
var tmpType:String = entry.@type;

// type switch structure
switch (tmpType) {

case “plane” :
tmpObj = new Plane(tmpMat, Number(tmpArr[0]), Number(tmpArr[1]), Number(tmpArrSeg[0]), Number(tmpArrSeg[1]));
break;

case “cube” :
tmpObj = new Cube(tmpMat, Number(tmpArr[0]), Number(tmpArr[1]), Number(tmpArr[2]), Number(tmpArrSeg[0]), Number(tmpArrSeg[1]), Number(tmpArrSeg[2]));
break;

}

mainScene.addChild(tmpObj, entry.@name);

}

}

This function is part of our RTS interface class that we are using for TheFirstExodus development (http://tesslabs.com/blog/2008/01/04/thefirstexodus-rts-flash-webbased-lunar-simulation/) and it has mainScene as public veriable:

public var mainScene:Scene3D;

XML description file looks like this:

<root>
<genObj>
  <ent type=”plane” mName=”ground” size=”1000,1000″ segs=”1,1″ pos=”0,0,0″ name=”podloga” />
  <ent type=”cube” mName=”cube” size=”200,200,200″ segs=”1,1,1″ pos=”0,0,0″ name=”kocka” />
 </genObj>
 <mats>
  <ent type=”ColorMaterial” name=”ground” color=”0×336633″ alpha=”1″ />
  <ent type=”ColorMaterial” name=”cube” color=”0×666666″ alpha=”1″ />
 </mats>
</root>

this version supports only BitmapMaterial and ColorMaterial as well as Plane and Cube Objects.
this is only to give you idea. Today I’ve started to learn Away3D
(I simple missed until today info that PV3D has sistem project… :( so now I have to pass whole code again )

and I’ll create same solution for all Away3D supported object and materials.

TheFirstExodus (RTS / Flash-WebBased / Lunar Simulation)

Friday, January 4th, 2008

Tesseract Studio started big project: The First Exodus (www.thefirstexodus.com)

TFE is Real Time Strategy - Simulation game with 3D graphics.
First part of game is about colonization of Moon. Game’s site is still under planning, we have a lot of artwork to show, but we need some more time.

Game story is under construction as well as game engine is in progress. Until today we have adapted PaperVision3D engine (1.5) and started to make serious extensions on it but now I’ve realized that there is Away3D, PV3D sister project.

Some features that we will create for game and share it with PV3D/Away3D community are:

- Displacment modifier (moves Vertices on selected axis using Brightness data of loaded Bitmap)
(in order to make terrean maps for our game)

- DynamicBitmapMaterial (material that uses Bitmap chache from any Sprite instance)

- PrerenderedObject - Sprite that shows different preloaded Bitmaps depending on angle of view

- Isometric Camera - Camera that doesn’t have perspective deformation, but, project view on isometric way

- AdobeFX - System to put adobe’s FX like Glow, Shadow, Blur on particular object or Face

- Particle Face emitter - Particles emission from particular face

- ifs3Dmesh - we will create our own 3D XML based format in order to faciliate our extensions

- ifs3Dscene - XML decription of scene with object’s instancing based on data in XML, already done, but for PV3D 1.5

XML driven object instancing and Array pushing

Friday, January 4th, 2008

I used similar XML  to make instances of class described in typing atributte of ogroup node.

<root>

 <ogroup typing=”ifs.ifs3D.Camera” arrayName=”camArray” >
  <ent camPos=”200,200,800? camTar=”0,0,0? camRot=”0,0,0? fov=”90? focus=”100? name=”maincam” />
 </ogroup>

</root>

 With small addaptation you can use following function (when XML data is already loaded and passed to function as input:XML).

output is refference to container object.
arrayName XML attribute telling to function in which array to push instanced object.

That means that your output object must have Array named as declared in XML attribute.

function loadObjectArrays(input:XML, output):void {

   var countArrays:Number;
   var countObjects:Number;
   var ogroups:Number;

   // creating array
   var tempArray:Array = new Array();

   // ogroups loop
   for each (var property:XML in input.ogroup) {

    var objectTyping:String = property.@typing;
    var arrayName:String = property.@arrayName;

    // if inserting successfull
    if (output.hasOwnProperty(arrayName)) {
    
     // instancing blank array
     output[arrayName] = new Array();
     
     // object loading loop
     for each (var entry:XML in property.ent) {

        var tmpClass:Object = getDefinitionByName(objectTyping);
        
        var tempObj:Object = new tmpClass();
        
        // checking if class has particular properties and if it has, applying values from XML
        if (tempObj.hasOwnProperty(”iParent”)) {
         tempObj.iParent = output;
        }
        if (tempObj.hasOwnProperty(”fromXMLentry”)) {
         tempObj.fromXMLentry(entry);
        }
        
        output[arrayName].push(tempObj);
      }
     }
     // object loading loop end

   }
}

hasOwnProperty is very usefull method. In my case, almost all of classes that I’m instancing have iParent property and fromXMLentry():void method (this method sets other object’s properties from XML node (<ent />), but not all. In case where I havn’t used hasOwnProperty():Boolean method (inherited from Object Class, that means that every class has it) I’ll have runtime error when class declared in typing=”" attribute is without mentioned property/method.

getDefinitionByName() problem

Friday, January 4th, 2008

Once I spotted getDefinitionByName function in flash.utils.* namespace I was very happy with idea to solve objects instancing on elegant and easy way. I’ve created XML decription format like this:

 <ogroup typing=”ifs.ifs3D.Camera” arrayName=”camArray” >
  <ent camPos=”200,200,800″ camTar=”0,0,0″ camRot=”0,0,0″ fov=”90″ focus=”100″ name=”maincam” />
 </ogroup>

 where <ogroup> with it’s <ent /> nodes describes objects and putting it to camArray:Array.
Here usage of getDefinitionByName() sounds great, because ifs.ifs3D.Camera is real classpath.

 But :)

 Problem is that both Flash and Flex compiler AI in this way can’t see that my code will use ifd.ifs3D.Camera class and in compile time it doesn’t put it in resulting bytecode. So I was getting error at runtime:

- Camera value is undefined

Solution is to somewhere in code use classpath that you’re going to call with getDefinitionByName(); in command like:

var proto_i3D:ifs.ifs3D.Camera;

or just

var proto_i3D:Camera;

 So it’s imposable to create totally XML driven object instancing, but getDefinitionByName() is still usable to avoid big switch(xml.@objecttype) structure.

Usefull links:

http://www.as3guru.com/?p=5 - Here I found solution for this problem


tesseractstudio.org | all rights reserved 2005-2008. | tesseractstudio ltd, Serbia
omnetwork.net | tutorialautomated.info | osgamer.org | freeflashscripts.com | tesslabs.com | thefirstexodus.com | thefirstexodus.com