ilteris kaplan blog

Archive of blog posts since 2005

April 5, 2008

Wiki

Flash OOP

#wiki

Flash OOP

ClassPath

Flash needs to know where to look for classes when it compiles the .swf. By default Flash looks in Classes folder of the flash installation aswell as the same directory as the .fla file for the class files. You can set a custom classpath for each .fla as well as a custom global classpath.

There is two places you can set those. One is file>publish settings>actionscript settings>then you can find classpath tab there. when you set this your fla is going to look for classes in that folder for that fla.

For global classpath you can go to preferences, actionscript pane and then you can just add another fodler to it. You can change the order of the folders that are already there.

book.xml one root one named library and library node has four nested nodes called book and each book node has title attribute and autohor attribute and conains one nested text node an d that text node contains description of the book.

Book.xml


<library>
 <book title = "Title" author="Author">Description</book>

</library>



// Book.as

class Book {
 private var _sTitle:String;
 private var _sAuthor:String;
 private var _sDescription:String;

public function get title():String {
return _sTitle;
}

public function get author():String {
 return _sAuthor;
}

public function get description():String {
 return _sDescription;
}

public function set dataProvider(xnBook:XMLNode):Void {
 _sTitle = xnBook.attributes.title;
 _sAuthor = xnBook.attributes.author;
  _sDescription = xnBook.attributes.description;
}
function Book(sTitle:String, sAuthor:String, sDescription:String) {
  _sTitle = sTitle;
  _sAuthor = sAuthor;
  _sDescription = sDescription;
}

public function toString:String {
 var sOutput:String = _sTitle + newline + _sAuthor + newline + _sDescription;
  return sOutput;
}
}

flash ide






var xmlLibrary:XML = new XML();
var kutuphane:Library = new Library();
var cssStyles:TextField.StyleSheet = new TextField.StyleSheet();
var nSelectedID:Number; // store of the id of the seleccted title
var clItems:CheckOutList = new CheckOutList();




function initializeTextFields():Void {
 TextUtilities.createHTMLText(this, "tDescription", 0, 185, 400, 0, true, true, false, cssStyles);
 this.createEmptyMovieClip("mcAddLink", this.getNextHighestDepth());
 TextUtilities.createHTMLText(mcAddLink, "tAddLink", 0,0,0,0,false,false,true, cssStyles);
  mcAddLink.tAddLink.htmlText = "<span class='link'>Add to CheckOut List <span>";
  mcAddLink._x = 45;
  mcAddLink._y  = 150;

 mcAddLink.onPress = function():Void {

   checkOut();
};
this.createEmptyMovieClip("mcList", this.getNextHighestDepth());
mcList._x = 485;
mcList._y = 200;


}


function initializeCSS():Void {
 cssStyles.onLoad = function(bSuccess:Boolean):Void {
   initializeTextFields(); 
  initializeXML(); // initial XML should be called after CSS is loaded and parsed. 
// so we can pass references. 
} 
cssStyles.load("styles.css" );
}


