I have been working on a terraria like game, and i have everything that i had really set forth to do already done, execpet that there is no lighting. :( here is my code so far for my "light.class" and my "block.class":
and the other one:
right now it just sets it springs a "null pointer exception" on my while loop. any Ideas?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Block here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Block extends Actor
{
int darkness = 0;
int blockSize = ((PlayerWorld) getWorld()).blockSize;
public GreenfootImage lightMap = new GreenfootImage(blockSize, blockSize);
GreenfootImage baseImg = null;
public Block()
{
lightMap.setColor(new java.awt.Color(0,0,0,darkness));
lightMap.fillRect(0, 0, blockSize, blockSize);
}
/**
* Act - do whatever the Block wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
}
public void inheirt(Block obj)
{
GreenfootImage image = obj.getImage();
image.scale(10,10);
setLight(obj, 200);
}
public void setLight(Block obj, int light)
{
darkness = 255 - light;
baseImg = obj.getImage();
lightMap.clear();
lightMap.setColor(new java.awt.Color(0,0,0,darkness));
lightMap.fillRect(0, 0, blockSize, blockSize);
GreenfootImage tempImg = baseImg;
tempImg.drawImage(lightMap, 0 ,0);
obj.setImage(tempImg);
}
public void addLight(Block obj, int light)
{
darkness = darkness - light;
lightMap.clear();
lightMap.setColor(new java.awt.Color(0,0,0,darkness));
lightMap.fillRect(0, 0, blockSize, blockSize);
GreenfootImage tempImg = baseImg;
tempImg.drawImage(lightMap, 0 ,0);
obj.setImage(tempImg);
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class light here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class light extends Actor
{
/**
* Act - do whatever the light wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
}
public void produceRadiusLight()
{
}
public void produceSunLight()
{
int x = 0;
PlayerWorld world = ((PlayerWorld) getWorld());
int worldWidth = world.sizeX/world.blockSize;
while (x < worldWidth)
{
getTopBlock(x).setLight(getTopBlock(x), 255);
x++;
}
}
public Block getTopBlock(int x)
{
int y = 0;
World world = getWorld();
while(world.getObjectsAt(x, y, null).isEmpty())
{
y++;
}
return ((Block) (world.getObjectsAt(x, y, Block.class).get(0)));
}
}


but i don't know how to do this.