简体   繁体   中英

LibGdx - SideScroller With TiledMap and Two Actors

I have a tiled map that is 40960px wide and 640px high.

I also have a main character and a badguy that are both 64px wide and 64px high.

Assuming that the bottom left corner of the tiled map is displayed at (0,0) in pixels. I am trying to make the main character start at position (0,0) of the tiled map and be able to move all the way to the other end of the tiled map along the x axis dependent upon the users input.

Also I want a bad guy to be rendered at (255,0) on the x axis and to have a range of movement between 255 and 511 on the x axis this will be controlled programtically.

At the moment my code will display the tiled map and the two characters but when I move one of the characters the other character is moved as well.

I have a link here to an image for clarity

Here is my code in a class that implements libGdx screen interface.

public TestScreen(MyGame game){
    this.game = game;


    camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    camera.position.set(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2, 0);
    camera.update();

    String filename = "levelMaps/level_1.tmx";
    map = new TmxMapLoader().load(filename);        
    mapRenderer = new OrthogonalTiledMapRenderer(map);

    mainPlayer = new Player();

    badGuy = new BadGuy();
    badGuy.velocity.x = camera.position.x;
}

@Override
public void render(float delta) {

    Gdx.gl.glClearColor(0, 0, 0.2f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);       

    mapRenderer.setView(camera);        
    mapRenderer.render();

    game.batch.begin();     
    mainPlayer.render(game.batch);

    badGuy.render(game.batch);
    game.batch.end();


    //simple input handling
    if(Gdx.input.isKeyPressed(Keys.LEFT)){
        if(mainPlayer.velocity.x >= 0 && camera.position.x > 400){
            mainPlayer.moveLeft();      
            camera.position.x -= Math.abs(superSim.velocity.x); 
        } else {
            camera.position.x = 400;
            superSim.velocity.x = 0;
        }
    } 

    if(Gdx.input.isKeyPressed(Keys.RIGHT)){
        if(mainPlayer.velocity.x >= 0 && camera.position.x < 41286-(64*12)){
            mainPlayer.moveRight();     
            camera.position.x += superSim.velocity.x;
        } 
    }

    camera.update();
    mainPlayer.update(delta);
}

How can I get badguys position to remain the same whilst the players position is dynamic?

Anyhelp is truly appreciated

This is how i inialize my camera to be 16*9 units.

private OrthographicCamera camera;
camera = new OrthographicCamera(16, 9);
camera.position.set(camera.viewportWidth / 2,
            camera.viewportHeight / 2, 0);
camera.update();

My resize method

public void resize(int width, int height) {
        camera.viewportHeight = 16 * (float) height / (float) width;
        camera.update();
    }

Really my camera is 16*y units because i calculate height to keep same aspect ratio.

Use deltaTime for movement to make app run smoothly on different fps rates. Your input handling is very weird. Why do you change camera's position there? If you need to make your camera follow the player then make something like this:

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.mygdx.game.entities.Player;

    public class ExtendedCamera extends OrthographicCamera {
        public Player player;

        public ExtendedCamera(Player player) {
            super(Constants.WORLD_WIDTH, Constants.WORLD_HEIGHT);
            this.player = player;
        }

        public void followPlayer() {
            if (player.body.getPosition().x - position.x > Constants.CAMERA_FOLLOW_LINE_X) {
                position.x = player.body.getPosition().x
                        - Constants.CAMERA_FOLLOW_LINE_X;
                update();
            } else if (player.body.getPosition().x - position.x < -Constants.CAMERA_FOLLOW_LINE_X) {
                position.x = player.body.getPosition().x
                        + Constants.CAMERA_FOLLOW_LINE_X;
                update();
            }
            if (player.body.getPosition().y - position.y > Constants.CAMERA_FOLLOW_LINE_Y) {
                position.y = player.body.getPosition().y
                        - Constants.CAMERA_FOLLOW_LINE_Y;
                update();
            } else if (player.body.getPosition().y - position.y < -Constants.CAMERA_FOLLOW_LINE_Y) {
                position.y = player.body.getPosition().y
                        + Constants.CAMERA_FOLLOW_LINE_Y;
                update();
            }
        }
    }

With this follow method player has zone where he can move freely and camera won't follow him. If player left this zone, camera moves.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM