Gradient over BitmapData object with Alpha - code snippet

January 14th, 2008

public function addGradient(input:BitmapData, color:String=”0xFFFFFF”, ratio:Number=0.5):BitmapData {

var output:BitmapData = new BitmapData(input.width, input.height, true);

var tempMatrix:Matrix = new Matrix();
tempMatrix.createGradientBox(input.width, input.height, -1.57);
var mult:uint = 255; // 50%

ratio = ratio * 255;

var outputMC:Sprite = new Sprite();

var imgMC:Sprite = new Sprite();
imgMC.graphics.beginBitmapFill(input);
imgMC.graphics.drawRect(0,0,input.width,input.height);
imgMC.graphics.endFill();
imgMC.cacheAsBitmap = true;

var gradMC:Sprite = new Sprite();
gradMC.graphics.beginGradientFill(”linear”, new Array(color, color), new Array(0,1), new Array(0,ratio), tempMatrix);
gradMC.graphics.drawRect(0,0,input.width,input.height);
gradMC.graphics.endFill();
gradMC.cacheAsBitmap = true;

outputMC.addChild(imgMC);
outputMC.addChild(gradMC);

output.draw(outputMC);

return output;

}

With this function your’re getting as output BitmapData with applied gradient with supplied from 0 to 1 ratio in 90 decrease. This is useful if you’re creating image reflection with simulated 3d effectGalerija.

I’ve used this in real 3D engine also.

What is AIR in short word?

January 7th, 2008

AIR is runtime enviroment, imagine it like extended Flash Player with support of some system specific classes like file manipulation and local database connection. To compile .air applications you can download Flash (or Flex, what ever you are using) plug-in. To run .air applications your customers will need small framework to download from Adobe’s site. To make .air applications you’ll need just to learn some new classes incorporated to AS3.

Why AIR is good for us? (webdevelopers)

We all would like to learn one language for all uses. With AIR you can use your knowledge of Action Script to create stand alone applications for desktops (ok, not so standalone since your users going to have AIR runtime…) . I find it very useful :)

learn more:

http://labs.adobe.com/technologies/air/

Extending MovieClip and visual structure in AS2

January 7th, 2008

In AS3 movieClip class is class like all other classes and it’s easy to extend it. 

From time to time as outsorcing Action Script developer you’ll get job that strictly demands AS2 because customer is affraid of poor Flash Player 9 support on internet. I hope this will ocur rarly in near future but it still can happend.

 

MovieClip instancing from AS2:

In AS2 you can instance property from MovieClip class but that property can just point to real MovieClip on stage:

 

 var imageMC:MovieClip;

 

but to create real MovieClip you’ll need to do something like this:

 

this[”imageMC”] = this.createEmptyMovieClip(”imageMC”, 7);

 

somewhere in code (also you can use dot paths it doesn’t matter: this.imageMC …)

Now you can access properties of imageMC:MovieClip from code.

 

Problem begins when you want to create class that extends MovieClip and when you want to instance it and see it on stage. here I’m giving example class with hack in constructor function:

 

import flash.geom.*;

 

class mcClass extends MovieClip {
 
 // some custom variables

 var myWidth:Number = 60;
 var myHeight:Number = 60;

 

// hacked constructor function

 static function createInstance(base_mc:MovieClip, myname:String, depth:Number) {
    var saveProto = MovieClip.prototype;
    MovieClip.prototype = ifsA2Image.prototype;
    var mc = base_mc.createEmptyMovieClip(myname, depth);
    MovieClip.prototype = saveProto;
    return mc;
  }
 
 function processStep():void {
 
  // some custom method
 }

 

now usage of this class is more elegant:

 

mcClass.createInstace(this, “someClip”, 40);

 

this is same code like in AS3:

var someClip:mcClass = new mcClass();

addChild(someClip);

 

where mcClass is class that extends MovieClip.

 

I found this hack somewhere on internet and I was really happy with it.

I don’t have URL of original author :) but this is shorted version of his post. 

 
 
}

 

 

 

 

 

 

getDefinitionByName() - Lovly tool, but…

January 7th, 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

How to repack class paths on painless way

January 7th, 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.


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