I might do it like this:
class mypackage.Connect4State extends Object {
public function Connect4State(state:Connect4State) {
super();
if (state instanceof Connect4State) {
copy(state);
} else {
board = new Array();
score = new Array();
// Default initialization code.
}
}
public function copy(state:Connect4State):Void {
board = state.board.concat();
score = state.score.concat();
}
public var board:Array;
public var score:Array;
}
Then you could call the construct with or without a parameter.
(Actually, I would never use public variables, but that's another
topic....)
―
Mike Keesey
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:flashcoders-
> [EMAIL PROTECTED] On Behalf Of Paul Steven
> Sent: Thursday, November 02, 2006 2:31 PM
> To: 'Flashcoders mailing list'
> Subject: RE: [Flashcoders] Copy Constructor
>
> Perhaps it is easier if I explain what I need:
>
> I am rewriting some Java code into AS2.
>
> Basically I have a class called Connect4State
>
> class Connect4State {
>
> public function Connect4State() {
>
> // -------------------------
> // Initialize the board array
> // --------------------------
>
> board = new Array();
>
> code goes here
>
>
> // --------------------------
> // Initialize the score array
> // --------------------------
>
> score = new Array();
>
> code goes here
> }
>
> public var board:Array; // A 7 by 6 two dimensional array of
> integers representing the state of game
>
> public var score:Array; // A 2 dimensional array of integers
> representing the "score" for the players
>
> }
>
> The Java code has a function called a "Copy Constructor" that enables
you
> to
> create new objects that are copies of existing objects. The copy
> constructor
> for Connect4State just copies the contents of each member variable. It
is
> necessary to have a copy constructor for Connect4State because the AI
> algorithms use temporary state objects a great deal
>
> This is the "copy constructor" code translated to AS2
>
> public function Connect4State(state:Connect4State) {
>
> // --------------
> // Copy the board
> // --------------
>
> board = new Array();
>
> for (var i:Number = 0; i < 7; i++) {
>
> board[i] = new Array();
>
> for (j:Number = 0; j < 6; j++) {
>
> board[i][j] = state.board[i][j];
>
> }
>
> }
>
>
> // ---------------
> // Copy the scores
> // ---------------
>
> for (var i:Number = 0; i < 2; i++) {
>
> score = new Array();
>
> for (var j:Number = 0; j < winPlaces; j++) {
>
> score[i][j] = state.score[i][j];
> numPieces = state.numPieces;
> }
>
> }
>
>
> }
>
> AS2 seems not to like this as I get the following error
>
> A class must have only one constructor.
> public function Connect4State(state:Connect4State)
>
> Anyone suggest an alternative to this "Copy Constructor"?
>
>
>
>
>
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of Paul
> Steven
> Sent: 02 November 2006 20:01
> To: 'Flashcoders mailing list'
> Subject: [Flashcoders] Copy Constructor
>
> Hi there
>
> Is there an equivalent of the Java Copy constructor in Flash AS2?
>
> Thanks
>
> Paul
>
> _______________________________________________
> [email protected]
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.com
>
> _______________________________________________
> [email protected]
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.com
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com