29 March 2010

SearchArray AS3 functions class renamed and updated

Three months ago, back in January I have added one more function to popular my SearchArray AS3 Class. Now, I'm introducing three more functions to it but since functions are not search related now this Class have more general purpose, so I needed to rename it to ArraysUtil Class.

New functions are:

- public static function unique(arr:Array):Array
This function returns new Array with elements of given arr Array appearing just once. If arr = [1, 3, 5, 3, 5], new Array will be [1, 3, 5].

- public static function exclude(elem:Object, arr:Array):Array
This function returns new Array from elements of old arr Array without element elem.

- public static function excludeAll(ar1:Array, ar2:Array):Array
This function works like previous exclude function except it removes multiple elements from old Array

Here is class code with example usage:


package com.blogspot.flanture.arrays
{

// ArraysUtil AS3 functions
// author: http://flanture.blogspot.com
// license: Creative Commons Attribution-Share Alike 3.0
// http://creativecommons.org/licenses/by-sa/3.0/
// contact: flanture@gmail.com
// March 2010

import flash.display.Sprite;

public class ArraysUtil extends Sprite
{

public function ArraysUtil()
{

// example usage

var a:Array = [1,2,3,5,3];
var b:Array = [2,4,6,8];
var c:Array = [1,2,3,5,3];
var d:Array = [5,7,9,5,5,7,5,7,9];
var e:Array = [5,5,9];
var f:Array = [5,7,9,5,5,7,5,7,9];
trace("array a: "+a);
trace("array b: "+b);
trace("array c: "+c);
trace("array d: "+d);
trace("array e: "+e);
trace("array f: "+f);
trace("* * * * *");
trace("is there element 6 inside array b?");
search(6, b);
trace("a combined with b: "+combine(a,b));
trace("6 exists in b? "+searchB(6,b));
trace("number of 3 in a? "+searchCount(3,a));
trace("intersection of a and b: "+iSection(a,b));
trace("shuffled b: "+shuffle(b));
trace("exclude element 3 from array c : "+exclude(3,c));
trace("exclude all array e elements from array d :"+excludeAll(e, d));
trace("unique of f :"+unique(f));
}

// function search finds element in array and displays result

public static function search(word:Object, arr:Array):void
{
var exists:Boolean = false;
for(var i:uint=0; i < arr.length; i++)
{
if(arr[i]==word)
{
trace("Element exist on position "+i);
exists = true;
}
}
if(!(exists))
{
trace("Element doesn't exist in array.");
}
}

// function searchB finds element in array and returns Boolean value

public static function searchB(word:Object, arr:Array):Boolean
{
var exists:Boolean = false;
for(var i:uint=0; i < arr.length; i++)
{
if(arr[i]==word)
{
exists = true;
}
}
return exists;
}

// function searchCount returns number of apperances of element in array

public static function searchCount(word:Object, arr:Array):uint
{
var counter:uint = 0;
for(var i:uint=0; i < arr.length; i++)
{
if(arr[i]==word){
counter+=1;
}
}
return counter;
}

// function iSection returns intersection array of two arrays

public static function iSection(arr1:Array, arr2:Array):Array
{
var arr3:Array = new Array();
var count:uint = 0;
for(var i:uint=0; i < arr1.length; i++)
{
for(var j:uint=0; j < arr2.length; j++)
{
if(arr1[i]==arr2[j])
{
arr3[count] = arr1[i];
count+=1;
}
}
}
return arr3;
}

// function shuffle simply shuffles given array elements

public static function shuffle(b:Array):Array
{
var temp:Array = new Array();
var templen:uint;
var take:uint;
while (b.length > 0)
{
take = Math.floor(Math.random()*b.length);
templen = temp.push(b[take]);
b.splice(take,1);
}
return temp;
}

// function combine returns union of two arrays

public static function combine(ar1:Array, ar2:Array):Array
{
var rAr:Array = new Array();
var i:uint = 0;
var j:uint = 0;

while((i < ar1.length) || (j < ar2.length))
{
if(i < ar1.length)
{
rAr.push(ar1[i]);
i+=1;
}
if(j < ar2.length)
{
rAr.push(ar2[j]);
j+=1;
}
}
return rAr;
}

// function exclude remove all appearances of element inside given array

public static function exclude(elem:Object, arr:Array):Array
{
var temp:Array = new Array();

for(var i:uint = 0; i < arr.length; i++)
{
if(!(arr[i]==elem))
{
temp.push(arr[i]);
}
}
return temp;
}

// function excludeAll remove all appearances of ar1 elements inside ar2 array

public static function excludeAll(ar1:Array, ar2:Array):Array
{
var temp:Array = new Array();
ar1 = unique(ar1);

while(ar2.length > 0)
{
if(searchCount(ar2[0], ar1) == 0)
{
temp.push(ar2[0]);
}
ar2.splice(0, 1);
}
return temp;
}

public static function unique(arr:Array):Array
{
var res:Array = new Array();

while(arr.length > 0)
{
if(searchCount(arr[0], arr) == 1)
{
res.push(arr[0]);
}
arr.splice(0,1);
}
return res;
}
}
}



