How to properly scroll a 2D tilemap?

Posted by Sri Harsha Chilakapati on Game Development See other posts from Game Development or by Sri Harsha Chilakapati
Published on 2012-03-22T01:23:21Z Indexed on 2012/03/22 17:43 UTC
Read the original article Hit count: 908

Filed under:
|
|

Hello and I'm trying to make my own game engine in Java. I have completed all the necessary ones but I can't figure it out with the TileGame class. It just can't scroll. Also there are no exceptions. Here I'm listing the code.

TileGame.java

@Override
public void draw(Graphics2D g) {
    if (back!=null){
        back.render(g);
    }
    if (follower!=null){
        follower.render(g);
        follower.draw(g);
    }
    for (int i=0; i<actors.size(); i++){
        Actor actor = actors.get(i);
        if (actor!=follower&&getVisibleRect().intersects(actor.getBounds())){
            g.drawImage(actor.getAnimation().getFrameImage(), actor.x - OffSetX, actor.y - OffSetY, null);
            actor.draw(g);
        }
    }
}

/**
 * This method returns the visible rectangle
 * @return The visible rectangle
 */
public Rectangle getVisibleRect(){
    return new Rectangle(OffSetX, OffSetY, global.WIDTH, global.HEIGHT);
}

@Override
public void update(){
    if (follower!=null){
        if (scrollHorizontally){
            OffSetX = global.WIDTH/2 - Math.round((float)follower.x) - tileSize;
            OffSetX = Math.min(OffSetX, 0);
            OffSetX = Math.max(OffSetX, global.WIDTH - mapWidth);
        }
        if (scrollVertically){
            OffSetY = global.HEIGHT/2 - Math.round((float)follower.y) - tileSize;
            OffSetY = Math.min(OffSetY, 0);
            OffSetY = Math.max(OffSetY, global.HEIGHT - mapHeight);
        }
    }
    for (int i=0; i<actors.size(); i++){
        Actor actor1 = actors.get(i);
        if (getVisibleRect().contains(actor1.x, actor1.y)){
            actor1.update();
            for (int j=0; j<actors.size(); j++){
                Actor actor2 = actors.get(j);
                if (actor1.isCollidingWith(actor2)){
                    actor1.collision(actor2);
                    actor2.collision(actor1);
                }
            }
        }
    }
}

but the problem is that all the actors are working, but it just won't scroll. Help Please.. Thanks in Advance.

© Game Development or respective owner

Related posts about 2d

Related posts about java