29 March 2009

Windmill Simulation - Free Flash File

Windmill simulation file is good in educational purposes, but also can find its place in any green technologies related project, website or blog.

Simple to use, press '-' control to lower wind speed and '+' sign to make windmill rotate faster. Light bulb is glowing according to amount of energy provided by windmill.


keywords: windmill, wind generator, green, ecology, technology, energy, simulation.


More free Flash files on downloads page

.

24 March 2009

Advanced For Loop Actionscript Example

Let's create two arrays.

var a:Array = [3, 5, 7, 9];
var b:Array = [2, 3, 7, 9, 11];

Next function will check if arrays have equal elements on same places.


function checkArrays(arrayA:Array, arrayB:Array):void {
for(var i=0, j=0; (i<arrayA.length-1) || (j<arrayB.length-1); i++, j++) {
if(arrayA[i]==arrayB[j]){
trace("elements a["+i+"] and b["+j+"] are equal!");
trace("element value is : "+arrayA[i]);
}
}
trace("process finished.");
}

checkArrays(a, b);

same works in as2.0, but instead of returned type void as2.0 prefers Void with capital letter V.

output ::

elements a[2] and b[2] are equal!
element value is : 7
elements a[3] and b[3] are equal!
element value is : 9
process finished.

This for loop will finish in 4 iterations, when first array runs out of elements.

This example is simple to understand and in fact it is little unusual one since it needs single variable to find same elements and not both i and j. We can check arrays in 'reverse' order also, meaning if first element of array a is equal to last element of array b, etc. Here is the code:


function checkArrays2(arrayA:Array, arrayB:Array):void {
for(var i=0, j=arrayB.length-1; (i<arrayA.length-1) || (j>0); i++, j--) {
if(arrayA[i]==arrayB[j]){
trace("elements a["+i+"] and b["+j+"] are equal!");
trace("element value is : "+arrayA[i]);
}
}
trace("process finished.");
}

and output is ::

elements a[2] and b[2] are equal!
element value is : 7
process finished.

In this example, first iteration checks if a[0] and b[4] are equal and nothing happens because 3 is not 11. Second iteration checks a[1] and b[3], third checks a[2] and b[2], this one will produce output and last iteration checks a[3] and b[1]. After this step for loop ends because i becomes equal to arrayA.length-1. This means that element b[0] is never checked because arrays don't have same length.

Point is you can use multiple initial values, complex loop exit condition and multiple iteration steps inside the for loop and do more with less code. I hope you find this example useful.

.

21 March 2009

Twitter Networking Tip


It doesn't matter how many do you follow, but who do you follow

.

13 March 2009

Twitter Stream #1

Some useful handpicked tweets within last few days_}

@visormatt: TweenLite and TweenMax for Actionscript 3 ROCK!!!! http://blog.greensock.com/tweenliteas3/ easy to use and offers an abundance of features

@mikeyq6: #Flex For all u Java -> ActionScript 3 programmers: An interesting way to implement constructor overloading. http://twurl.nl/85wsnl

@cbehrlich: UI Design Patterns: Design Pattern Principles for ActionScript 3.0: Loose Coupling ... http://tinyurl.com/b682nx

@augcolombia: Tech Labs - Working with XML, E4X and ActionScript 3 http://tinyurl.com/ckytg4

@WebBasedApps: MAKE: Blog: Flash bridge for Facebook Connect: Pieter Michels wrote a nice actionscript/javascript bridge that a.. http://twurl.nl/ia52ps

@stasisfield: Primarias - gorgeous ActionScript painting: http://tinyurl.com/bdnlex

@flexughh: ActionScript for Java Developers. Part 1: http://bit.ly/ejkeb Part 2: http://bit.ly/U09Fu

@untereinerL [on ntt.cc] 23 design patterns in ActionScript with examples from Abstract Factory to Visitor http://snurl.com/dopg8

@UltraVisual: Animating text - letter by letter with actionscript - http://tinyurl.com/dxopg4

@springas: Christophe Coenraets on Spring ActionScript: Autowiring - http://tinyurl.com/amuotv

.

12 March 2009

roadMap function example

download roadMap function example chasing balls.

actionscript function example

readme.txt file included, but in case you don't wanna read it: use mouse to distribute green balls around the stage and press SPACE on keyboard after to start blue ball moving along defined roadMap. repeat last action.

%

11 March 2009

Object Movement Functions

Recently I needed some custom movie clips movement functions, but none of those free ones out there didn't fit and I had to write few of them on my own. First function is moveToPoint which uses 4 arguments, obj is Object to move (MovieClip), fx is end x point, fy is end y point and sp (speed) is number of frames to reach the goal. This means function is frame based, not time based.


function moveToPoint(obj:Object, fx:Number, fy:Number, sp:Number) {
var dx = (fx - obj._x) / sp;
var dy = (fy - obj._y) / sp;
var counter = 0;
obj.onEnterFrame = function() {
if(counter < sp) {
obj._x += dx;
obj._y += dy;
counter += 1;
}else{
delete obj.onEnterFrame;
}
}
}

OK, so dx and dy are distances object will need to move in sp frames. Counter is exit loop variable so we know when to delete onEnterFrame function. As you may noticed this is AS2.0 code.

Next function is what I was looking for. I'm using 3 arrays arguments where first array stores x positions of movement steps, second array stores y positions and third array stores speed, number of frames needed to move from one point to another. This has its down sides since if you want constant speed over entire road you have to calculate frame for each part, but this was not something I need, so roadMap function works for me just fine.


function roadMap2(obj:Object, arx:Array, ary:Array, ars:Array) {
var dx = (arx[0] - obj._x) / ars[0];
var dy = (ary[0] - obj._y) / ars[0];
var counter = 0;
obj.onEnterFrame = function() {
if(counter < ars[0]) {
obj._x += dx;
obj._y += dy;
counter += 1;
}else{
delete obj.onEnterFrame;
counter = 0;
if(ars.length > 0) {
// remove first point from all arrays
arrx.splice(0, 1); arry.splice(0, 1); arrs.splice(0, 1);
roadMap2(obj_mc, arrx, arry, arrs);
}else{
delete arrx; delete arry; delete arrs;
obj.unloadMovie(); // this method is optional
}
}
}
}

I'm using exactly the same thing as in moveToPoint function but difference is recursive call after point is reached and deleted from arrays. Requirement here is same length for all 3 arrays. Note also that ars array value of 1 will move desired MovieClip in single frame.

Functions usage example: (obj_mc MovieClip instance is needed on Stage)


var arrx:Array = new Array(0, 200, 300, 400, 550);
var arry:Array = new Array(200, 200, 300, 50, 200);
var arrs:Array = new Array(1, 10, 10, 20, 20);
roadMap2(obj_mc, arrx, arry, arrs);

I thing there is 1000 points limit on recursive calls, so keep that in mind.

Live example coming soon.

.

08 March 2009

My Avatar Editor Project (open source)

Here is one interesting project by Senocular - it is an editor of personalized avatar characters much like Nintendo Wii ones. Howevere, besides online version, author is offering desktop AIR app version to play with. You can export your avatar image to JPG or PNG.



Source code is also available from Google Code, MIT License, (I don't see any ads on websites nether!) and you will need Adobe Flash CS4 or later to use the code. Or later?! :)

.

 

template by blogger templates