01 July 2010

Simple AS3 Data Structure - part II



Student data structure from previous post doesn't have to be created like I have shown earlier. There is one more OOP way of doing it, to create a class. Private variables will hold particular information and we can use getters and setters to assign and retrieve values.

Here is the code for Student.as class:



package
{
public class Student
{
private var _name:String;
private var _age:uint;
private var _grades:Array;

public function Student (sName:String):void
{
_name = sName;
}

public function set name (studentName:String):void
{
_name = studentName;
}

public function get name ():String
{
return _name;
}
public function set age (studentAge:uint):void
{
_age = studentAge;
}

public function get age ():uint
{
return _age;
}

public function set grades (studentGrades:Array):void
{
_grades = studentGrades;
}

public function get grades ():Array
{
return _grades;
}

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



As you may noticed there is built-in getAverage function to make things easier. Now, you can test this class functionallity by creating TestStudent.fla in same directory where you saved Student.as file. In first frame of the TestStudent file enter next code:



import Student;

var student1:Student = new Student("John");
student1.age = 22;
student1.grades = [4, 5, 5, 3];

var student2:Student = new Student("Ella");
student2.age = 20;
student2.grades = [4, 5, 5, 4, 5];

var average:Number = student1.getAverage();
trace(average);

average = student2.getAverage();
trace(average);



Which method you will use depends on your needs and style.

*_*

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.

*_*

22 May 2010

Using BitmapSplit function with Tweener



Few posts ago I introduced Dynamic Bitmap Split Function. This function allows you to split any Bitmap into any (reasonable) number of column and rows and use those parts separately.

Here is one quick example of using this function together with Tweener class. Click on image to see live example.



and here is code:

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.utils.ByteArray;
import flash.geom.Rectangle;
import flash.events.Event;
import caurina.transitions.*;

var bmd1:BitmapData = new BitmapData(100, 100, false, 0x666666);
var bm1:Bitmap = new Bitmap(bmd1);
bm1.x = 120;
bm1.y = 100;
addChild(bm1);
var expand:Boolean = false;
var bitmapsArray:Array = new Array();

addEventListener(Event.ENTER_FRAME, onFrame);

function onFrame(evt:Event):void
{
if(expand)
{
bm1.alpha = 0;
for(var k:uint = 0; k < bitmapsArray.length; k++)
{
var curr:uint = bitmapsArray[k].length;

for(var l:uint = 0; l < curr; l++)
{
Tweener.addTween(bitmapsArray[k][l],
{x:20*l+10, y:33*k+10, alpha:0.5, time:3, transition:"linear"});
expand = false;
}
}
}
}

function splitBitmap(_source:BitmapData, _columns:int, _rows:int):void
{
var _bitmapWidth:int = _source.width;
var _bitmapHeight:int = _source.height;

var _onePieceWidth:Number = Math.round(_bitmapWidth / _columns);
var _onePieceHeight:Number = Math.round(_bitmapWidth / _rows);

var _copyRect:Rectangle = new Rectangle(0, 0, _onePieceWidth, _onePieceHeight);
for(var i:int = 0; i < _columns; i++)
{
var tempArray:Array = new Array();

for(var j:int = 0; j < _rows; j++)
{
var _piece:String = "piece"+String(i)+String(j);
var temp:* = [_piece];
temp = new BitmapData(_onePieceWidth, _onePieceHeight, true, 0xFF0000CC);

var newBytes:ByteArray = _source.getPixels(_copyRect);
newBytes.position = 0;
temp.setPixels(_copyRect, newBytes);

var _newBitmap:String = "newBitmap"+String(i)+String(j);
var tempBitmap:* = [_newBitmap];
tempBitmap = new Bitmap(temp);

tempBitmap.x = i * (_onePieceWidth) + bm1.x;
tempBitmap.y = j * (_onePieceHeight)+ bm1.y;
addChild(tempBitmap);

tempArray.push(tempBitmap);
}
bitmapsArray.push(tempArray);
}
expand = true;
}

splitBitmap(bmd1, 5, 3);


more examples to come.

*_*

 

template by blogger templates