package {
import flash.display.*;
import flash.events.*;
import flash.utils.*;
import flash.errors.*;
public class DrawTrees extends MovieClip {
//drawing board variables
private static const DrawBoardWidth:uint=500;
private static const DrawBoardHeight:uint=500;
private static const DrawBoardPegHorizontalSpacing:uint=50;
private static const DrawBoardPegVerticalSpacing:uint=50;
private static const DrawBoardOffsetx:int=25;
private static const DrawBoardOffsety:int=25;
private static const GroundHeight:int=400;
//tree variables, including different player colors
private static const pointSize:uint=5;
private static const playerOneColor:uint=0x000000;
private static const playerTwoColor:uint=0xFFFFFF;
//this is the number of players. each should have his own tree.
public var NumberOfPlayers:uint=1;
//this is the current player's number. eventually, should switch between turns.
public var CurrentPlayer:uint=1;
public var CurrentPlayerColor:uint=playerOneColor;//current player's color switches between turns, too.
public var SelectedPoint:Sprite=null;//the currently-selected point. NOTE: Does this work?
public var PointIsSelected:Boolean=false;//is a point selected?
//this array will hold all of the trees
public var allTrees:Array=null;
//this is where the points will be drawn- a limiting box.
public var DrawBoard:Sprite=new Sprite;
//constructor function
public function DrawTrees() {
MakeDrawBoard();
FillallTrees(NumberOfPlayers);
DrawBoard.addEventListener(MouseEvent.CLICK,DrawTreePoint);//draw a point when they click
}
//this makes and places the drawing board (limit of where points can be drawn)
//NOTE: this will eventually have to be invisible. how to do this?
public function MakeDrawBoard() {
DrawBoard.graphics.lineStyle(2,0x000000);
DrawBoard.graphics.beginFill(0xFF0000);
DrawBoard.graphics.drawRect(0,0,DrawBoardWidth,DrawBoardHeight);
DrawBoard.graphics.endFill();
DrawBoard.x=DrawBoardOffsetx;
DrawBoard.y=DrawBoardOffsety;
stage.addChild(DrawBoard);
}
//this fills the allTrees array with new tree arrays, up to the number of players (each player has a tree)
public function FillallTrees(NumberOfPlayers:uint) {
allTrees=new Array;
for (var x:uint=0; x < NumberOfPlayers; x++) {
var tree:Array=new Array ;
allTrees.push(tree);
}
}
//this creates, adds to trees, and draws points, given X and Y coordinates (pointX and pointY, respectively)
public function DrawTreePoint(event:MouseEvent) {
var newbranch:Object=new Object;
if (allTrees[CurrentPlayer - 1].length == 0) {//if the current player's tree length is zero (no points have been added):
newbranch.point=new Sprite;
newbranch.point.addEventListener(MouseEvent.CLICK,SelectPoint);
newbranch.point.graphics.lineStyle(0,CurrentPlayerColor);//the color of the point changes depending on whose turn it is.
newbranch.point.graphics.beginFill(CurrentPlayerColor);//this too
newbranch.point.graphics.drawCircle(event.localX,GroundHeight,pointSize);//add point at their cursor's x, but at GroundHeight y.
newbranch.point.graphics.endFill();
allTrees[CurrentPlayer - 1].push(newbranch);//this should add the new point to the end of the tree-array of the current player
DrawBoard.addChild(allTrees[CurrentPlayer - 1][allTrees[CurrentPlayer - 1].length - 1].point);/*this should add to DrawBoard the point that was just added
(in current player's tree array, the point that is at the current length of the array, minus one (to compensate for 0 index)*/
} else if (PointIsSelected) {//if a point is selected:
//add a new point,
newbranch.point=new Sprite;
newbranch.point.addEventListener(MouseEvent.CLICK,SelectPoint);
newbranch.point.graphics.lineStyle(0,CurrentPlayerColor);//the color of the point changes depending on whose turn it is.
newbranch.point.graphics.beginFill(CurrentPlayerColor);//this too
newbranch.point.graphics.drawCircle(event.localX,event.localY,pointSize);//add point at their cursor
newbranch.point.graphics.endFill();
//and a new line.
newbranch.branch=new Shape;
newbranch.branch.graphics.lineStyle(3,CurrentPlayerColor);
newbranch.branch.graphics.moveTo(newbranch.point.x,newbranch.point.y);
newbranch.branch.graphics.lineTo(SelectedPoint.x,SelectedPoint.y);
//add newbranch to tree array
allTrees[CurrentPlayer - 1].push(newbranch);//this should add the new point to the end of the tree-array of the current player
//draw new point and branch
DrawBoard.addChild(allTrees[CurrentPlayer - 1][allTrees[CurrentPlayer - 1].length - 1].point);/*this should add to DrawBoard the point that was just added
(in current player's tree array, the point that is at the current length of the array, minus one (to compensate for 0 index)*/
D