Archive für Mai 2007

String-matching-algorithm

Need to find specific textsnippets within text? Ever heard of the Boyer-Moore-Algorithm?

At the beginning, the pattern will be written left aligned under the text and then compared from right to left with it. Once a mismatch is found, two heuristics calculate how far to the right the searchpattern can be displaced.

function bm(txt, pattern){

var tlen:Number = txt.length;
var mlen:Number = pattern.length;
var i:int;
var j:int;
var match:Array = new Array(256);

for(i=0; i<256; ++i) match[i] = mlen;
for(i=0; i

for(i=j=mlen-1; j>0; –i, –j)
while(txt.charAt(i) != pattern.charAt(j)) {
if (match[txt.charAt(i)] != undefined) {
i += Math.max(mlen-j,match[txt.charAt(i)]);
} else {
i++;
}
if(i >= tlen) return -1;
j = mlen-1;
}
return “searchstring matches between ” + i + ” ” + (i+mlen);
}

trace(bm(”climb that mountain to cut off the top”, “mo”));

Plasma - nothing more or less.

Plasma-effects may kill the performance of your flashplayer - hope this tiny plasma soup, i scripted last evening won´t freeze yours. I think it´s pretty fast - and did you see all that colors ( just move your mouse ) ?

Launch Plasma-effect.

Or just download the sources.

TextLineMetrics Example.

Just a quick example for using the TextLineMetrics combined with the textArea-component.

import flash.text.TextLineMetrics;
import flash.display.Sprite;
import flash.events.Event;
myTextArea.text = “hello world”;

var rect :Sprite;
var tH :Number;
var lH :Number;
var mL :Number;
var metrics :TextLineMetrics;
var xPos :Number;
var yPos :Number;

rect = new Sprite();
rect.x = rect.y = 0;

rect.graphics.beginFill( 0xFFFF00, 1 );
rect.graphics.drawRect ( 0, 0, 10, 10 );
rect.graphics.endFill();

addChild( rect );

function fieldObserver ( event: Event ) {

tH = myTextArea.textHeight;
lH = myTextArea.getLineMetrics( 0 ).height;
mL = Math.ceil( tH / lH );

metrics = myTextArea.getLineMetrics( mL-1 );

xPos = metrics.width + myTextArea.x;
yPos = tH - ( lH * myTextArea.verticalScrollPosition );

rect.x = xPos;
rect.y = yPos + myTextArea.y;

}

addEventListener( Event.ENTER_FRAME, fieldObserver );

“hey look - it´s a cursor !”

Preloading in Flash CS3 like i did in Flash3 - what a mess…

In Flash CS3 Beta, loading a single swf and displaying loaded bytes changed a lot. My solution ( probably a bad one! ) is to load my main swf like i did in flash3 - shame on me! For example you have 3 Frames. In the first frame i wait and look if all frames have loaded:
percentt.text = Math.round( ( framesLoaded/totalFrames ) *100 );

if ( framesLoaded == totalFrames ) {

//do some

}

…in the second frame i paste all my library-linkage stuff, classes and more. Of course that´s not the way i used to do it before, but i didn´t read ´bout any other true good flash as3-solution yet. Taking part on a blog-comment-discussion show´s - that the many other people haven´t got better ways to do it eather… ( read more )

For any solution or intresting approach - feel free to post me your comments!

PV3D : First steps on character animations…

Last weekend i spent my time playing with papervision3d, xsi collada export and flash cs3 beta. after doing some basics, like rendering and parsing cubes or planes form xsi to flash via collada export, i tried to display a little more complex figures.

Unfortunatly i didn´t manage to parse a complex bone-animation ( xsi, pp3d to flash ) - so, following 3d character mesh is the result to play with. But after all i can say, doing stuff with papervision is great… hope to find some time soon to do more things like that.

3D Cube : The filtered kind of…

Just a quick scripted 3D spinning cube with some filters i made a view month ago…

>> Launch.

Sound Compute Spectrum.

Visualising soundwaves with actionscript3 is pure fun. Inspired by an demo Joa Ebert released on the last flashforum conference in germany, i decided to build my own little sound-visualising demo, called “the brightWhite experience”.
Premium supported on getting some free sounds created by Philipp Weigl, i tought up some nice little effects for that visualiser, that react, interact or just feature the sound. It took me a bit to motivate my old scripting buddy thorsten werner, the guy behind rgblaster, to contribute some of his great as3-experiments too. The end result was a nice little flashdemo for sound-visualisation via compute spectrum.

Want a little snippet?

///////////////////////////////////
// analyse sound spectrum
///////////////////////////////////

getByte = 0;

SoundMixer.computeSpectrum(byteArr, true);

var i:int;
var w:uint = 2;
var num = undefined;
var type:String = undefined;

for (i=0; i<512; i+=w) {

var getByte:Number = byteArr.readFloat();
var n:Number = Math.floor(getByte * 100);

////////////////////////
// catch massiv amp
////////////////////////

if (i > 256 && n > 20) {

type = “massiv”;

}

}

paintTunnelizer(type);

///////////////////////////////////
// paint some effects
///////////////////////////////////

function paintTunnelizer(type:String) {

zIndex1++;
modulo++;

var mc:MovieClip = new MovieClip();
mc.name = “circle__” + String(zIndex1);
mc.x = 200;
mc.y = 200;
stage.addChild(mc);

if (modulo==2) {

modulo=0;

mc.graphics.lineStyle(5,0×000033,.1);
mc.graphics.drawCircle(0,0,50);

} else {

if (type == “massiv”) {

mc.graphics.lineStyle(5,0xffffff,5);
mc.graphics.drawCircle(0,0,50);

} else {

mc.graphics.lineStyle(5,0xffffff,.3);
mc.graphics.drawCircle(0,0,50);

}

}

dot0 = stage.getChildAt(1);

var getDotX:Number = Number(dot0.x);
var getDotY:Number = Number(dot0.y);

mc.x = getDotX;
mc.y = getDotY;

mc.sc = 1;
mc.alpha = 0.1;

for (var i:Number = 3; i < stage.numChildren; i++) {

var p = stage.getChildAt(i);

if (p.name.indexOf("circle__",0) != -1) {

var p1 = stage.getChildAt(i);

if (!tSwitch) {

p1.sc *= 1.2;

} else {

p1.sc *= .9;

}

p1.scaleX = p1.sc;
p1.scaleY = p1.sc;

p1.alpha+=.02;

if (type == "massiv") {

if (!tSwitch) {

tSwitch = true;

} else {

tSwitch = false;

}

}

if (p1.scaleX > 7) {
stage.removeChild(p1);
}
}

}

}

See that snippet in action

Motion Rings

Things look so much better with math:

>> Motion Rings

Behind the scenes:

onEnterFrame = function() {

var p:Number = 51;
while(p>0) {
p–;
obj_array[p].x = 250 + 90 * Math.sin(zIndex + 0.1257 * p);
obj_array[p].y = 100 + 90 * Math.cos(zIndex + 0.1257 * p);
bmp.setPixel32( obj_array[p].x , obj_array[p].y , 0xffffffff );
}

var p:Number = 101;
while(p>51) {
p–;
obj_array[p].y = 100 + 90*Math.cos(0.1257*p)*Math.sin(zIndex);
bmp.setPixel32( obj_array[p].x , obj_array[p].y , 0xffffffff );
}

var p:Number = 151;
while(p>101) {
p–;
obj_array[p].x = 250 + 90*Math.sin(0.1257*p)*Math.sin(zIndex);
bmp.setPixel32( obj_array[p].x , obj_array[p].y , 0xffffffff );

}

zIndex = zIndex - 0.02;
}

Embed Fonts with AS3…

…the Flash CS3 kind of way! It´s simple, i can tell you - after trying for days…

Step1:
Add the font you want to the library. Be sure to name the font and give it a class linkageID-name too.

Step2:
var yourFont = getDefinitionByName( “yourFontLinkageID-name” );
Font.registerFont(yourFavFont);

Step3:
Finally just refer to the font-name when formatting your textfield.

import flash.text.*;

var format:TextFormat= new TextFormat();
format.font= “standard 07_53″;
format.color= 0×000000;
format.size= 20;

var display_txt:TextField = new TextField();
display_txt.embedFonts = true;
display_txt.autoSize= TextFieldAutoSize.LEFT;
display_txt.text = “Hello World!”;
display_txt.setTextFormat(format);
addChild(display_txt);

Got it.

Perlin Juliamenge Fractal

Fun with fractals:

>> Have a look

|