Good day everybody I'm new to this framework LibGdx. I need advice/suggestion for my game.Is it okay if I load my images in a single class? I'm a beginner and start learning LibGdx. I already coded my game screen and it works perfectly as I expected. My concern is how about the performance of this in other device? I don't know how to implement Assetmanager
.
Here is my code it works fine but I think it is not the proper way of loading images. Any suggestion help are much appreciated :)
public class IngamedayOne implements Screen ,InputProcessor {
final MyGdxGame game;
// Constant rows and columns of the sprite sheet
private static final int FRAME_COLS = 5, FRAME_ROWS = 1;
private boolean peripheralAvailable;
private static final float ACCELERATION = 20f;
// Objects used
Animation<TextureRegion> walkAnimation; // Must declare frame type (TextureRegion)
Texture left_paw,right_paw,progressbar_background,progressbar_knob,pause,meter,open_box;
Texture carpet,desk,plants,square_carpet,pauseImg,pause_cancel,retry,dayselect_img,household_select,back_to_main_menu;
SpriteBatch spriteBatch;
Sprite catsprite,sprite_shoes,sprite_plant,sprite_ball,dogsprite;
private Texture Background1,Background2,Background3;
float yMax, yCoordBg1, yCoordBg2,yCoordDesk;
final int BACKGROUND_MOVE_SPEED = -200;
ImageButton left_paw_btn,right_paw_btn,pause_btnDialog,pause_btn_exit,retry_btn,dayselect_btn,select_household,back_to_main_menu_btn;
Image pauseImg_text;
Viewport viewport;
private Stage stage;
// A variable for tracking elapsed time for the animation
float stateTime;
private TextureRegion myTextureRegion;
TextureRegion textureRegion;
private TextureRegionDrawable myTexRegionDrawable;
private boolean isPause;
private Group pauseGroup;
Texture cat,dog;
private float yPosition = -40;
//Screen Size
OrthographicCamera camera;
float catSpeed = 50.0f; // 10 pixels per second.
float catX;
float catY;
float x=0,y=0;
public IngamedayOne(final MyGdxGame game) {
this.game = game;
Gdx.input.setCatchBackKey(false);
Gdx.graphics.setContinuousRendering(false);
Gdx.graphics.requestRendering();
stage = new Stage(new StretchViewport( 720, 1280));
camera = new OrthographicCamera(1280 ,720);
camera.update();
camera.setToOrtho(false, 720, 1280);
camera.translate( 1280/2, 720/2 );
Gdx.input.setInputProcessor(stage);
spriteBatch = new SpriteBatch();
viewport = new StretchViewport(720, 1280);
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
// Load the sprite sheet as a texture
cat = new Texture(Gdx.files.internal("cat.png"));
catsprite = new Sprite(cat);
catX=300;
catY=0;
dog = new Texture(Gdx.files.internal("equip/Dog.png"));
dogsprite = new Sprite(dog);
dogsprite.setPosition(20,580);
Gdx.input.setInputProcessor( this);
peripheralAvailable = Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer);
int orientation = Gdx.input.getRotation();
Input.Orientation nativeOrientation = Gdx.input.getNativeOrientation();
viewport = new StretchViewport(720, 1280);
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
//Display Items
carpet = new Texture("equip/carpet2.png");
desk = new Texture("equip/Desk.png");
open_box = new Texture("equip/open box.png");
square_carpet = new Texture("equip/Carpet.png");
plants = new Texture("equip/Plants.png");
sprite_plant = new Sprite(plants);
sprite_shoes = new Sprite(new Texture("equip/Shoes.png"));
sprite_shoes.setPosition(260,580);
sprite_ball = new Sprite(new Texture("equip/Basketball.png"));
sprite_ball.setPosition(260,300);
// Progressbar
progressbar_background = new Texture("progression_map.png");
progressbar_knob = new Texture("cat_head.png");
//pause
pause = new Texture("pause.png");
myTextureRegion = new TextureRegion(pause);
myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
pause_btnDialog = new ImageButton(myTexRegionDrawable); //Set the button up
pause_btnDialog.setPosition(580,1150);
stage.addActor(pause_btnDialog); //Add the button to the stage to perform rendering and take input.
Gdx.input.setInputProcessor(stage);
pause_btnDialog.addListener(new ChangeListener() {
public void changed (ChangeEvent event, Actor actor) {
System.out.println("Pause Button Pressed");
//Show Pause Screen menu
pause();
}
});
stage.addActor(pause_btnDialog);
meter = new Texture("meter.png");
//background
Background1 = new Texture(Gdx.files.internal("floor.png"));
Background2 = Background1;
yMax = -1270;
yCoordBg1 = yMax*(-1);
yCoordBg2 = 0;
yCoordDesk = 0;
// Use the split utility method to create a 2D array of TextureRegions. This is
// possible because this sprite sheet contains frames of equal size and they are
// all aligned.
TextureRegion[][] tmp = TextureRegion.split(cat, cat.getWidth() / FRAME_COLS, cat.getHeight()/ FRAME_ROWS);
// Place the regions into a 1D array in the correct order, starting from the top
// left, going across first. The Animation constructor requires a 1D array.
TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
walkFrames[index++] = tmp[i][j];
}
}
// Initialize the Animation with the frame interval and array of frames
walkAnimation = new Animation<TextureRegion>(0.200f, walkFrames);
// Instantiate a SpriteBatch for drawing and reset the elapsed animation
// time to 0
spriteBatch = new SpriteBatch();
stateTime = 0f;
//left_control
left_paw = new Texture(Gdx.files.internal("left_paw.png"));
myTextureRegion = new TextureRegion(left_paw);
myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
left_paw_btn = new ImageButton(myTexRegionDrawable); //Set the button up
left_paw_btn.setPosition(10,25);
stage.addActor(left_paw_btn); //Add the button to the stage to perform rendering and take input.
Gdx.input.setInputProcessor(stage);
left_paw_btn.addListener(new InputListener(){
@Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
System.out.println("Left Button Pressed");
//Start Animation
}
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}
});
stage.addActor(left_paw_btn);
//right_control
right_paw = new Texture(Gdx.files.internal("right_paw.png"));
myTextureRegion = new TextureRegion(right_paw);
myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
right_paw_btn = new ImageButton(myTexRegionDrawable); //Set the button up
right_paw_btn.setPosition(517,25);
stage.addActor(right_paw_btn); //Add the button to the stage to perform rendering and take input.
Gdx.input.setInputProcessor(stage);
right_paw_btn.addListener(new InputListener(){
@Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
System.out.println("Right Button Pressed");
//Start Animation
stateTime += Gdx.graphics.getDeltaTime(); // Accumulate elapsed animation time
camera.update();
}
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}
});
stage.addActor(right_paw_btn);
}
public enum State
{
PAUSE,
RUN,
RESUME,
STOPPED
}
private State state = State.RUN;
@Override
public void show() {
}
@Override
public void render(float delta) {
// clear previous frame
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear screen
stateTime += Gdx.graphics.getDeltaTime(); // Accumulate elapsed animation time
yCoordBg1+= BACKGROUND_MOVE_SPEED * Gdx.graphics.getDeltaTime();
yCoordBg2 = yCoordBg1 + yMax; // We move the background, not the camera
yCoordDesk = BACKGROUND_MOVE_SPEED * Gdx.graphics.getDeltaTime();
if (yCoordBg1 <= 2) {
yCoordBg1 = yMax*(-1);
yCoordBg2 = 0;
}
spriteBatch.begin();
if(this.state==State.RESUME);
stateTime += Gdx.graphics.getDeltaTime();
TextureRegion currentFrame = walkAnimation.getKeyFrame(stateTime, true);
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.draw(Background1, 0,yCoordBg1);
spriteBatch.draw(Background2, 0,yCoordBg2);
//spriteBatch.draw(square_carpet,150,2,408,800);
//spriteBatch.draw(carpet,230,980,250,260);
//sprite_shoes.draw(spriteBatch);
spriteBatch.draw(open_box,490,690);
sprite_ball.draw(spriteBatch);
dogsprite.draw(spriteBatch);
spriteBatch.draw(currentFrame,catX,catY); // Draw current frame at (50, 50)
// spriteBatch.draw(desk,10,1150,160,260);
//spriteBatch.draw(plants,500,700,200,260);
spriteBatch.draw(meter,190,990);
spriteBatch.draw(progressbar_background,20,1170);
spriteBatch.draw(progressbar_knob,18,1170);
//Moving player on desktop
if (Gdx.input.isKeyPressed(Input.Keys.LEFT))
catX -= Gdx.graphics.getDeltaTime() * catSpeed;
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT))
catX += Gdx.graphics.getDeltaTime() * catSpeed;
if (Gdx.input.isKeyPressed(Input.Keys.UP))
catY += Gdx.graphics.getDeltaTime() * catSpeed;
if (Gdx.input.isKeyPressed(Input.Keys.DOWN))
catY -= Gdx.graphics.getDeltaTime() * catSpeed;
//Mobile acceleration
if (Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer)) {
catX -= Gdx.input.getAccelerometerX();
catY += Gdx.input.getAccelerometerY();
}
if (catY > Gdx.graphics.getHeight() - 100) {
catY = Gdx.graphics.getHeight() - 100;
}
if (catX < 0) {
catX = 0;
}
if (catX > Gdx.graphics.getHeight() - 250) {
catX = Gdx.graphics.getHeight() - 250;
}
if(this.state==State.RESUME) {
switch (state) {
case RUN:
//do suff here
break;
case PAUSE:
break;
case RESUME:
break;
default:
break;
}
}
spriteBatch.end();
stage.act(); //acting a stage to calculate positions of actors etc
stage.draw(); //drawing it to render all
}
// Check if Polygon intersects Rectangle
// Check if Polygon intersects Circle
private boolean isCollision(Polygon polygon, Circle c) {
float[] vertices = polygon.getTransformedVertices();
Vector2 center = new Vector2(c.x, c.y);
float squareRadius = c.radius * c.radius;
for (int i = 0; i < vertices.length; i += 2) {
if (i == 0) {
if (Intersector.intersectSegmentCircle(new Vector2(
vertices[vertices.length - 2],
vertices[vertices.length - 1]), new Vector2(
vertices[i], vertices[i + 1]), center, squareRadius))
return true;
} else {
if (Intersector.intersectSegmentCircle(new Vector2(
vertices[i - 2], vertices[i - 1]), new Vector2(
vertices[i], vertices[i + 1]), center, squareRadius))
return true;
}
}
return false;
}
@Override
public void resize(int width, int height) {
viewport.update(width, height);
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
}
@Override
public void pause() {
this.state = State.PAUSE;
pauseGroup = new Group();
Image semiTransparentBG=new Image(new Texture(Gdx.files.internal("backgroundimage.png")));
// setSize(Size of screen) and make it semi transparent.
pauseGroup.addActor(semiTransparentBG);
//create all other pause UI buttons with listener and add to pauseGroup
//cancel
pause_cancel = new Texture(Gdx.files.internal("pausemenu/exit.png"));
myTextureRegion = new TextureRegion(pause_cancel);
myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
pause_btn_exit = new ImageButton(myTexRegionDrawable); //Set the button up
pause_btn_exit.setPosition(550,1130);
stage.addActor(pause_btn_exit); //Add the button to the stage to perform rendering and take input.
Gdx.input.setInputProcessor(stage);
pause_btn_exit.addListener(new InputListener(){
@Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
System.out.println("Cancel Button Pressed");
resume();
}
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}
});
stage.addActor(pause_btn_exit);
pauseGroup.addActor(pause_btn_exit);
pauseImg = new Texture(Gdx.files.internal("pausemenu/pause_text.png"));
myTextureRegion = new TextureRegion(pauseImg);
myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
pauseImg_text = new Image(myTexRegionDrawable); //Set the button up
pauseImg_text.setPosition(260,970);
pauseGroup.addActor(pauseImg_text);
//retry
retry = new Texture(Gdx.files.internal("pausemenu/retry.png"));
myTextureRegion = new TextureRegion(retry);
myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
retry_btn = new ImageButton(myTexRegionDrawable); //Set the button up
retry_btn.setPosition(200,700);
stage.addActor(retry_btn); //Add the button to the stage to perform rendering and take input.
Gdx.input.setInputProcessor(stage);
retry_btn.addListener(new InputListener(){
@Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
System.out.println("Button Pressed");
//resume
game.setScreen(new IngamedayOne(game));
}
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}
});
stage.addActor(retry_btn);
pauseGroup.addActor(retry_btn);
//Day Select
dayselect_img = new Texture(Gdx.files.internal("pausemenu/day_select.png"));
myTextureRegion = new TextureRegion(dayselect_img);
myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
dayselect_btn = new ImageButton(myTexRegionDrawable); //Set the button up
dayselect_btn.setPosition(180,490);
stage.addActor(dayselect_btn); //Add the button to the stage to perform rendering and take input.
Gdx.input.setInputProcessor(stage);
dayselect_btn.addListener(new InputListener(){
@Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
System.out.println(" Button Pressed");
game.setScreen(new SelectDiffculty(game));
}
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}
});
stage.addActor(dayselect_btn);
pauseGroup.addActor(dayselect_btn);
//House Select
household_select = new Texture(Gdx.files.internal("pausemenu/household_select.png"));
myTextureRegion = new TextureRegion(household_select);
myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
select_household = new ImageButton(myTexRegionDrawable); //Set the button up
select_household.setPosition(180,300);
stage.addActor(select_household); //Add the button to the stage to perform rendering and take input.
Gdx.input.setInputProcessor(stage);
select_household.addListener(new InputListener(){
@Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
System.out.println(" Button Pressed");
//resume
game.setScreen(new SelectHouseHold(game));
}
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}
});
stage.addActor(select_household);
pauseGroup.addActor(select_household);
//Back to main menu
back_to_main_menu = new Texture(Gdx.files.internal("pausemenu/back_to_main_menu.png"));
myTextureRegion = new TextureRegion(back_to_main_menu);
myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
back_to_main_menu_btn = new ImageButton(myTexRegionDrawable); //Set the button up
back_to_main_menu_btn.setPosition(40,80);
stage.addActor(back_to_main_menu_btn); //Add the button to the stage to perform rendering and take input.
Gdx.input.setInputProcessor(stage);
back_to_main_menu_btn.addListener(new InputListener(){
@Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
System.out.println(" Button Pressed");
//resume
game.setScreen(new MainScreen(game));
}
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}
});
stage.addActor(back_to_main_menu_btn);
pauseGroup.addActor(back_to_main_menu_btn);
stage.addActor(pauseGroup);
}
@Override
public void resume() {
this.state = State.RESUME;
pauseGroup.remove();
}
@Override
public boolean keyDown(int keycode) {
return true;
}
@Override
public boolean keyUp(int keycode) {
return false;
}
@Override
public boolean keyTyped(char character) {
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
@Override
public boolean scrolled(int amount) {
return false;
}
@Override
public void hide() {
}
@Override
public void dispose() { // SpriteBatches and Textures must always be disposed
}
}