function initializeXML():Void {
   xmlLibrary.ignoreWhite = true;
   xmlLibrary.onLoad = function(bSuccess:Boolean):Void {
        kutuphane.dataProvider = this.firstChild;
        populateLibraryDisplay();
         // trace(kutuphane.collection);   
    
/* 
         var bkItem:Book;
       var xnBook:XMLNode;
       for(var i:Number = 0; i < this.firstChild.childNodes.length; i++) { 
             

          // this.firstChild.childNodes is book node.
             xnBook = this.firstChild.childNodes[i];
              bkItem = new Book();
              bkItem.dataprovider = xnBook;
               // xnBook.firstChild.nodeValue is textnode of the description.
                    trace(bkItem.title);
*/


function populateLibraryDisplay():Void {
 var nY:Number = 50; // determine the y coordinate of the mc.
 var mcBook:MovieClip; // in order to reference the mc weare going to create dynamically.
for (var i:Number = 0; i < kutuphane.collection.length; i++) {
  mcBook = this.createEmptyMovieClip("mcBook" + i, this.getNextHighestDepth());
  mcBook._y = nY;
 var tBook:Textfield;
 tBook =  TextUtilies.createHTMLText(mcBook, "tBook", 0,0, 0,0, false, false,true, cssStyles);
 tBook.htmlText = "<span class='list'>" + kutuphane.collection[i].title + "</span>";
nY += mcBook._height;
mcBook.onPress = function():Void {
  var nID:Number = Number(this._name.substr(6)); // extract the numeric portion initially we created with i.
 selectBook(nID);
};
}
}

function populateListDisplay():Void {
   for (var sItem:String in mcList) {
       if(mcList[sItem] instanceof MovieClip) {
             mcList[sItem].removeMovieClip();  
       } 
   }
  var nY:Number = 0;
  var mcBook:MovieClip;
  var tBook:TextField;
   for (var i:Number = 0; i<clItems.collection.length; i++) {
       mcBook = mcList.createEmptyMovieClip("mcBook" + i, mcList.getNextHighestDepth());
       tBook = TextUtilities.createHTMLText(mcBook, "tBook", 0,0,0,0,false, false, true, cssStyles);
       tBook.htmlText = "<span class='list'>" + clItems.collection[i].title + " </span>";
       mcBook._y = nY;
       nY += mcBook._height;
       mcBook.onPress = function():Void {
          var nID:Number = Number(this._name.substr(6));
           checkIn(nID); 
}

}



function selectBook(nID:Number):Void {
 nSelectedID = nID;
 tDescription.htmlText = "<spanclass = 'description'>" + kutuphane.collection[nID].toString() + "</span>"
 var tBook:TextField;
  for(var i:Number = 0; i < kutuphane.collection.length; i++) {
    tBook = this["mcbook" + i].tBook;
    if(i == nID) {
          TextUtilities.changeStyleClass(tBook, "listHighlighted");
} else {
   TextUtilies.changeStyleClass(tBook, "list"); 
    }
  }  
}


function checkOut():Void {
  var bCheckOut:Boolean = kutuphane.checkOut(nSelectedID);
  if(bCheckOut) {
  clItems.addItem(kutuphane.collection[nSelectedID]);
  trace(clItems.collection); // returns an array of book instances.
  populateListDisplay();
 }
}

function checkIn(nID:Number):Void {
  var nLibraryID:Number = kutuphane.findBookID(clItems.collection[nID]);
   kutuphane.checkIn(nLibraryID);
   clItems.removeItemAt(nID);
   populateListDisplay();
}

include a dynamic textfield and embed the characters. you don’t have to include any text in the textfield, make sure the font is set to verdana and then click the embed in the proprties window and then embed and click ok. now you have the embed font.

Getters/Setters

Getters and Setters allow you to get and set properties. Using getter/setters rather than public properties are preferential for several reasons. You can better implement good encapsulation if you control if, when, and how properties are affected. If you control the access to the properties, in that method you can do complex logic and calcuations when getters and settters calld. You can create read-only properties.

explicit getter:


// standart method
public function getSomeProperty():DataType {
 return someProperty;
}

public function setSomeProperty(parameter:DataType) {
someProperty = parameter;
}

instance.getSomeProperty();

Implicit getters and setters allow you to reference methods like properties. Not necessarily have advantages over explicit getters and setters.

Getters and Setters must be declared as public getters must acccept a value, setters only take one parameter.


public function get abcd():DataType {
return someProperty;
}

public function set abcd(SParameter:String):void {
someProperty = sPArameter;
}

trace(instance.abcd); // getters
instance.abcd = "some value"; // setters

Coding conventions are important as always. Find your own coding style best. -property declarations beginning of your class -constructor -public methods -private methods -getters and setters are between property declarationss and constructor. Library.as


class Library {
 private var _aBooks:Array;
 private _aBooksCheckedOut:Array;
public function set dataProvider(xnLibrary:XMLNode):Void {
  var bkItem:Book; 
   
for (var i:Number = 0; i < xnLibrary.childNodes.length; i++) {
   bkItem = new Book();
   bkItem.dataProvider = xnLibrary.childNodes[i];   
_aBooks.push(bkItem);
  }
}

public function get collection():Array {
  return _aBooks;
}



function Library() {
  _aBooksCheckedOut = new Array();
  _aBooks = new Array();
}

public function checkOut(nID:Number):Boolean {
   if(_aBooksCheckedOut[nID] || nID == undefined) {
         return false;
} 
  _aBooksCheckedOut[nID] = true;
   return true;
}

public function checkIn(nID:Number):Void {
  _aBooksCheckedOut[nID] = false;
}


public function findBookID(bkItem:Book):Number {
    for (var i :Number = 0; i < _aBooks.length; i++) {
       if(_aBooks[i] == bkItem) {
           return i;
        }
   } 
}
}

Static Methods in our Class. Text Utilities.as


class TextUtilities {
 public static function createHTMLText(mcParent:MovieClip, sInstanceName:String, nX:Number, nY:Number, nWidth:Number, nHeight:Number, bMultiLine:Boolean, bWWrap:Boolean, bEmbedFonts:Boolean, cssStyles:TextField.StyleSheet):TextField {
  mcParent.createTextField(sInstanceName, mcParent.getNextHighestDepth(),nX,nY, nWidth, nHeight);
 var tField:TextField = mcParent[sInstanceName];
tField.html = true;
tField.selectable = false;
tField.autoSize = "left";
tField.multiline = bMultiline;
tField.wordWrap = bWordWrap;
tField.embedFonts = bEmbedFonts;
tField.styleSheet = cssStyles;

return tField;

}

public static function changeStyleClass(tField:TextField, sClassName:String):Void {
  var sText:String = tField.htmlText;
  var nStartIndex:Number = sText.indexOf("class='");
  var nEndIndex:Number = sText.indexOf("'", nStartIndex+7);
  var sPreceding:String = sText.substring(0,nStartIndex + 7); // should be giving us class = ' 
  var sFollowing:String = sText.substring(nEndIndex); 
  tField.htmlText = sPreceding + sClassName + sFollowing;

}
}

We have a css file in our folder and if we open it; styles.css


.list { 
font-family: Verdana;
font-size: 17;
color: #FFFFFF;
}
.listHighlighted {

font-family: Verdana;
font-size:17;
color:#9EACB9;
}
.description {
font-family: Verdana;
color: #FFFFFF;
font-size :10;
}
.link {
font-family: Verdana;
color: #FFFFFF;
font-size : 14;
}


class CheckOutList {
  private var _aBooks:Array;
     
  public function get collection():Array {
     return _aBooks;
}

  function CheckOutList() {
    _aBooks = new Array();

  }
  
  public function addItem(bkItem:Book):Void {
    _aBooks.push(bkItem);

}

 public function removeItemAt(nIndex:Number):Void {
   _aBooks.splice(nIndex, 1);
  
 }

}

Continue Reading

Back to Archive