Posts Tagged ‘source’

Dynamic Sound – Part 2

Wednesday, May 6th, 2009

Last post I started with the basic of dynamic sound in flash. Now we will do something more pratical with what we saw. We will create a piano that play notes by clicking or pressing keys.

I will not spend time creating the piano, so you can download it here.

This movie requires Flash Player 9

OK, starting here, we need to create a sound instance, a soundChannel instance, add the listeners and play the sound.

private var sound:Sound;
private var channel:SoundChannel;
 
public function Main():void{
	init();
 
	sound = new Sound();
	sound.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
 
	channel = sound.play();
}
private function onSampleData(e:SampleDataEvent):void{
}

(more…)

CustomCursor advanced

Wednesday, April 15th, 2009

OK, let’s begin talking about Design Patterns.

The best thing of OOP, is the capability to create something in way to reuse it in another situation by defining some patterns to follow. Basicly this is Design Patterns. There are several kinds of Patterns and one of them is the Singleton Pattern. It consists in create only one instance of a class and control it by global functions and properties. Unless you are using a multi-touch device, a good example of usage is a CustomCursor class. There is no need to create more than one instance of a custom cursor, because you can point to only one thing at time. However you can change your cursor behavior from anywhere in your application.

The Basics

Let’s create our Singleton class

package {
	import flash.display.Sprite;
 
	public class CustomCursor extends Sprite {
 
		private static var _instance:CustomCursor;
		public function CustomCursor(singleton:SingletonEnforcer):void{
		}
		public static function getInstance():CustomCursor{
			if(CustomCursor._instance == null){
				CustomCursor._instance = new CustomCursor(new SingletonEnforcer());
			}
 
			return CustomCursor._instance;
		}
	}
}
class SingletonEnforcer {
	public function SingletonEnforcer():void{
	}
}

(more…)

Stage.quality vs performance

Friday, April 3rd, 2009

One of the worst problem that any developer have to face is the performance of his applications. There are many factor contributing to a bad performance, one of them is graphic rendering.

Depending of what you’re doing, there’s a single line of code that solves this problem.

stage.quality = StageQuality.LOW

When you set the stage quality as low, you tell flash to render all vectors without anti-alias. This increases the application performance a lot. However this can makes your application looks like a crap depending of how you built it. There is a few things you can do to balance performance and visual quality.

The first thing you can do is set your text alias as ADVANCED or if it’s a static text Anti-alias for readability.

This movie requires Flash Player 9

Another tip is use bitmaps instead of vectors. Of course you must choose wisely whose graphics need to have a good quality and the size of them. Also, you can change you image quality at the properties panel to get a smaller file. Although you need to be carefull with this, because your bitmaps wont smooth if you rotate or scale them.

I didn’t research why exactly bitmaps don’t mess up when the stage quality is set to low, but I think it is because flash don’t need to calculate what color every pixel when they are displayed, all colors are already defined by the bitmap data.

With this thought, I created a high quality container class, called HQContainer, that renders all children at the quality you want (low, medium, high, best).

I have a 1000 boxes rotating. 990 of them are blue and have .05 of opacity. 10 of them are red and have a MouseEvent listener added.


(more…)

Matrix3D 101

Thursday, April 2nd, 2009

3D engines are very present in web applications these days. With ActionScript3 become possible develop faster 3d lib and, with that, the technology has spread widely thru the web. Today we have a lot of elaborated 3d engines like Papervision3D, Away3D, Sandy3D, Five3D, Alternativa and many others.

Their usage is quite simple, once that all calculations behind any lib is automatic e independent of the knowledge of the developer. But I think that is extremely important understand what happen in any application, lib or class, for the knowledge or to improve something that has already been created.

Despite the way that is built, any 3d engine have some elements in common, one of them is what we call Transformation Matrix. Yes, matrices. That thing that we learn on school and we don’t have any ideia of what it is until the day that we became developers. But before understand what is a transformation matrix, we must understand what it does. And for this we’ll think in a 3d engine in a basic way.

For example, let’s take a cube as a group of 8 connected points:

cubo
(more…)

Papervision3D – Max3DS Parser bug

Friday, March 27th, 2009

Since papervision added the Max3DS parser I’ve been working with it instead of DAE. Although there’s a bug that I noticed when I imported an asymmetric model to my scene. It appear to be mirrored at the X axis. I was in a rush so I took the easy way, and flipped my model before export the 3ds file.

After that I tried to chage the 3ds parser to import the file correctly but it was taking me much more time than I had, so I wrote this method to fix the mesh after parsing.

Here is my scene in 3D Max

objects

Note that the box is behind the teapot and the cylinder is in front of it.

Here is a picture of the PPV scene when I import them usign Max3DS parser.

objects_fliped

And here it is after running my script

objects_normalized

I didn’t test it for complex models, but this will work just fine in 90% of the cases.

Here is the script:

normalize(object3ds);
 
function normalize(max3ds:Max3DS):void{
	max3ds.rotationX = 0;
	max3ds.rotationY = 0;
	max3ds.rotationZ = 0;
 
	for each(var mesh:DisplayObject3D in max3ds.children){
		mesh.scaleZ *= -1;
		mesh.rotationX *= -1;
		mesh.rotationY *= -1;
		mesh.rotationZ *= -1;
 
		var m:Matrix3D =  new Matrix3D();
		m.calculateMultiply(m, Matrix3D.rotationX(mesh.rotationX*Math.PI/180));
		m.calculateMultiply(m, Matrix3D.rotationY(mesh.rotationY*Math.PI/180));
		m.calculateMultiply(m, Matrix3D.rotationZ(mesh.rotationZ*Math.PI/180));
 
		m.calculateMultiply(m, Matrix3D.scaleMatrix(mesh.scaleX, mesh.scaleY, mesh.scaleZ));
 
		for(var i:Number = 0; i<mesh.geometry.vertices.length; i++){
			var vertex:* = mesh.geometry.vertices[i];
 
			var vx :Number = vertex.x;
			var vy :Number = vertex.y;
			var vz :Number = vertex.z;
 
			var tx :Number = vx * m.n11 + vy * m.n12 + vz * m.n13 + m.n14;
			var ty :Number = vx * m.n21 + vy * m.n22 + vz * m.n23 + m.n24;
			var tz :Number = vx * m.n31 + vy * m.n32 + vz * m.n33 + m.n34;
 
			vertex.x = tx;
			vertex.y = ty;
			vertex.z = tz;
		}
 
 
		mesh.rotationX = 0;
		mesh.rotationY = 0;
		mesh.rotationZ = 0;
		mesh.scaleZ *= -1;
 
		mesh.geometry.flipFaces();
	}
}

Demo
Source

PaperPlane Test

Tuesday, March 24th, 2009

Early experiments of nothing that could become something.

Use the arrows to control the plane
Speed up pressing space

Demo

plane

Source