GammelJAN 5 years ago
commit da93e763f2

@ -0,0 +1,121 @@
package com.trs.main;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
/**
*
* @author jonathan
*/
public class AnimatedSprite {
private Sprite sprite;
private TextureRegion[][] texture;
private int frame;
private int row;
private float delta;
public AnimatedSprite(Texture tx, int tileWidth, int tileHeight){
texture = TextureRegion.split(tx, tileWidth, tileHeight);
row = (int) (Math.random()*texture.length);
frame = (int) (Math.random()*texture[row].length);
sprite = new Sprite(texture[getRow()][getFrame()]);
}
public void updateAnimation(float delta){
this.delta += delta;
if(this.delta >= 0.2f) {
this.delta = 0;
if(getFrame() >= texture[getRow()].length - 1){
setFrame(0);
}
else{
setFrame(getFrame() + 1);
}
sprite.setRegion(texture[getRow()][getFrame()]);
}
}
/*
public void updateBackwards(){
if(getFrame() <= 0){
setFrame(texture[0].length - 1);
}
else{
setFrame(getFrame() - 1);
}
sprite.setRegion(texture[getRow()][getFrame()]);
}
*/
public void draw(Batch batch) {
sprite.draw(batch);
}
/**
* @return the sprite
*/
public Sprite getSprite() {
return sprite;
}
/**
* @param sprite the sprite to set
*/
public void setSprite(Sprite sprite) {
this.sprite = sprite;
}
public void setSpritePosition(int xPos, int yPos){
sprite.setPosition(xPos, yPos);
}
/**
* @return the texture
*/
public TextureRegion[][] getTexture() {
return texture;
}
/**
* @param texture the texture to set
*/
public void setTexture(TextureRegion[][] texture) {
this.texture = texture;
}
/**
* @return the frame
*/
public int getFrame() {
return frame;
}
/**
* @param frame the frame to set
*/
public void setFrame(int frame) {
this.frame = frame;
}
/**
* @return the row
*/
public int getRow() {
return row;
}
/**
* @param row the row to set
*/
public void setRow(int row) {
this.row = row;
}
}

@ -2,11 +2,7 @@ package com.trs.main;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.trs.main.view.screens.GameScreen;
public class Main extends Game{

@ -21,11 +21,13 @@ import com.badlogic.gdx.scenes.scene2d.Actor;
public class Player extends Actor{
Texture t;
private AnimatedSprite playerSprite;
public Player(int xPos, int yPos){
setName("player");
t = new Texture(Gdx.files.internal("badlogic.jpg"));
setBounds(xPos, yPos, t.getWidth(), t.getHeight());
playerSprite = new AnimatedSprite(t, 64, 64);
}
@Override
@ -49,12 +51,14 @@ public class Player extends Actor{
if(Gdx.input.isKeyPressed(Input.Keys.S)){
setY(getY()-10);
}
playerSprite.updateAnimation(delta);
super.act(delta); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void draw(Batch batch, float parentAlpha) {
batch.draw(t, getX(), getY());
playerSprite.draw(batch);
super.draw(batch, parentAlpha); //To change body of generated methods, choose Tools | Templates.
}

Loading…
Cancel
Save