Ok, so about a week or two ago, I posted about a problem with world generation, and here I am again, posting about yet another error.
I'm trying to make a java version of Mario bros, and when I compile and shift the screen, only QuestionMarkBlock and GroundBlock spawn, and I can't get the other two (BrickBlock and HillA) to spawn. the world's code, which I'll show here, is supposed to identify specific colors on a map and spawn objects if it detects those colors on the map. before we begin, I'd like to mention that while there is a GroundBlockMap actor, it has no code. here's the world's code:
as a sidenote, those other colors are simply for later, so don't mind them (unless they affect world generation)
and here's code for each of these entities (don't worry, they're not nearly as long). first is the GroundBlock, which works:
second is the HillA, which doesn't work:
3rd is the BrickBlock which, again, doesn't work:
and last but not least, the QuestionMarkBlock, which works (surprisingly):
if anyone knows how to fix this, please let me know. I'm using greenfoot 2.4.2, if that helps. if there is a more efficient method I could use, please tell me, I would be interested in knowing. thank you!
import greenfoot.*; import java.awt.Color; import java.util.List; import java.util.ArrayList; public class Sky extends World { GroundBlockMap map = new GroundBlockMap(); //new groundblock map GreenfootImage mapImg = map.getImage(); final int MAPIMGWIDTH = mapImg.getWidth(); final int MAPIMGHEIGHT = mapImg.getHeight(); GroundBlock groundBlockTemplate = new GroundBlock(0,0); //groundblocktemplate placed offscreen BrickBlock brickBlockTemplate = new BrickBlock(0,0); //brickblocktemplate placed offscreen QuestionMarkBlock questionMarkBlockTemplate = new QuestionMarkBlock(0, 0); //questionmarkblocktemplate placed offscreen HillA hillATemplate = new HillA(0, 0); //hilla template placed offscreen GreenfootImage pfImg = groundBlockTemplate.getImage(); //gets image of groundblock GreenfootImage bbImg = brickBlockTemplate.getImage(); //gets image of brickblock GreenfootImage qmbImg = questionMarkBlockTemplate.getImage(); //gets image of questionmarkblock GreenfootImage haImg = hillATemplate.getImage(); //gets image of hilla final int GROUNDBLOCKHEIGHT = pfImg.getHeight(); //the height of the groundblock final int GROUNDBLOCKWIDTH = pfImg.getWidth(); //the width of the groundblock final int BRICKBLOCKHEIGHT = bbImg.getHeight(); //the height of the brickblock final int BRICKBLOCKWIDTH = bbImg.getWidth(); //the width of the brickblock final int QUESTIONMARKBLOCKHEIGHT = qmbImg.getHeight(); //the height of the questionmarkblock final int QUESTIONMARKBLOCKWIDTH = qmbImg.getWidth(); //the width of the questionmarkblock final int HILLAHEIGHT = haImg.getHeight(); //the height of hill a final int HILLAWIDTH = haImg.getWidth(); //the width of hill a final int MAPWIDTH = MAPIMGWIDTH * GROUNDBLOCKWIDTH; //sizes the world's width, does not need counterparts final int MAPHEIGHT = MAPIMGHEIGHT * GROUNDBLOCKHEIGHT; //sizes the world's height, does not need couterparts private List<GroundBlock> theGroundBlocks = new ArrayList<GroundBlock>(); // thegroundblocks, arraylist for groundblock private List<BrickBlock> theBrickBlocks = new ArrayList<BrickBlock>(); //thebrickblocks, arraylist for brickblock private List<QuestionMarkBlock> theQuestionMarkBlocks = new ArrayList<QuestionMarkBlock>(); //theQuestionMarkBlocks, arraylist for questionmarkblock private List<HillA> theHillAs = new ArrayList<HillA>(); //the HillAs, arratlist for hilla int leftBound = 0; int bottomBound = MAPHEIGHT; int topBound = MAPHEIGHT - getHeight(); int rightBound = getWidth(); /** * Constructor for objects of class Sky. * */ public Sky() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(512, 480, 1); makeMap(); update(); } public void makeMap() { for(int y=0;y<MAPIMGHEIGHT;y++) { for(int x=0;x<MAPIMGWIDTH;x++) { int colorRGB = mapImg.getColorAt(x,y).getRGB(); /*NOTE: colors below are listed in the order seen when viewing the default colors through "edit colors" in ms paint */ Color hillA = new Color(255, 128, 128); //scenery, hill tile 1, color 1 Color hillB = new Color(255, 255, 128); //scenery, hill tile 2, color 2 //Color hillT3 = new Color(0, 0, 255); //scenery, hill tile 3, Color 29 //Color hillT4 = new Color(0, 0, 160); //scenery, hill tile 4, color 30 //Color hillT5 = new Color(128, 0, 128); //scenery, hill tile 5, color 31 //Color hillT6 = new Color(128, 0, 255); //scenery, hill tile 6, Color 32 Color cloudA = new Color(128, 255,128 ); //scenery, one cloud, color 3 Color cloudB = new Color(0, 255, 128); //scenery, two clouds, color 4 Color cloudC = new Color(128, 255, 255); //scenery, three clouds, color 5 Color bushA = new Color(0, 128, 255); //scenery, one bush, color 6 Color bushB = new Color(255, 128, 192); //scenery, two bushes, color 7 Color bushC = new Color(255, 128, 255); //scenery, three bushes, color 8 Color questionMarkBlock = new Color(255, 0, 0); //block, ? block, color 9 Color hardBlock = new Color(255, 255, 0); //block, hard block, color 10 Color hiddenBlock = new Color(128, 255, 0); //block, hidden block, color 11 Color brickBlock = new Color(0, 128, 64); //block, brick block, color 28 Color mushroom = new Color(0, 255, 64); //powerup, mushroom, color 12 Color star = new Color(0, 255, 255); //powerup, starman, color 13 Color oneUp = new Color(0, 128, 192); //item, 1-up, color 14 Color coin = new Color(128, 128, 192); //item, coin, color 15 Color goomba = new Color(255, 0, 255); //enemy, goomba, color 16 Color koopa = new Color(128, 64, 64); //enemy, koopa, color 17 Color pipeA = new Color(255, 128, 64); //pipe, small, color 18 Color pipeB = new Color(0, 255, 0); //pipe, medium, color 19 Color pipeC = new Color(0, 128, 128); //pipe, large, color 20 Color warpPipeA = new Color(0, 64, 128); //warp pipe, small, color 21 Color warpPipeB = new Color(128, 128, 255); //warp pipe, medium, color 22 Color warpPipeC = new Color(128, 0, 64); //warp pipe, large, color 23 Color flag = new Color(255, 0, 128); //flagpole, flag, color 24 Color pole = new Color(128, 0, 0); //flagpole, pole, color 25 Color castle = new Color(255, 128, 0); //castle, castle, color 26 Color flagCastle = new Color(0, 128, 0); //castle, flag, color 27 Color mario = new Color(0, 0, 255); //mario, color 29 if(colorRGB==Color.BLACK.getRGB()) //spawns groundblocks from the map { int mapGBX = x * GROUNDBLOCKWIDTH + GROUNDBLOCKWIDTH/2; int mapGBY = y * GROUNDBLOCKHEIGHT + GROUNDBLOCKHEIGHT/2; theGroundBlocks.add(new GroundBlock (mapGBX, mapGBY)); } if(colorRGB==brickBlock.getRGB()) { int mapBBX = x * BRICKBLOCKWIDTH + BRICKBLOCKWIDTH/2; int mapBBY = y * BRICKBLOCKHEIGHT + BRICKBLOCKHEIGHT/2; theBrickBlocks.add(new BrickBlock (mapBBX, mapBBY)); } if(colorRGB==questionMarkBlock.getRGB()) { int mapQMBX = x * QUESTIONMARKBLOCKWIDTH + QUESTIONMARKBLOCKWIDTH/2; int mapQMBY = y * QUESTIONMARKBLOCKHEIGHT + QUESTIONMARKBLOCKHEIGHT/2; theQuestionMarkBlocks.add(new QuestionMarkBlock(mapQMBX, mapQMBY)); } if(colorRGB==hillA.getRGB()) { int mapHAX = x * HILLAWIDTH + HILLAWIDTH/2; int mapHAY = y * HILLAHEIGHT + HILLAHEIGHT/2; theHillAs.add(new HillA(mapHAX, mapHAY)); } } } } 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 = MAPHEIGHT - 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() { GroundBlock thisGroundBlock; BrickBlock thisBrickBlock; QuestionMarkBlock thisQuestionMarkBlock; HillA thisHillA; int thisGroundBlockX; int thisGroundBlockY; int thisBrickBlockX; int thisBrickBlockY; int thisQuestionMarkBlockX; int thisQuestionMarkBlockY; int thisHillAX; int thisHillAY; int screenGBX; int screenGBY; int screenBBX; int screenBBY; int screenQMBX; int screenQMBY; int screenHAX; int screenHAY; for(int gbi=0; gbi<theGroundBlocks.size(); gbi++) { thisGroundBlock = theGroundBlocks.get(gbi); thisGroundBlockX = thisGroundBlock.gbmapX; thisGroundBlockY = thisGroundBlock.gbmapY; if(thisGroundBlockX>=leftBound && thisGroundBlockX<=rightBound && thisGroundBlockY>=topBound && thisGroundBlockY<=bottomBound) { screenGBX = thisGroundBlockX - leftBound; screenGBY = thisGroundBlockY - topBound; if(thisGroundBlock.getWorld()==null) { addObject (thisGroundBlock, screenGBX, screenGBY); } else { thisGroundBlock.setLocation(screenGBX, screenGBY); } }else { if(thisGroundBlock.getWorld()!=null) { removeObject(thisGroundBlock); } } } for(int bbi=0; bbi<theBrickBlocks.size(); bbi++) { thisBrickBlock = theBrickBlocks.get(bbi); thisBrickBlockX = thisBrickBlock.bbmapX; thisBrickBlockY = thisBrickBlock.bbmapX; if(thisBrickBlockX>=leftBound && thisBrickBlockX<=rightBound && thisBrickBlockY>=topBound && thisBrickBlockY<=bottomBound) { screenBBX = thisBrickBlockX - leftBound; screenBBY = thisBrickBlockY - topBound; if(thisBrickBlock.getWorld()==null) { addObject (thisBrickBlock, screenBBX, screenBBY); }else { thisBrickBlock.setLocation(screenBBX, screenBBY); } }else { if(thisBrickBlock.getWorld()!=null) { removeObject(thisBrickBlock); } } } for(int qmbi=0; qmbi<theQuestionMarkBlocks.size(); qmbi++) { thisQuestionMarkBlock = theQuestionMarkBlocks.get(qmbi); thisQuestionMarkBlockX = thisQuestionMarkBlock.qmbmapX; thisQuestionMarkBlockY = thisQuestionMarkBlock.qmbmapY; if(thisQuestionMarkBlockX>=leftBound && thisQuestionMarkBlockX<=rightBound && thisQuestionMarkBlockY>=topBound && thisQuestionMarkBlockY<= bottomBound) { screenQMBX = thisQuestionMarkBlockX - leftBound; screenQMBY = thisQuestionMarkBlockY - topBound; if(thisQuestionMarkBlock.getWorld() == null) { addObject (thisQuestionMarkBlock, screenQMBX, screenQMBY); }else { thisQuestionMarkBlock.setLocation(screenQMBX, screenQMBY); } }else { if(thisQuestionMarkBlock.getWorld()!=null) { removeObject(thisQuestionMarkBlock); } } } for(int hai=0; hai<theHillAs.size(); hai++) { thisHillA = theHillAs.get(hai); thisHillAX = thisHillA.hamapX; thisHillAY= thisHillA.hamapY; if(thisHillAX>=leftBound && thisHillAX<=rightBound && thisHillAY>=topBound && thisHillAY<=bottomBound) { screenHAX = thisHillAX - leftBound; screenHAY = thisHillAY - topBound; if(thisHillA.getWorld() == null) { addObject (thisHillA, screenHAX, screenHAY); }else { thisHillA.setLocation(screenHAX, screenHAY); } }else { if(thisHillA.getWorld()!= null) { removeObject(thisHillA); } } } } }
public class GroundBlock extends Actor { int gbmapX; int gbmapY; public GroundBlock(int getMapX, int getMapY) { gbmapX = getMapX; gbmapY = getMapY; } }
public class HillA extends Actor { int hamapX; int hamapY; public HillA(int getMapX, int getMapY) { hamapX = getMapX; hamapY = getMapY; } }
public class BrickBlock extends Actor { int bbmapX; int bbmapY; public BrickBlock(int getMapX, int getMapY) { bbmapX = getMapX; bbmapY = getMapY; } }
public class QuestionMarkBlock extends Actor { int qmbmapX; int qmbmapY; public QuestionMarkBlock(int getMapX, int getMapY) { qmbmapX = getMapX; qmbmapY = getMapY; } }