I'm trying to make a grid-based level editor, and I need a giant white grid in the background that moves with the objects when the level scrolls, but isn't one huge image that's the size of the stage. Any ideas on how to do that?
int scrollAmtX = (scrollingX+gridSize)%gridSize; // wraps along x-axis int scrollAmtY = (scrollingY+gridSize)%gridSize; // wraps along y-axis GreenfootImage old = new GreenfootImage(gridSize, gridSize); old.drawImage(getBackground(), 0, 0); // the current grid-sized image GreenfootImage image = new GreenfootImage(gridSize, gridSize); image.drawImage(old, scrollAmtX, scrollAmtY); image.drawImage(old, scrollAmtX-gridSize, scrollAmtY); image.drawImage(old, scrollAmtX, scrollAmtY-gridSize); image.drawImage(old, scrollAmtX-gridSize, scrollAmtY-gridSize); setBackground(image); // move all objects by (objectRef.getX()+scrollingX, objectRef.getY()+scrollingY) scrollX += scrollingX; scrollY += scrollingY;
boolean autoScroll=true; int xc=getWidth()/2;//X center of screen int yc=getHeight()/2;//Y center of screen int xX=xc;//X's X coordinate int xY=yc;//X's Y coordinate public void getCoordinates(int x, int y){xX=x; xY=y;} int xcorrect=0;//How much world needs to be scrolled on the X-axis int ycorrect=0;//How much world need to be scrolled on the Y-axis int s=25;//How far from center X can stray before the screen scrolls int xst=0;//How far the screen's x coordinate is away from start int yst=0;//How far the screen's y coordinate is away from start int rlimit=1000; int llimit=1000; int ulimit=500; int dlimit=200; public void setScrollingLimits(int right, int left, int up, int down){rlimit=right; llimit=left; ulimit=up; dlimit=down;} public void scrollingEngine(){ xcorrect=0; ycorrect=0; if (autoScroll==true){ //Figure out how far away X is from the scrolling margin, if at all if (xX<xc-s){xcorrect=xc-s-xX;}else if (xX>xc+s){xcorrect=xc+s-xX;} if (xY<yc-s){ycorrect=yc-s-xY;}else if (xY>yc+s){ycorrect=yc+s-xY;} } //Test if the scrolling boundries have been reached, and stop scrolling if they have if (xst+-xcorrect>=rlimit){xcorrect=0; xst=rlimit;} if (xst+-xcorrect<=-llimit){xcorrect=0; xst=-llimit;} if (yst+-ycorrect>=dlimit){ycorrect=0; yst=dlimit;} if (yst+-ycorrect<=-ulimit){ycorrect=0; yst=-ulimit;} //Track the screen's scrolling xst+=-xcorrect; //x-axis tracking yst+=-ycorrect; //y-axis tracking //Scroll world, if at all ((GridCoordinates)getObjects(GridCoordinates.class).get(0)).giveGridCoordinates(xcorrect,ycorrect); if (xcorrect==0&&ycorrect==0)return; scroll(); } Class[] world = {Matter.class,Terrain.class,xbullets.class}; public void scroll(){ for (int i=0; i<world.length; i++){ List objects = getObjects(world[i]); if (! objects.isEmpty()){ for (int o=0; o<objects.size(); o++){ Actor object = (Actor) objects.get(o); object.setLocation(object.getX()+xcorrect,object.getY()+ycorrect); } } } }
GreenfootImage cell = new GreenfootImage(50, 50); cell.setColor(java.awt.Color.white); cell.fill(); cell.setColor(java.awt.Color.gray); cell.drawRect(0, 0, 49, 49); setBackground(cell);