24 June 2010

Simple AS3 Data Structure

In ActionScript 3 Arrays play very important role. This is powerfull class for storing and manipulating different type of values. If I have only one type of data, for example, only Strings, I will use Vector class which is almost exaclty like Array class, but it can store one data type only. Simple Array with different data types would be created like this:



var student = new Array();
student[0] = "John";
student[1] = 22;
student[2] = "Elle";
student[3] = 21;



Here I have strings representing names of students and numbers representing their age. I can trace all info at once with a loop:



for (var i:uint = 0; i<student.length; i++) {
trace("info : "+student[i]);
}



Since my collection of data have clear "units" where single unit holds data for single student I need to structure data differently so I can take advantage of language for easier data manipulation. In this particular case indexed array is fine, but in some other cases it will be better and more efficient to use associative arrays or not use arrays at all.

For this example I'm using Objects to store data about students, one Object per student. Simplified example would look something like this:


var st1 = new Object();
st1.name = "John";
st1.age = 22;
st1.grades = [5, 4, 4, 3, 5, 3];


Now I have my data structured like image shows.



When data is structured like this it's easy to trace info and manipulate data. This example finds average grade for every student in students Array and outputs info for every student.


var students = new Array();

var st1 = new Object();
st1.name = "John";
st1.age = 22;
st1.grades = [5, 4, 4, 3, 5, 3];
students.push(st1);

var st2 = new Object();
st2.name = "Ella";
st2.age = 20;
st2.grades = [4, 5, 5, 4, 5];
students.push(st2);

for(var i:uint = 0; i < students.length; i++) {
trace((i+1)+".");
trace("NAME : "+students[i]["name"]);
trace("AGE : "+students[i]["age"]);
trace("grades : "+students[i]["grades"]);
var temp:Number = getAverage(students[i]);
trace("average grade :: "+temp);
trace("");
}

function getAverage(o:Object):Number
{
var sum:Number = 0;
for (var j:uint = 0; j < o.grades.length; j++) {
sum = sum + o.grades[j];
}
return sum / o.grades.length;
}



You can use Array class methods to add new students // students.push(st3) // or remove them from array with splice. To remove John use // students.splice(0,1);

*_*

15 June 2010

Free FFD magazine June issue


Since Flash & Flex Developer's Magazine started with monthly editions I can't read the whole issue and another one is already out! I'm not complaining, quite the opposite. If you're not familiar with it, I recommend you take a look at only FREE Flash related magazine. Here is short content of June issue.

  • Lee Graham writes about Flash Player 10.1 on Android and AIR for Android.
  • Louis DiCarro has Flash and the City 2010 review, three days presentations event held in New York from 13th until 16th May.
  • Huw Collingbourne writes about the Flash Platform in Visual Studio. The Amethyst IDE lets you edit, design and debug Flash, Flex and AIR applications ...
  • You can also read interesting story about validating your skills and best practices with the Adobe Certified Associate Exam by Sinead Hogan.
  • In ActionScript Development section find out how to use Flash Builder 4 to code Flash CS5 projects, another one by Louis DiCarro.
  • In Flex Development awaits part II of XML Photo Gallery Tutorial written by Ryan D'agostino.
This is not entire content! Visit FFmag and download latest June issue.

*_*

01 June 2010

How to use transparent Flash widget

One interesting aspect of using Flash widgets on web pages is transparency. In some situations it's good to leave background behind transparent Flash widget as it is to get floating, natural effect. Widget will look almost as part of your template except it can react to mouse movements and/or include some interesting animation.

You can also take advantage of transparent Flash widget by including background image right into your HTML tag which embeds Flash file. Take a look at recently published Office Transparent Flash Clock at my Flash2nd blog. Here are some suggestions how you can use this widget.



In this way you are no longer passive user, you participate and improve existing Flash widget by giving it personal, customized touch.

images by: source 1, source 2.

*_*

 

template by blogger templates