public class Leaf extends Actor
{
/* Fields Go Here */
public static int numLeaves;
int timer;
int dx;
boolean fallingRight;
/* This is a Constructor */
public Leaf()
{
switch( Greenfoot.getRandomNumber(3) )
{
case 0: setImage("Leaf1.png"); break;
case 1: setImage("Leaf2.png"); break;
case 2: setImage("Leaf3.png"); break;
}
setRotation((int)Math.random()*360);
timer =(int)(Math.random()*900)+100;
if ((int)(Math.random()*5)>=3)
{
fallingRight=true;
}
else if ((int)(Math.random()*5)<=3)
{
}
numLeaves++;
}
public void act()
{
timer--;
if (timer==0 & timer==-1)
{
setLocation (getX()+dx/2,getY()+5-Math.abs(dx)/4);
if(fallingRight == true)
{
dx++;
if (dx > 20)
{
fallingRight = false;
}
}
else if(fallingRight == false)
{
dx--;
if (dx < -20)
{
fallingRight = true;
}
}
if (getY() > 600)
{
numLeaves--;
getWorld().removeObject(this);
}
}
}
public static int getNumLeaves()
{
return numLeaves;
}
public static void reset()
{
numLeaves = 0;
}
}
public class Tree extends World
{
public Tree()
{
super(1000, 750, 1);
Leaf.reset();
}
public void addLeaves( int amount )
{
for(int i=0;0 < amount; i++)
{
int x = (int)(Math.random()*800)+100;
int y = (int)(Math.random()*400);
Leaf leaves = new Leaf();
addObject (leaves,x,y);
}
}
public void increaseLeavesTo( int amount )
{
while (Leaf.numLeaves < amount)
{
int x = (int)(Math.random()*800)+100;
int y = (int)(Math.random()*400);
Leaf leaves = new Leaf();
addObject (leaves,x,y);
}
}
}



