I've been trying to stop moving my camera at the end of my map, but it continues to move.
Here is my code:
#Override
public void render(float delta) {
//moving tiled map
camera.position.x=camera.position.x+Gdx.graphics.getDeltaTime()*200;
camera.update();
//...........................................
Gdx.gl.glClearColor(1, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// renderer camera and map
renderer.setView(camera);
renderer.render();
//...................................................
}
#Override
public void show() {
batch = new SpriteBatch();
map = new TmxMapLoader().load("maps/map1.tmx");
renderer = new OrthogonalTiledMapRenderer(map);
camera = new OrthographicCamera();
}
#Override
public void hide() {
}
#Override
public void create() {
}
#Override
public void resize(int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.position.set(width/2f, height/3f, 0); //by default camera position on (0,0,0)
camera.update();
}
#Override
public void render() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
map.dispose();
renderer.dispose();
}
}
I couldn't find any error in my code.
So my question here is, how to stop the camera from moving at the end of the map?
You have at least one layer in the tiled map, so:
TiledMap map = ......//whatever method that you are using to load a map
//skip to the part that the map is loaded
TiledMapLayer layer = (TiledMapLayer)map.getLayers().get(0);
int layerWidth = layer.getWidth()*layer.getTileWidth();
And now, you know the layer's width in pixels, and yo can make sure that camera.position.x+Gdx.graphics.getDeltaTime()*200; never exceeds this length.
Related
I've a problem with setting my monster on the map. Firstly I create a Knight with camera coordinates. Now I want to set a monster on the map independent of the camera coordinates, so that when I am moving the player using keys the monster stays at the one position. I tried to implemets this and all I got was that the monster stayed at the bottom left corner of the screen all the time. Here is my Person class
public abstract class Person implements Stats {
public Person(String pathToFile,Vector2 position) {
...
}
public void update(float delta) {
spriteBatch.begin();
sprite.draw(spriteBatch);
spriteBatch.end();
}
And my bot class
public class Bot extends Person {
public Bot() {
super(toFilePath,new Vector2(500,550));
super.position.set(500,550);
}
#Override
public void update(float delta) {
super.update(delta);
}
#Override
public void dispose() {
super.dispose();
}
}
The Knight class
public class Knight extends Person {
public Knight(OrthographicCamera camera) {
super(toFilePath, new Vector2(MapScreen.startPositionX, MapScreen.startPositionY));
super.sprite.setCenter(camera.viewportWidth / 2, camera.viewportHeight / 2);
this.camera = camera;
// animation
...
}
public void update(float delta, MapScreen mapScreen) {
camera.update();
walkBatch.begin();
// input handling
walkBatch.end();
}
#Override
public void dispose() {
...
}
And the class where I set up all classes
public class MapScreen implements Screen {
...
#Override
public void show() {
init(startPositionX, startPositionY);
}
// initialize variable
private void init(float posX, float posY) {
camera = new OrthographicCamera();
camera.setToOrtho(false, width, height);
tiledMap = new TmxMapLoader(new ExternalFileHandleResolver()).load(mapName);
setTiledMapRenderer(new OrthogonalTiledMapRenderer(tiledMap));
knight = new Knight(camera);
camera.zoom = ZOOM;
camera.position.set(posX, posY, 0);
camera.update();
}
#Override
public void render(float delta) {
camera.update();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
getTiledMapRenderer().setView(camera);
getTiledMapRenderer().render(layerBottom);
knight.update(delta, this);
getTiledMapRenderer().render(layerTop);
}
When using a SpriteBatch (which you probably don't use at all, when looking at your code), then you can use your camera's matrix to calculate the offsets correctly.
use it like:
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.begin();
// draw your sprites here
spriteBatch.end();
You should then also consider improve performances by not rendering sprites off the screen:
Consider implementing some of the tips
I find a solution. I created a object layer in Tiled and there I set my monsters. Then in MapScreen I render it. Thanks for your help.
I'm kind of new to LibGdx and android studio.
I'm trying to create clickable textures, one for play and one for credits.
Both should be opening a new empty screen/event.
public void create() {
batch = new SpriteBatch();
img = new Texture("Main_Screen.png");
music = Gdx.audio.newMusic(Gdx.files.internal("bgmusic.wav"));
music.play();
music.setLooping(true);
credits = new Texture("credits.png");
play = new Texture("play.png");
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(img, 0, 0);
batch.draw(play, 340, 1400);
batch.draw(credits, 340, 400);
batch.end();
}
However i'm unsure on how to do this since i'm also creating the background with a texture, so i'd be very happy if someone could assist me with helping me out.
Use Sprite instead of Texture, that holds the geometry, color, and texture information for drawing 2D sprites.
MyGdxGame
public class MyGdxGame extends Game {
public Screen menuScreen,creditsScreen,playScreen;
#Override
public void create () {
menuScreen=new MenuScreen(this);
creditsScreen=new CreditsScreen();
playScreen=new PlayScreen();
setScreen(menuScreen);
}
}
MenuScreen
public class MenuScreen extends InputAdapter implements Screen {
SpriteBatch batch;
Texture background,play,credits;
Sprite backgoundSprite,playSprite,creditsSprite;
private ExtendViewport extendViewport;
OrthographicCamera cam;
private float w=480;
private float h=800;
private Vector3 vector3;
MyGdxGame game;
Music music;
public MenuScreen(MyGdxGame game){
this.game=game;
}
#Override
public void show() {
batch = new SpriteBatch();
cam = new OrthographicCamera();
extendViewport=new ExtendViewport(w,h,cam);
vector3=new Vector3();
background = new Texture("Main_Screen.png");
play=new Texture("play.png");
credits=new Texture("credits.png");
backgoundSprite=new Sprite(background);
backgoundSprite.setSize(w,h); // If resources are not in context of your viewport
backgoundSprite.setPosition(0,0); //Default Position
playSprite=new Sprite(play);
playSprite.setSize(100,100);
playSprite.setPosition(w/2-playSprite.getWidth()/2,h/2+100);
creditsSprite=new Sprite(credits);
creditsSprite.setSize(100,100);
creditsSprite.setPosition(w/2-creditsSprite.getWidth()/2,h/2-100);
Gdx.input.setInputProcessor(this);
music = Gdx.audio.newMusic(Gdx.files.internal("bgmusic.wav"));
music.play();
music.setLooping(true);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(cam.combined);
batch.begin();
backgoundSprite.draw(batch);
playSprite.draw(batch);
creditsSprite.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
extendViewport.update(width,height);
cam.position.x = w /2;
cam.position.y = h/2;
cam.update();
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
vector3.set(screenX,screenY,0);
Vector3 position=cam.unproject(vector3);
if(playSprite.getBoundingRectangle().contains(position.x,position.y)) {
game.setScreen(game.playScreen);
}
if(creditsSprite.getBoundingRectangle().contains(position.x,position.y)){
game.setScreen(game.creditsScreen);
}
return super.touchDown(screenX, screenY, pointer, button);
}
#Override
public void pause() { }
#Override
public void resume() { }
#Override
public void hide() { }
#Override
public void dispose() {
batch.dispose();
background.dispose();
play.dispose();
credits.dispose();
}
}
CreditsScreen
public class CreditsScreen implements Screen {
#Override
public void show() {
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
#Override
public void resize(int width, int height) { }
#Override
public void pause() { }
#Override
public void resume() { }
#Override
public void hide() { }
#Override
public void dispose() { }
}
PlayScreen
public class PlayScreen implements Screen {
#Override
public void show() {
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
#Override
public void resize(int width, int height) { }
#Override
public void pause() { }
#Override
public void resume() { }
#Override
public void hide() { }
#Override
public void dispose() { }
}
Welcome to libGDX!
If I understand your question correctly, you are trying to figure out how to change screens in your game. At a high level, this is what you'd need to do:
Draw your button texture on the screen.
Use Gdx.input to detect when the player lifts their mouse button (and if it's over one of your buttons when they do it)
Switch to the new screen (presumably with a new background) when this happens.
I would recommend that you first complete the "A Simple Game" and "Extending the Simple Game" tutorials in order to familiarize yourself with the basics of libGDX and the Game and Screen classes.
Next, try using Scene2D.UI (there are links at the bottom of the second tutorial) to add a Stage and TextButtons to your main menu.
Hopefully that helps you get started- there is a lot of helpful information on that libGDX wiki which I think you will find helpful.
i'm making a game for school motives, i have already made the sprite and the background(tiled map) my problem is how i can make the sprite move left, right, back and down using the keyboard, please guys help me as soon as possible here is my code:
public class LEVEL1 implements ApplicationListener, Screen {
private Music music;
private SpriteBatch batch;
private Texture Sprite;
private Vector2 position;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private OrthographicCamera camera;
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
position.y = position.y - 5;
// player Controls
if(position.y < 0){
position.y = 0;
}
//..................................................
// renderer camera and map
camera.update();
renderer.setView(camera);
renderer.render();
//...................................................
//tells the computer when to start drawing textures
batch.begin();
batch.draw(Sprite, position.x, position.y, 50, 50);
batch.end();
//...................................................
camera = new OrthographicCamera();
camera.setToOrtho(true, 2920,950);
}
#Override
public void show() {
Sprite = new Texture("Sprite.png");
batch = new SpriteBatch();
position = new Vector2(650, Gdx.graphics.getHeight());
map = new TmxMapLoader().load("map1.tmx");
renderer = new OrthogonalTiledMapRenderer(map);
camera = new OrthographicCamera();
music = Gdx.audio.newMusic((Gdx.files.internal("GameSound.mp3")));
music.setLooping(false);
music.setVolume(0.5f);
music.play();
}
#Override
public void create() {
}
#Override
public void resize(int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.position.set(width/2f, height/3f, 0); //by default camera position on (0,0,0)
camera.update();
}
#Override
public void render() {
if(Gdx.input.justTouched())
music.play();
}
#Override
public void dispose() {
map.dispose();
renderer.dispose();
music.dispose();
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
}
Here i make a little example of moving a sprite using keys (up,down, left,right)
you should find more details in libgdx wiki
public class Level1 implements ApplicationListener {
Sprite sprite;
SpriteBatch batch;
float spriteXposition;
float spriteYposition;
#Override
public void render() {
Gdx.gl.glClearColor(1, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//tells the computer when to start drawing textures
batch.begin();
sprite.setPosition(spriteXposition, spriteYposition);
sprite.draw(batch);
batch.end();
spriteControl();
}
public void spriteControl() {
if(Gdx.input.isKeyPressed(Keys.UP)) {
spriteYposition++;
}
if(Gdx.input.isKeyPressed(Keys.DOWN)) {
spriteYposition--;
}
if(Gdx.input.isKeyPressed(Keys.LEFT)) {
spriteXposition--;
}
if(Gdx.input.isKeyPressed(Keys.RIGHT)) {
spriteXposition++;
}
}
#Override
public void create() {
sprite = new Sprite(new Texture(Gdx.files.internal("sprite.png")));
batch = new SpriteBatch();
}
}
To react on input events you need to implement InputProcessor. It has the methods keyDown (called when a key is pressed) and keyUp (called when a key is released).
Those methods have an argument keyCode, which defines the int code of the pressed/released key.
So you need to override this 2 methods and depending on the keyCode you get, you should do something or not.
To move the player for example, you might keep a member speed, which you set, depending on the pressed/released key. In the render method you then need to update the position depending on the elapsed time (delta) and the speed.
To get the input event, you need to tell libgdx, that your InputProcessor should be the active one. This is done by calling Gdx.input.setInputProcessor(yourInputProcessor).
Also make sure to read the Libgdx wiki, many of your questions will be answered there.
EDIT:
Some code:
public class Level implements ApplicationListener, InputProcessor {
private int speedX; // Speed in x direction
private Vector2 position; // Position
private boolean keyDown(int keyCode) {
boolean result = false;
if (keyCode == Keys.D) {
result = true;
speed += 5;
}
else if (keyCode == Keys.A) {
result = true;
speed -= 5;
}
return result;
}
private boolean keyUp(int keyCode) {
boolean result = false;
if (keyCode == Keys.D) {
result = true;
speed -= 5;
}
else if (keyCode == Keys.A) {
result = true;
speed += 5;
}
return result;
}
public void render(float delta) {
position.x += speed*delta;
// Render here
}
}
I am making a 2d game using libgdx and am adding hexagon shaped actors to a group which is then added to a stage. For a normal camera you can use camera.zoom in the render method to zoom in and out along with camera.translate to pan around the world.
I have been getting the camera used by the stage using stage.getCamera() and I can still call stage.getcamera().translate however there is no stage.getCamera().zoom option.
Here is my code:
//import statements
public class HexGame implements ApplicationListener{
private Stage stage;
private Texture hexTexture;
private Group hexGroup;
private int screenWidth;
private int screenHeight;
#Override
public void create() {
hexTexture = new Texture(Gdx.files.internal("hex.png"));
screenHeight = Gdx.graphics.getHeight();
screenWidth = Gdx.graphics.getWidth();
stage = new Stage(new ScreenViewport());
hexGroup = new HexGroup(screenWidth,screenHeight,hexTexture);
stage.addActor(hexGroup);
}
#Override
public void dispose() {
stage.dispose();
hexTexture.dispose();
}
#Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
handleInput();
stage.getCamera().update();
}
private void handleInput() {
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
stage.getCamera().translate(-3, 0, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
stage.getCamera().translate(3, 0, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
stage.getCamera().translate(0, -3, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
stage.getCamera().translate(0, 3, 0);
}
//This is the part that doesn't work
/*
if (Gdx.input.isKeyPressed(Input.Keys.Z)) {
stage.getCamera().zoom += 0.02;
}
*/
}
#Override
public void resize(int width, int height) {
stage.getViewport().update(width, height);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
Any help is appreciated, and if there is anything else wrong with my code please let me know, I'm new to libgdx. Thanks
Zoom is available in OrthographicCamera class and by default Stage class create a OrthographicCamera
/** Creates a stage with a {#link ScalingViewport} set to {#link Scaling#stretch}. The stage will use its own {#link Batch}
* which will be disposed when the stage is disposed. */
public Stage () {
this(new ScalingViewport(Scaling.stretch, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), new OrthographicCamera()),
new SpriteBatch());
ownsBatch = true;
}
So what you need is to cast your camera to OrthographicCamera:
((OrthographicCamera)stage.getCamera()).zoom += 0.02f;
private Texture druidTexture;
private SpriteBatch batch;
Sprite sprite;
#Override
public void create() {
druidTexture = new Texture(Gdx.files.internal("star-large.gif"));
sprite = new Sprite(druidTexture);
batch = new SpriteBatch();
}
#Override
public void dispose() {
}
#Override
public void pause() {
}
#Override
public void render() {
batch.begin();
sprite.rotate(45);
float x=sprite.getX();
float y=sprite.getY();
float newx=x+1;
System.out.println(newx);
float newy=y+1;
sprite.setX(newx);
sprite.draw(batch);
batch.end();
}
#Override
public void resize(int arg0, int arg1) {
}
#Override
public void resume() {
}
}
i am getting output for the above code as this
but i need to remove the star at back,for each star,how can i do that??
You need to clear the screen before each frame as described here:
public void render () {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // This cryptic line clears the screen.
batch.begin();
// Drawing goes here!
batch.end();
}
Clear the screen firstly in function render
#Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
.....
}