Here is an example of what it looks like when I drag a picture with the mouse
http://i.stack.imgur.com/4RQDU.jpg
And here is the code for it.
When I load the project on the desktop application, the picture displays perfectly in the middle, but when I move it with the mouse it drags itself like in Windows xp.
public class SlingshotSteve implements ApplicationListener{
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
#Override
public void create() {
camera = new OrthographicCamera(1280, 720);
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("image.png"));
sprite = new Sprite(texture);
sprite = new Sprite(texture);
sprite.setOrigin(0,0);
sprite.setPosition(-sprite.getWidth()/2,-sprite.getHeight()/2);
}
#Override
public void dispose() {
batch.dispose();
texture.dispose();
}
#Override
public void render() {
// Make the background color Black
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
// Mouse imput
if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
sprite.setPosition(Gdx.input.getX() - sprite.getWidth()/2,
Gdx.graphics.getHeight() - Gdx.input.getY() - sprite.getHeight()/2);
}
if(Gdx.input.isButtonPressed(Input.Buttons.RIGHT)){
sprite.setPosition(Gdx.graphics.getWidth()/2 -sprite.getWidth()/2,
Gdx.graphics.getHeight()/2 - sprite.getHeight()/2);
}
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.end();
}
I have no idea what I'm doing wrong
Related
The problem
When I move the Camera every texture that is getting rendered flickers about the screen on 2 different positions.
What I want
example: When I move the camera to the left, I want the all the textures to move 32 pixels to the right. camera moves 32 pixels per button press.
Current code
I added some extra explanation in comments.
MainProgramEntryPoint
/**
* DefaultCamera: Setting up OrthographicCamera
* CameraMovement: Using camera.translate on keypresses to move the screen.
* TestMoveable: Creating a texture for testing rendering.
*/
public class WorldSimGame extends ApplicationAdapter {
private DefaultCamera defaultCamera;
private CameraMovement cameraMovement;
private TestMoveable testMoveable;
// Setting up the camera and texture.
public void create () {
defaultCamera = new DefaultCamera();
cameraMovement = new CameraMovement(defaultCamera.getCamera());
testMoveable = new TestMoveable();
testMoveable.create(defaultCamera);
}
// The testMoveable.render(defaultCamera) should keep track of the testMoveable position
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
defaultCamera.render();
testMoveable.render(defaultCamera);
}
}
TestMoveable
public class TestMoveable {
private Texture tex;
private SpriteBatch batch;
private Vector3 position;
public void create(DefaultCamera defaultCamera) {
tex = new Texture(("wall.png"));
batch = new SpriteBatch();
position = new Vector3(100, 100, 0);
defaultCamera.getCamera().unproject(position);
}
I cant imagine setting the x and y coordinates on the world coordinates wouldn't work.
public void render(DefaultCamera defaultCamera) {
batch.begin();
batch.draw(tex, position.x, position.y);
batch.end();
}
}
What am I doing wrong here? And is there a better way to implement position checking for the renderers?
You don't need to check the position of the renderer. All what you must do is to set the size and position of your camera. Then with batch.setProjectionMatrix(camera.combined) you say the batch to draw what the camera sees.
So when you create a camera with size = 50x50 and position = 100,100
and you now create a Texture with size = 50x50 and position = 75,75
The Texture will perfectly fit the hole screen.
The position of camera is in the center. So the position of Texture is 75,75 and not 100,100
When you now will move your camera, you can use the method: translate() of your camera.
Call: camera.translate(25,0) to move your camera 25 units to the right and now you only see the half of your Texture on the left side of the screen.
This is an easy example of a Moveable camera. You can use the arrow keys to move around:
public class WorldSimGame extends ApplicationAdapter {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
public WorldSimGame() { }
#Override
public void create(){
//Create texture, batch and camera
texture = new Texture(Gdx.files.internal("badlogic.jpg"));
batch = new SpriteBatch();
camera = new OrthographicCamera(60,60);
}
#Override
public void render(){
//clear screen
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
moveCamera();
//update camera that he recalculate his position and Matrix
camera.update();
batch.setProjectionMatrix(camera.combined); //set batch to draw what the camera sees
batch.begin();
batch.draw(texture,0,0); //draw texture
batch.end();
}
private void moveCamera(){
//move camera
if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
camera.translate(4,0);
}
if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
camera.translate(-4,0);
}
if(Gdx.input.isKeyPressed(Input.Keys.UP)){
camera.translate(0,4);
}
if(Gdx.input.isKeyPressed(Input.Keys.DOWN)){
camera.translate(0,-4);
}
}
}
On my 854 x 480 LG Leon it renders fine, but on a larger screen resolution (2560 x 1440) this happens: http://imgur.com/a/rcbJK.
The code for processing the text is: http://imgur.com/a/c316e.
I've tried expanding the bounds of the Label because I thought that it might be constricting the text, but it still won't display properly.
What could be causing this?
The different mobile phone has the different aspect ratio in their screen size . that is the reason it shows different different screen. to avoid this problem you must use the viewPort. So you will be able to handle the different screen size. for more details, you must read the libgdx documentation. here I'm posting a sample code which can help you to handle the different screen size. there are three kinds of viewports mainly using
1) fillviewport
2) fitviewPort
3)stretchviewport
here is the documentation in detail.
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/viewport/FillViewport.html
public class myGame extends ApplicationAdapter {
public OrthographicCamera camera;
public Viewport viewPort;
private SpriteBatch batch;
public myGame() {
}
#Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.position.set(0, 0, 0);
camera.update();
camera.setToOrtho(false, 1280, 800);
viewPort = new FillViewport(1280, 800, camera);
}
#Override
public void dispose() {
batch.dispose();
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
float deltaTime = Gdx.graphics.getDeltaTime();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(sprie,0,0)
batch.end();
}
#Override
public void resize(int width, int height) {
viewPort.update(width, height);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
// don't copy paste it . just try to understand and implement it.
You should use Scene2D and ViewPort.
Also , be careful to use coordinates (i.e : x: 64 , y:32 changes in every different screen size)
This is the common issue when your running your game in multiple devices. Because your game is running in different aspect ratios in different screens . it's generally called the multi screen issue. To solve this problem the libgdx is providing it's on class called the viewPort. Mainly you can see three viewpors.
1)Fill viewPort.
2)Fit viewPort.
3)stretch viewport.
For more details you can go through the libgdx documentation. Here im posting some example code which can solve your multi screen issues.
public class myGame extends ApplicationAdapter {
public OrthographicCamera camera;
public Viewport viewPort;
private SpriteBatch batch;
private BitmapFont myScoreFont;
//General screen resolution
public int APP_WIDTH=1280;
public int APP_HEIGHT=800;
public int fontPositionIn_X=600;
public int fontPositionIn_Y=400;
public myGame() {
}
#Override
public void create() {
myScoreFont = new BitmapFont(Gdx.files.internal(Constants.PATH_TO_MY_SCORE_FONT), true);
batch = new SpriteBatch();
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.position.set(0, 0, 0);
camera.update();
camera.setToOrtho(false, APP_WIDTH, APP_HEIGHT);
viewPort = new fillViewPort(1280, 800, camera);
}
#Override
public void dispose() {
batch.dispose();
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
float deltaTime = Gdx.graphics.getDeltaTime();
batch.setProjectionMatrix(camera.combined);
batch.begin();
myScoreFont.draw(batch,"any texts", fontPositionIn_X, fontPositionIn_Y)
batch.end();
}
#Override
public void resize(int width, int height) {
viewPort.update(width, height);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
so by using the viewPort you can be able to play your game in all the different screens.
I am trying to make an object that extends the Actor class. but whenever I have call the Stage's draw() method, it only draws the button I added to it. Here is my code, maybe someone here can help me?
Code for my Screen class (Assume that I had overridden all necessary methods and that I imported all necessary classes):
public class PlayScreen implements Screen{
SpriteBatch batch;
Game game;
Table table;
Skin skin;
Stage stage;
BitmapFont font;
TextButton button;
TextDisplay display;
public PlayScreen(MyGdxGame game, SpriteBatch batch){
this.game = game;
this.batch = batch;
//instantiating Stage, Table, BitmapFont, and Skin objects.
font = new BitmapFont(Gdx.files.internal("MyTruefont.fnt"));
table = new Table();
stage = new Stage();
skin = new Skin(Gdx.files.internal("Test.json"));
button = new TextButton("Start!", skin, "default");
display = new TextDisplay(font, this);
//Setting table properties
table.setWidth(stage.getWidth());
table.align(Align.center|Align.top);
table.setPosition(0, Gdx.graphics.getHeight());
table.padTop(30);
//Adding actors to table
table.add(button).padBottom(50);
table.add(display);
//Adding table to stage and setting stage as the input processor
stage.addActor(table);
Gdx.input.setInputProcessor(stage);
}
#Override
public void show() {
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
stage.draw();
batch.end();
}
And here is My Textdisplay class that extends Actor (Again assume all other methods have been overriden, and all necessary classes have been imported):
public class TextDisplay extends Actor implements Drawable {
Texture texture;
BitmapFont font;
String str;
public TextDisplay(BitmapFont font, Screen screen){
this.font = font;
texture = new Texture(Gdx.files.internal("TextArea.png"));
str = "";
setLeftWidth(texture.getWidth());
setRightWidth(texture.getWidth());
setBottomHeight(texture.getHeight());
setTopHeight(texture.getHeight());
}
#Override
public void draw(Batch batch, float x, float y, float width, float height) {
batch.draw(texture, x, y);
}
You overrode the wrong draw method. Actor's draw method signature is:
public void draw (Batch batch, float parentAlpha)
You overrode the one from Drawable. Not sure why you're implementing Drawable. There's already an Actor available for drawing text anyway, called Label.
Also, if you want to put your Actor in a table, you need to set its width and height, or the width and height of its cell in the table.
I am using libgdx to make a card game but I have hit a deadlock.
Touches or clicks on sprites are not being detected.
There is extra space that appears on my board which is not there in the
tiled map file.
I made a simple project to illustrate my problem.
A compilable project is here that shows the code in its enterity https://dl.dropboxusercontent.com/u/51343591/card.zip
public class Card extends ApplicationAdapter {
SpriteBatch batch;
TmxMapLoader tmxMapLoader;
TiledMap tiledMap;
OrthogonalTiledMapRenderer mapRenderer;
OrthographicCamera camera;
Viewport gamePort;
Sprite cardSprite;
Sprite dotSprite;
Vector3 screenPointTouched;
#Override
public void create () {
batch = new SpriteBatch();
tmxMapLoader=new TmxMapLoader();
tiledMap= tmxMapLoader.load("level1.tmx");
mapRenderer=new OrthogonalTiledMapRenderer(tiledMap);
float worldwidth=19*16;//19tiles*16pixelspertile. This is what its like on the level1.tmx file
float worldheight=12*16;//12tiles*16pixelspertile. This is what its like on the level1.tmx file
gamePort= new StretchViewport(worldwidth,worldheight,camera);//new StretchViewport(,
camera= new OrthographicCamera(10,10*(Gdx.graphics.getWidth()/Gdx.graphics.getHeight()));
camera.position.set(gamePort.getWorldWidth()/2,gamePort.getWorldHeight()/2,0);//center the camera
cardSprite=new Sprite(new Texture("badlogic.jpg"));
dotSprite=new Sprite(new Texture("dot.png"));
screenPointTouched=new Vector3();
positionCardSpriteFromFirstRectangle();
}
#Override
public void render () {
update();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
mapRenderer.render();
batch.begin();
cardSprite.draw(batch);
dotSprite.draw(batch);
batch.end();
}
private void positionCardSpriteFromFirstRectangle() {
MapObject mo=tiledMap.getLayers().get("cardlayer").getObjects().get(0);//get the firstone onlys
Rectangle rect=((RectangleMapObject)mo).getRectangle();
cardSprite.setBounds(rect.x,rect.y,rect.width,rect.height);
}
public void update() {
camera.update();
mapRenderer.setView(camera);
if(Gdx.input.isTouched()){//handle touch event
screenPointTouched.set(Gdx.input.getX(),Gdx.input.getY(),0);
screenPointTouched=camera.unproject(screenPointTouched);
if(cardSprite.getBoundingRectangle().contains(screenPointTouched.x,screenPointTouched.y)){
System.out.println("being touched");
dotSprite.setPosition(screenPointTouched.x,screenPointTouched.y);
}
}
}
}
Basically, I want my coordinate system to have (0,0) as the top left corner of the screen(used to this in Swing and Android Canvas drawing).
I saw a great thread for doing so Changing the Coordinate System in LibGDX (Java). I used the code that the creator provided but the rectangle I am drawing is still at the bottom of the screen.(screenshot)
Here is my code for drawing the rectangle(I use one class, GameWorld, to manage all the "game objects" and another class, GameRenderer to render all the game objects) Both classes have a method that will be called from the GameScreen's render method.
Relevant code in GameScreen.java
public class GameScreen implements Screen {
//manage game objects
private GameWorld world;
//render game objects
private GameRenderer renderer;
public GameScreen() {
world = new GameWorld();
renderer = new GameRenderer(world);
}
public void render(float delta) {
world.update(delta);
renderer.render();
}
.....
}
And in GameWorld.java
public class GameWorld {
private Rectangle rectangle;
public GameWorld() {
rectangle = new Rectangle(0, 0, 17, 12);
}
public void update(float delta) {
rectangle.setX(rectangle.getX() + 1);
if(rectangle.getX() > Gdx.graphics.getWidth()) {
rectangle.setX(0);
}
}
public Rectangle getRect() {
return rectangle;
}
}
And in GameRenderer.java
public class GameRenderer {
private ShapeRenderer shapeRenderer;
private OrthographicCamera cam;
private GameWorld myWorld;
public GameRenderer(GameWorld theWorld) {
myWorld = theWorld;
cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
public void render() {
shapeRenderer = new ShapeRenderer();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(87/255.0f, 109/255.0f, 120/255.0f, 1);
Rectangle toDraw = myWorld.getRect();
shapeRenderer.rect(toDraw.getX(), toDraw.getY(),
toDraw.getWidth(), toDraw.getHeight());
shapeRenderer.end();
}
}
And finally in Desktop.java where I am testing the game,
public class DesktopLauncher {
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.title = "My test";
config.width = 272;
config.height = 408;
new LwjglApplication(new MyGdxGame(), config);
}
}
Does anyone see where i am doing wrong? Why is the rectangle still being drawn at the bottom of the screen. I used the provided code from the creator and specified the rectangle's top y coordinate as zero.
You didn't apply your camera's projection to the ShapeRenderer.
Add this line right before shapeRenderer.begin():
shapeRenderer.setProjectionMatrix(cam.combined);
Also, you should move ShapeRenderer's instantiation from render() to the class constructor so you aren't creating a new one on every frame.