Reputation: 697
I am barely starting up on developing android games. i am trying to animate my sprite and i have done that successfully, but now that i draw by buttons so i can start making my player move, they are not in the right position when they should be. could anyone help me? It looks great in the desktop version, but not on my android phone.
this is my code for the main class.
package com.Adventcloud.RealGame;
public class RealGame implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
//private Texture background;
private AnimatedSprite playerAnimated;
//Buttons
public static Texture leftButton;
public static Texture rightButton;
public static Texture shootButton;
public static Texture jumpButton;
@Override
public void create() {
int width = Gdx.graphics.getWidth();
int height = Gdx.graphics.getHeight();
Texture.setEnforcePotImages(false);
camera = new OrthographicCamera();
camera.setToOrtho(false, 1280, 720);
leftButton = new Texture(Gdx.files.internal("leftButton.png"));
rightButton = new Texture(Gdx.files.internal("rightButton.png"));
shootButton = new Texture(Gdx.files.internal("shootButton.png"));
jumpButton = new Texture(Gdx.files.internal("jumpButton.png"));
batch = new SpriteBatch();
//background = new Texture(Gdx.files.internal(""));
Texture spaceshipTexture = new Texture(Gdx.files.internal("spritesheet.png"));
Sprite playerSprite = new Sprite(spaceshipTexture);
playerAnimated = new AnimatedSprite(playerSprite);
playerAnimated.setPosition(1280/2, 0);
}
@Override
public void dispose() {
batch.dispose();
}
@Override
public void render() {
Gdx.gl.glClearColor(0.95f, 0.95f, 0.95f, 0.95f);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
//batch.draw(background, 0, 0);
playerAnimated.draw(batch);
batch.draw(leftButton, 0, 0, Gdx.graphics.getWidth()/4, Gdx.graphics.getHeight()/6);
batch.draw(rightButton, Gdx.graphics.getWidth()/4, 0, Gdx.graphics.getWidth()/4, Gdx.graphics.getHeight()/6);
batch.draw(shootButton, (Gdx.graphics.getWidth()/4)*2, 0, Gdx.graphics.getWidth()/4, Gdx.graphics.getHeight()/6);
batch.draw(jumpButton, (Gdx.graphics.getWidth()/4)*3, 0, Gdx.graphics.getWidth()/4, Gdx.graphics.getHeight()/6);
batch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}
here is my code for my animated sprite
package com.Adventcloud.RealGame;
public class AnimatedSprite {
private static final int FRAME_COLS = 16;
private static final int FRAME_ROWS = 16;
private static final int IDLE_COLS = 4;
private static final int IDLE_ROWS = 1;
private Sprite sprite;
private Animation animation;
private TextureRegion[] frames;
private TextureRegion currentFrame;
private float stateTime;
public AnimatedSprite(Sprite sprite){
this.sprite = sprite;
Texture texture = sprite.getTexture();
TextureRegion[][] temp = TextureRegion.split(texture, texture.getWidth() / FRAME_COLS, texture.getHeight() / FRAME_ROWS);
frames = new TextureRegion[IDLE_COLS * IDLE_ROWS];
//Animation for idling ======================================
int index = 0;
for (int i = 0; i < IDLE_ROWS; i++) {
for (int j = 1; j <= IDLE_COLS; j++) {
frames[index++] = temp[i][j];
}
}
animation = new Animation(0.2f, frames);
stateTime = 0f;
//end of idle animation ====================================
}
public void draw(SpriteBatch spriteBatch) {
stateTime += Gdx.graphics.getDeltaTime();
//continue to keep looping
currentFrame = animation.getKeyFrame(stateTime, true);
spriteBatch.draw(currentFrame, sprite.getX() - 32, sprite.getY() + 128, 128, 128);
}
public void setPosition(float x, float y){
float widthOffset = sprite.getWidth() / FRAME_COLS;
sprite.setPosition(x- widthOffset / 2, y);
}
}
Upvotes: 2
Views: 8067
Reputation: 9783
This is actually what the camera was made for (at least one of the things). You can use a custom viewport and it will be scaled up to fit the screen/window.
For example if you want to have an aspect ration of 16/9 and your screen should be 80 "worldunits" (maybe meters?) wide so use a viewport of 80 width and 45 height.
So use camera = new OrthographicCamera(80, 45);
This creates a camera with 80 units in the width, 45 in the height and the P(0,0) in the middle.
By doing this a Texture
, with a size of 1
, is 20px
wide and 20px
high on a 1600*900px
screen, and 10px
wide and 10px
high on a 800*450px
screen.
Drawing an object at the coordinates P(0,0) draws its left lower corner in the middle of the screen, drawing at P(-40, 0) draws it at the left border.
Do not forget to tell the SpriteBatch
to use this camera:
spriteBatch.setProjectionMatrix(camera.combined);
To move the camera use camera.translate(deltaX, deltaY);
If you move the camera or rotate it or do something else, what changes its matrix don't forget to call camera.update()
.
Now you can calculate everything in "worldunits" and it will be scaled up to fit the screen. Hope i could help
Upvotes: 3