How to apply a camera movement in Libgdx game tutorial series




Hello and welcome to this another  article on how to make a Libgdx game tutorial series,In this tutorial we will do an activity on which it will help us to enhance our coding skills using java language,furthermore we will also learn how to Apply a Camera movement into our Game Screen using the built in method inside of our camera class which gonna allow us to give an illusion of transition frames with help of translate method, The code below will help you to accomplish this task, you may also watch my video in case you don't understand the code.




This video is a continuation of the previous upload on How to Apply camera movement into our game screen in libgdx game development series
package com.ninal.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class ApplyCameraMovement extends ApplicationAdapter {
private OrthographicCamera camera;
private SpriteBatch batch;
private TextureRegion playerTexture;
private TextureRegion backgroundImage;

@Override
public void create() {
camera = new OrthographicCamera();
batch = new SpriteBatch();
configureCamera();
Texture texture1 = new Texture(Gdx.files.internal("sprites/background.png"));
backgroundImage = new TextureRegion(texture1,0,0,2048,568);
Texture texture = new Texture(Gdx.files.internal("sprites/Player.png"));
playerTexture = new TextureRegion(texture,50,50,250,250);
}

private void configureCamera() {
if(Gdx.graphics.getHeight()<Gdx.graphics.getWidth()){
camera.setToOrtho(false,800,800*Gdx.graphics.getHeight()/Gdx.graphics.getWidth());

}
else{
camera.setToOrtho(false,800*Gdx.graphics.getWidth()/Gdx.graphics.getHeight(),800);
}
}

@Override
public void render() {
Gdx.gl.glClearColor(0,0,0,0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
for(int i =0;i<30;i++){
batch.draw(backgroundImage, i*2900, 0,2900,800);
}
for(int i =0;i<100;i++){
batch.draw(playerTexture,i*800,50,250,250);
}
batch.end();
camera.translate(500*Gdx.graphics.getDeltaTime(),0);
}

@Override
public void resize(int width, int height) {
configureCamera();

}
@Override
public void pause() {

}

@Override
public void resume() {

}
@Override
public void dispose() {
batch.dispose();
}
}


Post a Comment

0 Comments