Browse the notebook

Tag: flash

Senocular’s Two Sided 3D Clip

CLICK HERE FOR TODAY’S SNIPPET

In a recent kirupa thread, senocular posted an very useful snippet to aid in the creation of a two sided 3D MovieClip. The thread also contains an in depth description of polygon winding.

Description of Polygon Winding

I created a navigation demo using this technique.

Here is the source for the above demo

UPDATE
Justin Windle of soulwire has written a nice class called PaperSprite that uses this technique. The class is worth checking out – it’s nicely designed… read more about it here.

Read snippet: Senocular’s Two Sided 3D Clip

Gradient Glow

var colors:Array = [0xFF0000, 0x666699, 0x223322, 0xCCCCDD, 0xFFEEFF];
var alphas:Array = [0, 1, 1, 1, 1];
var ratios:Array = [0, 50, 100, 200, 255]
var filter:GradientGlowFilter = new GradientGlowFilter(0, 0, colors, alphas, ratios, 30, 30, 1, 2, "full", true);
 
var circles:Shape = new Shape();

 for (var i:int = 0; i<30; i++){
  with(circles.graphics) beginFill(0xFF0000), drawCircle(Math.random()*500, Math.random()*400,10+Math.random()*40);
 }
 addChild(circles);
 
 circles.filters = [filter];

I don’t see GradientGlowFilter used much. But with some tweaking you can probably get some decent stuff out of it.

Read snippet: Gradient Glow

Bring Display Object to Top

mc.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
function onRollOver(evt:MouseEvent):void {
  addChild(MovieClip(evt.currentTarget));
}

This one is very simple, but it’s important to note that using addChild() on something that is already on the display list simply brings it to the top. Back in AS2 we used to do:

mc.swapDepths(1000);
Read snippet: Bring Display Object to Top

Procedural Pattern – Navigation

var currentBtn:MovieClip;

var nav:Sprite = new Sprite();
nav.x = nav.y = 20;
addChild(nav);

createBtns();

nav.addEventListener(MouseEvent.CLICK, onClickBtn);

function createBtns():void{
	for (var i:int = 0; i<10; i++){
		var btn:MovieClip = new MovieClip();
		with(btn.graphics) beginFill(0x666666), drawRect(-10,-10,20,20);
		btn.x = i * (btn.width + 10);
		btn.buttonMode = true;
		btn.num = i;
		btn.alpha = .5;
		nav.addChild(btn);
	}
}

function onClickBtn(evt:MouseEvent):void {
	//
	// this is the important part
	//
	if (currentBtn){
		currentBtn.scaleX = currentBtn.scaleY = 1;
		currentBtn.alpha = .5;
	}
	currentBtn = MovieClip(evt.target);
	currentBtn.scaleX = currentBtn.scaleY = 1.3;
	currentBtn.alpha = 1;
	trace("current button:", currentBtn.num);
	//
}

This code will create 10 boxes that represent buttons on a navigation. When you click a box it indicates that it is selected by scaling up and changing alpha. Simple enough…

The term Procedural Pattern is just a spin on the idea of Design Patterns. I’ve come up with lots of small patterns to solve simple recurring problems over the years. Most of these relate to things like drawing programs, ecards and games.

Read snippet: Procedural Pattern – Navigation

FileReference.save()

var file:FileReference = new FileReference();

stage.addEventListener(MouseEvent.CLICK, onClick);

function onClick(evt:MouseEvent):void {
	file.save("some text. nsome more text", "actionsnippet.txt");
}

This is possibly my favorite feature of flash 10. Save any kind of file to the users computer…

The first argument is for the data to put in the file, this can be a String, ByteArray or XML object. The second argument is the name of the file.

As a test I also created one that saved a BitmapData object as a jpeg. But it doesn’t really fall into the category of snippet because it uses the Jpeg Encoder from adobe.

Read snippet: FileReference.save()

XOR Conditional

addEventListener(Event.ENTER_FRAME, onLoop);

function onLoop(evt:Event):void {
	var boolA:Boolean = false;
	var boolB:Boolean = false;
	
	if (mouseX > 200){
		boolA = true;
	}else{
		boolA = false;
	}
	
	if (mouseY < 200){
		boolB = true
	}else{
		boolB = false;
	}
	
	if (boolA || boolB){
		trace("regular or");
	}
	
// this is the XOR conditional
	if (int(boolA) ^ int(boolB)){
		trace("exclusive or");
	}
	
}

There’s an obvious more logical ways to do this. But I thought it was somewhat interesting to see….

So in binary XOR will give us:
0^0 = 0;
0^1 = 1;
1^0 = 1;
1^1 = 0;

What other operator might you use to get the same results?… that’s right !=

With regular OR we get:
0|0 = 0;
0|1 = 1;
1|0 = 1;
1|1 = 1;

Read snippet: XOR Conditional

Unique ID

trace((new Date()).getTime() + Math.random()*0xFFFFFF);

A hacky way to get a unique ID, although this isn’t, perfect it works well enough. For a project that you really expect to get huge amounts of traffic… you might consider using php’s uniqid() function and passing it into your swf. That said, the odds of two users getting the same two ID values are about as good as the odds of you winning the lottery twice in a row.

Here is a version wrapped in a function, with a prefix argument:

trace(uniqid("z"));

function uniqid(prefix:String):String{
return prefix + (new Date()).getTime() + Math.random()*0xFFFFFF;
}
Read snippet: Unique ID

XML Node Parents

function numParents(e:XML):int {
	var num:int = 0;
	while (e.parent()!= null) {
		num++;
		e = e.parent();
	}
	return num;
}

Sometimes when reading through XML it’s helpful to know how many parents a given node has. This function will do that. I recently used this function in an accordion widget.

Read snippet: XML Node Parents

Web Cam

var cam:Camera =  Camera.getCamera();
var video:Video = new Video(320, 240);
video.attachCamera(cam);
addChild(video);

Adds the feed from your webcam to the stage.

Read snippet: Web Cam

BitmapData.setVector()

var canvas:BitmapData=new BitmapData(255,255,false,0x000000);
addChild(new Bitmap(canvas, "auto", true));

var size:int = canvas.width * canvas.height;

var pixels:Vector.<uint> = new Vector.<uint>(size);

for (var i:int  = 0; i<size; i++) {
	var ox:uint= i % canvas.width;
	var oy:uint= i / canvas.width;
	pixels[i] = oy << 16 | ox;
}
	
canvas.setVector(canvas.rect, pixels);

setVector() is used to set a group of pixels. I’ve been wanting this feature for a long time. I’m assuming that it’s at least a little faster than using a bunch of setPixel() calls, but i haven’t tested it. Of course, Pixel Bender will be much faster than this…. as long as your not on a powerPC based mac (or some other older computer) … as you may or may not know Pixel Bender will run very slow on a powerPC mac:

Quote from Tinic Uro – Flash Player Engineer

But… I have more news you might not like. 😉 If you ever run a Pixel Bender filter on PowerPC based Mac you will see that it runs about 10 times slower than on an Intel based Mac. For this release we only had time to implement a JIT code engine for Intel based CPUs. On a PowerPC Mac Pixel Bender kernels will run in interpreted mode. I leave it up to you to make a judgment of how this will affect you. All I can say: Be careful when deploying content using Pixel Bender filters, know your viewers.

Read the rest of the article here.

Read snippet: BitmapData.setVector()