// players code
import greenfoot.*;
public class Survivor extends Actor
{
// GLOBAL DECLARATIONS
// -------------------
int speed = 6;
int fn=0; // fn - frame number: TRACK ARRAY INDEX FOR CURRENT PICTURE
int dn =0; //dn is direction number first index in 2nd array
int actCount =0;
int score=0;
static ScoreBoard sb;
static MyWorld w;
static stage2 w2;
static stage3 w3;
GreenfootImage[][] images = new GreenfootImage[2][3];
// CONSTRUCTOR // runs one time only
// -----------
public Survivor()
{
// load images into an array of pics
// ---------------------------------
for(int i=0; i<3; i++)
{
images[0][i] = new GreenfootImage("playerRight"+i+".png");
} // end for
for(int i=0; i<3; i++)
{
images[1][i] = new GreenfootImage("playerLeft"+i+".png");
} // end for
// set the current image
// ----------------------
setImage(images[0][0]);
} // end constructor
// ACT METHOD // runs repeatedly
// ----------
public void act()
{
setLocation(getX(),getY());
checkForDirectionalControll();
isTagged();
switchWorld();
} // end act
public void checkForDirectionalControll()
{
actCount++;
// keyboard control
// -----------------
if(Greenfoot.isKeyDown("right")&&actCount>=10)
{
setLocation(getX()+speed, getY());
dn = 0; // dn 0 for right 1 for left
switchImage();
actCount =0;
} // end if
if(Greenfoot.isKeyDown("left")&&actCount>=10)
{
setLocation(getX()-speed, getY());
dn = 1;
switchImage();
actCount =0;
} // end if
if(Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY()-speed);
}
if(Greenfoot.isKeyDown("down"))
{
setLocation(getX(), getY()+speed);
}
//end checkForDirectionalControll
}
public void switchImage()
{
// increase frame number, then make sure we haven't gone off the edge of the array
// -------------------------------------------------------------------------------
fn++;
if(fn>=3)
{
fn=0;
} // end if
// set the image of the actor to the new pic
// ------------------------------------------
setImage(images[dn][fn]);
} // end switchImage
public void isTagged()
{
if(isTouching(Zombie.class))
{
removeTouching(Zombie.class);
w = (MyWorld)getWorld();
sb = w.getScoreboard();
sb.increaseScore(1000);
}
}
public void switchWorld()
{
if(getWorld()instanceof MyWorld)
{
if(sb.getScore()>=10100)
{
Greenfoot.setWorld(new stage2());
}
}//end if
if(getWorld()instanceof stage2)
{
if( sb.getScore() >=100100)
{
Greenfoot.setWorld(new stage3());
}
}//end if
}
}// scoreBoard code where i want to get the code from
import greenfoot.*;
public class ScoreBoard extends Actor
{
// GLOBAL DECLARATIONS
// -------------------
FontImage pic;
int score=0;
int finalScore =0;
// CONSTRUCTOR // runs one time only
// -----------
public ScoreBoard()
{
pic = new FontImage("score:"+score,36,Color.BLUE,new Color(0,0,0,0),"Square.ttf");
setImage(pic);
} // end constructor
public void increaseScore(int newPts)
{//increases score
score=score+ newPts;
pic = new FontImage("score:"+score,36,Color.BLUE,new Color(0,0,0,0),"Square.ttf");
setImage(pic);
} //end increase score
public int getScore()
{
finalScore=finalScore +score;
return finalScore;
}
// ACT METHOD // runs repeatedly
// ----------
public void act()
{
} // end act
} // end class