Class has 9 functions right now, I'll add some more later. If you have any questions about the code don't hesitate to ask.

*_*

24 March 2010

Adobe Creative Suite CS5 Launch Invitation

CS5 Adobe Creative Suite is coming out in 18 days. We are all invited to exclusive global launch on Monday, April 12, 2010 via Adobe's website, where right now you can watch some sneak peek videos.

Some of them are : Introduction to Flash Catalyst, sneak peek at the new Adobe Mercury playback engine technology, building powerful online stores, Adobe Story sneak peek and more.



*_*

18 March 2010

Energy Bar Flash Game AS3 Tutorial

In some Flash games author often has a need to display energy bar for hero character or enemy character or both. In this tutorial you will learn how to create reusable energy bar which you can ..., well reuse, in all your future projects. Code is pure ActionScript 3.0. I'm trying to develop this habit to write small chunks of useful code for my future games :)



15 March 2010

Flash Widgets Promotion Model

As you probably already know, besides my personal Flash Platform Adventures represented on this blog, which includes recent AS3 learning experiments and gamedev efforts, one of my projects is also Free Flash Clocks blog. Flash2nd blog already hosts 58 free clocks. All those clocks are available for download, so anyone can use it on their own website or blog as Flash widget.

There are several good ways for widgets promotion, besides your own blog(s). First obvious one is social networking websites. Twitter is good choice if you have established account with few thousands of real followers in related field. Another good choice is Facebook where you can create new related group, join existing one or even better, you can create fan page. However, you will need to spend some time on promoting and marketing.

There are solutions where you can achieve good results very quickly and with no investment at all, Flash widgets directories like Widgetbox and Widgipedia. Take a look at Flash2nd's widgets page on Widgipedia. Currently there are 7 clocks uploaded. Here are current stats with number of views and installs for every clock:

flash widgets stats image
This result is very good since it didn't required any extra work. Uploading is simple and it took only few seconds and because those directories have lots of daily visitors entire promotion work is done for you. Like in blogging if you have unique kind of widgets in niche category you might grab even more attention.

Some developers sell their widgets for a price, but this model can help them too. Why not consider lighter widget version with reduced functionality and widgets directories promotion? If customer finds light version useful there is good possibility he will be ready to pay for full version.

*_*

12 March 2010

Eco Planet Flash Animation

Just finished eco planet animation. File is resizable, published size is 6kb and uses AS3 to move things around.

Animation is 100% vector file. It is suitable for green technologies websites and blogs, presentations and it can be used for educational purposes. You can easily change rotation speed, add new elements or remove existing ones. Entire animation is single self-contained MovieClip, just drag and drop from Library to the Stage.


Different rotation speed for static elements windmills, houses and trees; clouds are a little bit faster and they move in opposite direction from static elements; bikers have their own speed.

keywords: ecology, eco, green, world, globe, animation, earth, house, building, windmill, wind, power, bicycle, planet, Earth

*_*

06 March 2010

Using Deco Tool in Flash CS4 (Vine Fill)

One of the exciting new features in Flash CS4 is Deco Tool (shortcut U). This is actually 3 tools in one, because if you check properties panel while deco tool is selected you will notice 3 different drawing effects: Vine Fill, Grid Fill and Symmetry Brush. Now we have nice set of tools to create best patterns out there.

Default settings for Vine Fill tested on empty Stage can give you pretty good example how this tool works. When you click on the Stage, pattern expands from mouse position to every direction where there are no boundaries of other objects. This means you can limit where your pattern should expand. However expanding is not perfect with default settings and you will often find some space left blank (see image below).


You can reduce unwanted empty space using advanced options located on properties panel. If you change pattern scale to 50%, you will probably fill entire given shape. You can also change segment length, but be careful not to over-do-it or you will lose segment integrity and you may not see flowers, only leafs.


Another parameter available in advanced options is branch angle if you want to rotate your pattern. It seems this doesn't change the way pattern is expanding, something I expected. Last parameter in advance options gives you a way to animate pattern, thus to record pattern expanding! Nice, but most interesting option for Vine Fill is to edit Leaf and Flower shapes.


Here is one trick - when using Vine Fill deco tool for branches choose same color as background and you will see only leafs and flowers.

For creating static patterns, use graphics shapes and you can achieve amazing things, but you are not limited only by graphics. Just as easy you can create animated MovieClip and use it as both custom Leaf or Flower inside your pattern.

*_*

 

template by blogger templates