.
Floor World
public Floor()
{
// Create a new world with 600x400 cells with a cell
//size of 1x1 pixels.
super(800, 600, 1,false);
makeMap();
update();
addObject(counter, 590, 54);
addObject(healthbar, 689, 54);
addObject(daddy, 37, 541);
prepare();
}
public Counter getCounter()
{
return counter;
}
public HealthBar getHealthBar()
{
return healthbar;
}
public void makeMap()
{
for (int y=0;y<MAPIMGHEIGHT;y++)
{
for (int x=0;x<MAPIMGWIDTH;x++)
{
int colorRGB = mapImg.getColorAt (x,y).getRGB();
if (colorRGB == Color.BLACK.getRGB())
{
int mapX = x * PLATFORMWIDTH + PLATFORMWIDTH/2;
int mapY = y * PLATFORMHEIGHT + PLATFORMHEIGHT/2;
thePlatforms.add(new Platform(mapX,mapY));
}
}
}
}
public void shiftScreen (int changeX, int changeY)
{
leftBound += changeX;
rightBound += changeX;
if (leftBound <0)
{
leftBound =0;
rightBound = getWidth();
}
else if (rightBound >= MAPWIDTH)
{
rightBound = MAPWIDTH;
leftBound = MAPWIDTH - getWidth();
}
topBound -= changeY;
bottomBound -= changeY;
if (topBound <0)
{
topBound =0;
bottomBound = getHeight();
}
else if (bottomBound >= MAPHEIGHT)
{
bottomBound = MAPHEIGHT;
topBound = MAPHEIGHT - getHeight();
}
update();
}
public void update()
{
Platform thisPlatform;
int thisPlatformX;
int thisPlatformY;
int screenX;
int screenY;
for (int i=0;i<thePlatforms.size();i++)
{
thisPlatform = thePlatforms.get(i);
thisPlatformX = thisPlatform.mapX;
thisPlatformY = thisPlatform.mapY;
if (thisPlatformX + PLATFORMWIDTH>=leftBound && thisPlatformX - PLATFORMWIDTH<=rightBound && thisPlatformY+PLATFORMHEIGHT >=topBound && thisPlatformY-PLATFORMHEIGHT<=bottomBound)
{
screenX = thisPlatformX - leftBound;
screenY = thisPlatformY - topBound;
if (thisPlatform.getWorld()==null)
{
addObject (thisPlatform, screenX, screenY);
}
else
{
thisPlatform.setLocation(screenX, screenY);
}
}
else
{
if (thisPlatform.getWorld()!=null)
{
removeObject(thisPlatform);
}
}
}
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
/* Potionpotion = new Potion;
for (int i=0;i <potion.length;i++)
{
potion = new Potion();
int potionX = Greenfoot.getRandomNumber(getWidth());
int potionY = Greenfoot.getRandomNumber(600);
addObject(potion, potionX, potionY);
}*/
}
Actor Class
Counter counter = new Counter();
GreenfootSound jumpMusic = new GreenfootSound ("jumpMusic.wav");
boolean inTheAir = false;
int groundHeight = getImage().getHeight()/2;
int sideWidth = getImage().getWidth()/2;
int deltaX = 0;
int deltaY = 0;
World myWorld;
int worldHeight;
int worldWidth;
boolean touchingHuman = false;
public void addedToWorld (World theWorld)
{
myWorld = theWorld;
worldHeight = myWorld.getHeight();
worldWidth = myWorld.getWidth();
}
public void act()
{
Actor potion = getOneIntersectingObject(Potion.class);
if (potion !=null)
{
World myWorld = getWorld();
myWorld.removeObject(potion);
Floor floor = (Floor)myWorld;
Counter counter = floor.getCounter();
counter.addScore();
}
moveMent();
move();
hitHuman();
leftNright();
}
public void moveMent()
{
if(inTheAir)
{
fall();
}else
{
if (Greenfoot.isKeyDown ("left"))
{
deltaX = -4;
}
else if (Greenfoot.isKeyDown ("right"))
{
deltaX = 4;
}else
{
deltaX = 0;
}
if (Greenfoot.isKeyDown("space"))
{
deltaY +=10;
jumpMusic.play();
jumpMusic.setVolume(95);
}
}
}
public void move()
{
int newX = getX() + deltaX;
int newY = getY() - deltaY;
Actor platformBelow = getOneObjectAtOffset(0, groundHeight+4,Platform.class);
Actor platformAbove = getOneObjectAtOffset(0, -groundHeight-4,Platform.class);
if (platformBelow !=null)
{
if (deltaY<0)
{
deltaY = 0;
inTheAir = false;
GreenfootImage platformImage = platformBelow.getImage();
int topOfPlatform = platformBelow.getY() - platformImage.getHeight()/2;
newY = topOfPlatform - groundHeight;
}
}
else if (getY() >= worldHeight - groundHeight)
{
if (deltaY <0)
{
deltaY = 0;
inTheAir = false;
newY = worldHeight - groundHeight;
}
}
else
{
inTheAir = true;
}
if (platformAbove !=null)
{
if (deltaY>0)
{
deltaY = 0;
GreenfootImage platformImage = platformAbove.getImage();
int bottomOfPlatform = platformAbove.getY() + platformImage.getHeight()/2;
newY = bottomOfPlatform + groundHeight;
}
}
if (getX() <= sideWidth)
{
deltaX = Math.abs(deltaX);
}
if (getX() >= worldWidth-sideWidth)
{
deltaX = Math.abs(deltaX)*-1;
}
setLocationWithScroll(newX, newY);
}
public void leftNright()
{
int newX = getX() + deltaX;
int newY = getY() - deltaY;
Actor platformToRight = getOneObjectAtOffset(sideWidth +4, 0,Platform.class);
Actor platformToLeft = getOneObjectAtOffset(-sideWidth -4, 0,Platform.class);
if (platformToRight !=null)
{
deltaX = Math.abs(deltaX)*-1;
}
if (platformToLeft !=null)
{
deltaX = Math.abs(deltaX);
}
setLocationWithScroll(newX, newY);
}
public void fall()
{
deltaY--;
}
public void setLocationWithScroll (int newX, int newY)
{
Floor myFloor = (Floor)getWorld();
if ((newY<200 && myFloor.topBound>0) || (newY > worldHeight-200&& myFloor.bottomBound < myFloor.MAPHEIGHT))
{
int yShift = newY - getY();
myFloor. shiftScreen(0, -yShift);
}else
{
setLocation(getX(), newY);
}
if ((newX<350 && myFloor.leftBound >0) || (newX>worldWidth-350 && myFloor.rightBound < myFloor.MAPWIDTH))
{
int xShift = newX - getX();
myFloor.shiftScreen(xShift,0);
}else
{
setLocation(newX, getY());
}
}
i dont know how to add some random actors on the world that will go left and disappears while my actor go right. Thank you in advance :)