This site requires JavaScript, please enable it in your browser!
Greenfoot back
Entity1037
Entity1037 wrote ...

2013/10/5

Create a grid that outlines squares where objects can be placed?

Entity1037 Entity1037

2013/10/5

#
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?
danpost danpost

2013/10/6

#
You can make the grid by setting the background of the world to an grid-cell-sized image that has a frame, with your world being of cell-size one. After that, you just need a scrolling engine with x and y scroll offsets and an array to keep track of where everything is at; so object would be located at (arrayX*gridSize+gridSize/2+scrollX, arrayY*gridSize+gridSize/2+scrollY). Hope this helps. (+: DAN :+)
danpost danpost

2013/10/6

#
To scroll the background, take a grid-cell-sized image from the top-left corner of your current background and apply the scroll amounts to that image and re-apply the new image to set the background to. For example, if you needed to scroll scrollingX along the x-axis and scrollingY along the y-axis:
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;
Entity1037 Entity1037

2013/10/6

#
Actually, I have a scrolling engine already. I just need some way to make a 50 cell X 50 cell grid that moves with the objects (which can only be placed inside those grid spaces. Here's my scrolling engine:
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);
                }
            }
        }
    }
The player's coordinates are continually given to the engine by X.class, which uses getCoordinates(int x,int y){}
danpost danpost

2013/10/6

#
Entity1037 wrote...
I just need some way to make a 50 cell X 50 cell grid that moves with the objects (which can only be placed inside those grid spaces.
The code I supplied above is sufficient in scrolling the background image with the objects. If it is the image itself you are having problems with then use the following in the world constructor:
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);
Entity1037 Entity1037

2013/10/6

#
I'm having problems getting your code to work. I'm not exactly sure what those variables are suppose to represent in the first place, and the background ends up going really fast diagonally.
Entity1037 Entity1037

2013/10/6

#
Nevermind, I got it! ScrollingX and ScrollingY were the same thing as xcorrect and ycorrect.
You need to login to post a reply.