Nickbee
10-14-2010, 01:55 PM
I’m just starting to get my head around OOP. In my last few projects I wrote some custom classes where I saw fit. In this example I wrote a class to provide a non-repeating Array.
ArrayShuffle Class:
// com > ArrayShuffle.as
package com{
//imports
import flash.display.Sprite;
//declare ArrayShuffle class
public class ArrayShuffle extends Sprite
{
//vars
//var to hold argument from the constructor
public var _arrayLength:uint;
//temp array to initialy hold the numbers in sequential order
public var _tempArray:Array = new Array();
//array to be populated with the numbers in random order
public var _shuffledArray:Array = new Array();
//constructor method ArrayShuffle
//argument passes the amount of numbers in the final shuffled Array
public function ArrayShuffle(arrayLength:uint)
{
//pass the argument to the class var
_arrayLength = arrayLength;
//set up loop to populate the _tempArray with sequential numbers based on the _arrayLenght var
for (var i:int=0 ; i<_arrayLength ; i++){
//populate the _tempArray with the current value of i
_tempArray.push(i);
}//closes for loop
//set up loop to randomly populate the _shuffledArray
while (_tempArray.length > 0)
{
//declare r which is a random number based on current the length of _tempArray
var r:int = Math.floor(Math.random()*_tempArray.length);
//populate the _suffledArray with the value of _tempArray at the index of r
_shuffledArray.push(_tempArray[r]);
//remove to value of _tempArray at the index of r since it is no longer needed
//this assures that the _shuffledArray will contain non-repeating random numbers
_tempArray.splice(r,1)
}//closes while loop
}//closes ArraySuffle constuctor method
}//closes public class ArraySuffle
}//closes package
Document Class:
//Document Class Test
package {
//flash imports
import flash.display.Sprite;
//custom class imports
import com.ArrayShuffle
//declare Test class
public class Test extends Sprite
{
//vars
//_testArray to populate non-repeating numbers from the ArrayShuffle Class
var _testArray:Array = new Array();
//Test constructor method
public function Test()
{
//new instance of the ArrayShuffle Class
//argument of 10 passed - the amount of non-repeating numbers to create
var _arrayShuffle:ArrayShuffle = new ArrayShuffle(10);
//transfer non-repeating numbers from _arrayShuffle instance to class var
_testArray = _arrayShuffle._shuffledArray;
//trace _testArray
trace(_testArray); //1,5,6,2,8,9,3,4,7,0
}//closes Test constructor method
}//closes Test class
}//closes package
My question… What is the best way to access the Array made by the ArrayShuffle Class? I just used public vars and transferred the data to local vars. But I think this might be a good place to use a getter?
Thanks for any insight…
ArrayShuffle Class:
// com > ArrayShuffle.as
package com{
//imports
import flash.display.Sprite;
//declare ArrayShuffle class
public class ArrayShuffle extends Sprite
{
//vars
//var to hold argument from the constructor
public var _arrayLength:uint;
//temp array to initialy hold the numbers in sequential order
public var _tempArray:Array = new Array();
//array to be populated with the numbers in random order
public var _shuffledArray:Array = new Array();
//constructor method ArrayShuffle
//argument passes the amount of numbers in the final shuffled Array
public function ArrayShuffle(arrayLength:uint)
{
//pass the argument to the class var
_arrayLength = arrayLength;
//set up loop to populate the _tempArray with sequential numbers based on the _arrayLenght var
for (var i:int=0 ; i<_arrayLength ; i++){
//populate the _tempArray with the current value of i
_tempArray.push(i);
}//closes for loop
//set up loop to randomly populate the _shuffledArray
while (_tempArray.length > 0)
{
//declare r which is a random number based on current the length of _tempArray
var r:int = Math.floor(Math.random()*_tempArray.length);
//populate the _suffledArray with the value of _tempArray at the index of r
_shuffledArray.push(_tempArray[r]);
//remove to value of _tempArray at the index of r since it is no longer needed
//this assures that the _shuffledArray will contain non-repeating random numbers
_tempArray.splice(r,1)
}//closes while loop
}//closes ArraySuffle constuctor method
}//closes public class ArraySuffle
}//closes package
Document Class:
//Document Class Test
package {
//flash imports
import flash.display.Sprite;
//custom class imports
import com.ArrayShuffle
//declare Test class
public class Test extends Sprite
{
//vars
//_testArray to populate non-repeating numbers from the ArrayShuffle Class
var _testArray:Array = new Array();
//Test constructor method
public function Test()
{
//new instance of the ArrayShuffle Class
//argument of 10 passed - the amount of non-repeating numbers to create
var _arrayShuffle:ArrayShuffle = new ArrayShuffle(10);
//transfer non-repeating numbers from _arrayShuffle instance to class var
_testArray = _arrayShuffle._shuffledArray;
//trace _testArray
trace(_testArray); //1,5,6,2,8,9,3,4,7,0
}//closes Test constructor method
}//closes Test class
}//closes package
My question… What is the best way to access the Array made by the ArrayShuffle Class? I just used public vars and transferred the data to local vars. But I think this might be a good place to use a getter?
Thanks for any insight…