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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | 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); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 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 ))); } } |