Drawing partial sprites in Java using the TextureRegion (LibGDX) - java

I currently have the class for my main character "Student", it has no behaviors except the left and right movements. I managed to make it so all the frames of my spritesheet will render, so I need help in drawing the first 3 frames (which are the walk cycle) when I press LEFT / RIGHT keys.
Here's the spritesheet: http://imgur.com/a/HHdm9
Edit: AND 2nd and 3rd rows when pressing UP and Down Keys.
Student Class
public class Student {
private Texture player;
private Animation<TextureRegion> playerAnimation;
private int pX;
private int pY;
private SpriteBatch batch;
private int HP;
public Student(float speed, int x, int y){
player = new Texture("student.png");
TextureRegion[][] tmp= TextureRegion.split(player,450/3,450/3);
TextureRegion[] character = new TextureRegion[9];
int index = 0;
for(int i=0; i <3; i++){
for(int j=0; j < 3; j++){
character[index++] = tmp[i][j];}
}
playerAnimation = new Animation<TextureRegion>(speed,character);
pX=x;
pY=y;
}
public SpriteBatch getBatch() {
return batch;
}
public void setBatch(SpriteBatch batch) {
this.batch = batch;
}
public void goLeft(){
pX-=5;
if(pX < -10){
pX = -10;
}
}
public void goRight(){
pX+=5;
}
public void renderStudent(float stateTime){
TextureRegion currentFrame = playerAnimation.getKeyFrame(stateTime,true);
batch.draw(currentFrame, (float) pX, (float) pY, 0, 0, currentFrame.getRegionWidth(),
currentFrame.getRegionHeight(),1.5f,1.5f,0);
}
public void dispose(){
player.dispose();
}
}
Main Class
public class HaxMain extends ApplicationAdapter {
SpriteBatch batch;
Texture bg;
float stateTime;
private Student student1;
private Guard mguard, fguard;
private Admin madmin, fadmin;
#Override
public void create () {
batch = new SpriteBatch();
bg = new Texture("Level1.png");
student1 = new Student(.5f, 100, 0);
student1.setBatch(batch);
}
#Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stateTime += Gdx.graphics.getDeltaTime();
if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
student1.goLeft();
//Code to render image to the left
}else if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
//Code to render image to the right
student1.goRight();
}
batch.begin(); /*default code*/
batch.draw(bg, 0, 0);
student1.renderStudent(stateTime);
batch.end();
}
#Override
public void dispose () {
batch.dispose();
bg.dispose();
student1.dispose();
}
}

You need to create Animation only for Running and use that animation to show your player is running left or right. You can flip TextureRegion according to direction of your player like this :
TextureRegion[] character;
boolean isRight;
public Student(float speed, int x, int y){
player = new Texture("student.png");
TextureRegion[][] tmp= TextureRegion.split(player,450/3,450/3);
character = new TextureRegion[9];
int index = 0;
for(int i=0; i <3; i++){
for(int j=0; j < 3; j++){
character[index++] = tmp[i][j];}
}
TextureRegion runningFrames[]=new TextureRegion[3];
for (int i=0;i<runningFrames.length;i++)
runningFrames[i]= character[i];
playerAnimation = new Animation<TextureRegion>(speed, runningFrames);
playerAnimation.setPlayMode(Animation.PlayMode.LOOP);
pX=x;
pY=y;
}
public void goLeft(){
pX-=5;
if(pX < -10){
pX = -10;
}
if(isRight){
isRight=false;
for (TextureRegion textureRegion:playerAnimation.getKeyFrames())
if(textureRegion.isFlipX()) textureRegion.flip(true,false);
}
}
public void goRight(){
pX+=5;
if(!isRight){
isRight=true;
for (TextureRegion textureRegion: playerAnimation.getKeyFrames())
if(!textureRegion.isFlipX()) textureRegion.flip(true,false);
}
}
Create another animation of other action and draw frame of animation using flag.

Related

How do I reposition a moving animation during a gameplay?

I am using the Android Studio IDE, with the libGDX framework to develop a game. It is a clone of the Pac-Man game with a similar gameplay to flappy bird. The concept is that the pac man moves through the planks while avoiding the ghosts coming from the right direction, that are moving off straight into the left direction (not chasing the position of the player). I am not sure how I create a 'for loop' for the ghost animations, I want the ghosts to consistently reposition and re-appear from the right side after a few seconds, with the exception that they completely gone off the screen initially.
The class for one of the Ghosts.
public class Blinky {
private Vector3 position; //x y and z axis
private Rectangle bounds;
private Texture texture1;
private Animation blinkyAnimLeft;
public Blinky(int x, int y) {
position = new Vector3(x, y, 0);
Texture texture1 = new Texture("blinkyLeft.png");
blinkyAnimLeft = new Animation(new TextureRegion(texture1), 2, 0.5f);
//bounds = new Rectangle(x,y,texture1.getWidth() / 2, texture1.getHeight());
}
public void update(float dt) {
blinkyAnimLeft.update(dt);
//bounds.setPosition(position.x, position.y);
}
public Vector3 getPosition() {
return position;
}
public TextureRegion getTexture() {
return blinkyAnimLeft.getFrame();
}
//public Rectangle getBounds() {
return bounds;
}
public void dispose() {
texture1.dispose();
}
}
Ghosts and Player initialised in the GamePlayState Class
public class GamePlayState extends State {
//Variables
private float timePassed = 0;
private Texture background;
public static final int WALK = 1;
public static final double GHOST_WALK = 0.5;
private static final int PLANKS_SPACING = 125; //gap betwen the planks
private static final int PLANK_COUNT = 4;
private Array<Obstacle> planks;
private Player player;
private Blinky blinky;
private Inky inky;
private Texture missile;
public GamePlayState(GameStateManager gsm) {
super(gsm);
player = new Player(50, 100);
blinky = new Blinky(400, 220);
inky = new Inky(400, 240);
// missile = new Texture("missile.png");
background = new Texture("black.jpg");
cam.setToOrtho(false, PacMan.WIDTH/2, PacMan.HEIGHT/2);
planks = new Array<Obstacle>();
for (int i = 1; i<= PLANK_COUNT; i++) {
planks.add(new Obstacle(i * (PLANKS_SPACING + Obstacle.PLANK_WIDTH)));
}
}
#Override
public void handleInput() {
}
#Override
public void update(float dt) {
handleInput();
player.update(dt);
blinky.update(dt);
inky.update(dt);
cam.position.x = player.getPosition().x + 80; //update the position of the camera with the bird
//update when pacman cam viewport has passed plank
//make cam follow the player
for (Obstacle plank: planks) {
if (cam.position.x - (cam.viewportWidth/1) > plank.getPosTopPlank().x + plank.getTopPlank().getWidth()) {
plank.respositionPlanks(plank.getPosTopPlank().x + ((Obstacle.PLANK_WIDTH + PLANKS_SPACING * PLANK_COUNT )));
}
if (plank.collision (player.getBounds()))
gsm.set(new GamePlayState(gsm));
}
cam.update();
}
#Override
public void render(SpriteBatch sb) {
sb.setProjectionMatrix(cam.combined);
sb.begin();
sb.draw(background, cam.position.x - (cam.viewportWidth/2), 0);
timePassed += Gdx.graphics.getDeltaTime();
//Moving Inky
sb.draw(inky.getTexture(), inky.getPosition().x, inky.getPosition().y);
inky.getPosition().x -= GHOST_WALK;
//Moving Blinky
sb.draw(blinky.getTexture(), blinky.getPosition().x, blinky.getPosition().y);
blinky.getPosition().x -= GHOST_WALK;
You should get x, y( and z) position from Vector3.
Then check for borders
if (inky.getPosition().x < 0){
Thread.sleep(/*a few sdeconds*/2000);/*Android studio would ask you about wrapping it with try - catch*/
inky.setPostion( x + /*screen x size*/,
inky.getPosition().y,inky.getPosition().z)/*add setter in ghost class*/
}
In fact, this is not the best way to do it, but the simplest one

libGDX sprite sheet 2d animation

I'm trying to create simple 2d game in Java with libGDX.
I stuck on creating 2d animation from sprite sheet in PNG file.
I have AbstractScreen class which holds 'things' for every screen.
GameScreen class for drawing my game and Player class with animation code.
Player class is from https://github.com/libgdx/libgdx/wiki/2D-Animation example.
Firstly i created AbstractScreen and MainMenuScreen (based on AbstractScreen). It's works but now i'm trying to create GameScreen with my animated player. Something is wrong because i haven't any error and player is not visible on app screen. How to properly implement sprite sheet animation for skeleton of my app?
My game screen:
class GameScreen extends AbstractScreen {
Player player;
public GameScreen(NinjaGame game) {
super(game);
init();
}
#Override
protected void init() {
initPlayer();
}
private void initPlayer() {
player = new Player();
player.setDebug(true);
stage.addActor(player);
}
#Override
public void render(float delta) {
super.render(delta);
update();
spriteBatch.begin();
stage.draw();
spriteBatch.end();
}
private void update() {
stage.act();
}
}
My AbstractScreen class:
public abstract class AbstractScreen implements Screen {
protected NinjaGame game;
protected Stage stage;
private OrthographicCamera camera;
protected SpriteBatch spriteBatch;
public AbstractScreen(NinjaGame game) {
this.game = game;
createCamera();
/* Stage for actors */
stage = new Stage(new StretchViewport(NinjaGame.SCREEN_WIDTH, NinjaGame.SCREEN_HEIGHT, camera));
/* Batch for sprites */
spriteBatch = new SpriteBatch();
/* Stage takes user inputs */
Gdx.input.setInputProcessor(stage);
init();
}
protected abstract void init();
private void createCamera() {
/* Orthographic means like in CAD drawings */
camera = new OrthographicCamera();
camera.setToOrtho(false, NinjaGame.SCREEN_WIDTH, NinjaGame.SCREEN_HEIGHT);
camera.update();
}
/** Clean screen on black color between render frames */
private void clearScreen() {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
#Override
public void render(float delta) {
clearScreen();
camera.update();
spriteBatch.setProjectionMatrix(camera.combined);
}
}
My player class:
public class Player extends Actor {
private static final int FRAME_COLS = 10, FRAME_ROWS = 1;
Animation<TextureRegion> walkAnimation;
Texture walkSheet;
private final static int STARTING_X = 50;
private final static int STARTING_Y = 50;
public Player(){
createIdleAnimation();
this.setPosition(STARTING_X, STARTING_Y);
}
private void createIdleAnimation() {
walkSheet = new Texture(Gdx.files.internal("sheets/ninja-idle-sheet.png"));
TextureRegion[][] tmp = TextureRegion.split(walkSheet,
walkSheet.getWidth() / FRAME_COLS,
walkSheet.getHeight() / FRAME_ROWS);
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];
}
}
walkAnimation = new Animation<TextureRegion>(0.025f, walkFrames);
}
#Override
public void act(float delta) {
super.act(delta);
}
#Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
}
}
UPDATE 1
NinjaGame class
public class NinjaGame extends Game{
public final static int SCREEN_WIDTH = 800;
public final static int SCREEN_HEIGHT = 480;
public final static String GAME_NAME = "ninja vs zombie";
private boolean paused;
#Override
public void create() {
this.setScreen(new GameScreen(this));
}
}
Make small modification in your Player class so that it draw player Animation frame. You need to overloaded draw method of Actor and draw texture of Player.
public class Player extends Actor {
private static final int FRAME_COLS = 10, FRAME_ROWS = 1;
Animation<TextureRegion> walkAnimation;
Texture walkSheet;
private final static int STARTING_X = 50;
private final static int STARTING_Y = 50;
TextureRegion reg;
float stateTime;
public Player(){
createIdleAnimation();
this.setPosition(STARTING_X, STARTING_Y);
}
private void createIdleAnimation() {
walkSheet = new Texture(Gdx.files.internal("sheets/ninja-idle-sheet.png"));
TextureRegion[][] tmp = TextureRegion.split(walkSheet,
walkSheet.getWidth() / FRAME_COLS,
walkSheet.getHeight() / FRAME_ROWS);
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];
}
}
walkAnimation = new Animation<TextureRegion>(0.025f, walkFrames);
stateTime = 0f;
reg=walkAnimation.getKeyFrame(0);
}
#Override
public void act(float delta) {
super.act(delta);
stateTime += delta;
reg = walkAnimation.getKeyFrame(stateTime,true);
}
#Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
Color color = getColor();
batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
batch.draw(reg,getX(),getY(),getWidth()/2,getHeight()/2,getWidth(),getHeight(),getScaleX(),getScaleY(),getRotation());
}
}
EDIT
You need to resize viewport of stage according to your device screen width and height so override resize method and also no need to call begin and end of spritebatch. you're using stage that having own SpriteBatch
class GameScreen extends AbstractScreen {
Player player;
public GameScreen(NinjaGame game) {
super(game);
init();
}
#Override
protected void init() {
initPlayer();
}
private void initPlayer() {
player = new Player();
player.setDebug(true);
stage.addActor(player);
}
#Override
public void render(float delta) {
super.render(delta);
update();
stage.draw();
}
private void update() {
stage.act();
}
#Override
public void resize(int width, int height){
stage.getViewport().update(width,height);
}
}
EDIT 2
private void initPlayer() {
player = new Player();
player.setSize(100,150);
player.setDebug(true);
stage.addActor(player);
}

Libgdx updating animation

I'm learning libgdx and currently doing a flappy bird demo. For fun I tried to implement when the score reached a certain number the bird sprite texture will update and change to another color. Instead of using the spritebatch and changing the color through tinting I wanted to create a new texture(png file).
The problem is since it is a sprite it needs to be animated so the wings will flap. When I try and update the texture at runtime it will only work but the animation wont play.
Here is my bird class:
public class Bird {
private static final int GRAVITY = -15;
private static final int MOVEMENT = 100;
private Vector3 position;
private Vector3 velocity;
private Rectangle bounds;
private Animation birdAnimation;
private Texture birdTexture;
private TextureRegion textureRegion;
private Sound flap;
public Bird(int x, int y){
position = new Vector3(x, y, 0);
velocity = new Vector3(0, 0, 0);
textureRegion = new TextureRegion(returnTexture());
birdAnimation = new Animation(textureRegion, 3, 0.5f);
bounds = new Rectangle(x, y, returnTexture().getWidth() / 3, returnTexture().getHeight());
flap = Gdx.audio.newSound(Gdx.files.internal("sfx_wing.ogg"));
}
public void update(float dt){
textureRegion = new TextureRegion(returnTexture());
birdAnimation = new Animation(textureRegion, 3, 0.5f);
birdAnimation.update(dt);
if(position.y > 0){
velocity.add(0, GRAVITY, 0);
}
velocity.scl(dt);
position.add(MOVEMENT * dt, velocity.y, 0);
if(position.y < 0){
position.y = 0;
}
velocity.scl(1/dt);
bounds.setPosition(position.x, position.y);
}
public TextureRegion getTexture() {
return birdAnimation.getFrame();
}
public Texture returnTexture(){
if(PlayState.score > 1){
return birdTexture = new Texture("birdanimation1.png");
}else{
return birdTexture = new Texture("birdanimation.png");
}
}
public Vector3 getPosition() {
return position;
}
public void jump(){
velocity.y = 250;
flap.play(0.15f);
}
public Rectangle getBounds(){
return bounds;
}
public void dispose(){
returnTexture().dispose();
flap.dispose();
}
}
Here is my animation class:
public class Animation {
private Array<TextureRegion> frames;
private float maxFrameTime;
private float currentFrameTime;
private int frameCount;
private int frame;
public Animation(TextureRegion region, int frameCount, float cycleTime){
frames = new Array<TextureRegion>();
int frameWidth = region.getRegionWidth() / frameCount;
for(int i = 0; i < frameCount; i++){
frames.add(new TextureRegion(region, i * frameWidth, 0, frameWidth, region.getRegionHeight()));
}
this.frameCount = frameCount;
maxFrameTime = cycleTime / frameCount;
frame = 0;
}
public void update(float dt){
currentFrameTime += dt;
if(currentFrameTime > maxFrameTime){
frame++;
currentFrameTime = 0;
}
if(frame >= frameCount){
frame = 0;
}
}
public TextureRegion getFrame(){
return frames.get(frame);
}
}
Here's my render code in my play state:
#Override
public void render(SpriteBatch sb) {
sb.setProjectionMatrix(cam.combined);
sb.begin();
sb.draw(bg, cam.position.x - (cam.viewportWidth /2), 0);
sb.draw(bird.getTexture(), bird.getPosition().x, bird.getPosition().y);
for(Tube tube : tubes){
sb.draw(tube.getTopTube(), tube.getPosTopTube().x, tube.getPosTopTube().y);
sb.draw(tube.getBottomTube(), tube.getPosBotTube().x, tube.getPosBotTube().y);
}
sb.draw(ground, groundPos1.x, groundPos1.y);
sb.draw(ground, groundPos2.x, groundPos2.y);
font.draw(sb, text, cam.position.x - gl.width / 2, cam.position.y + 200);
sb.end();
}
If you need any other classes just ask. I'm probably making a stupid mistake or just coding it entirely wrong for what I'm trying to achieve.
Thanks, Jackson
You need to NOT load your textures every single frame.
libgdx has its own Animation class so you don't need to make your own.
Here is an example on animation from libgdx's github:
2D Animation
To make it simple, just have 2 animaitons on Bird and switch between them when you need to.

Deleting "item" whan its picked up/when im standing on it

I have this ArrayList that I'm using to generate 2 types of swords that has from 1 to 5 levels. When I "pick it up" I want it to be gone forever. How do I dispose that sword?
This code allocate and generate the Sword(s) Renderer class:
private List<Sword> swords = new ArrayList<Sword>();
Then in public Renderer() method I do:
for (int i = 0; i<10 ; i++ ) {
swords.add(new Sword());
}
When I'm standing on it:
if(sword.myLevel<3){
sword.pickMe();
}
In the render() they are displayed on the screen:
for (Sword sword : swords) {
sword.createMe();
}
And here is my Sword class:
public class Sword {
private TextureRegion sprite;
public Vector2 position;
private int x, y;
private int size;
private Random r;
private boolean pickme = false;
public int myLevel;
public Sword() {
position = new Vector2(x, y);
r = new Random();
position.x = (r.nextInt(15))*GameRender.tilesize;
position.y = (r.nextInt(15))*GameRender.tilesize;
size = GameRender.tilesize;
myLevel = r.nextInt(4);
sprite =getSprite();
System.out.println(myLevel);
}
private TextureRegion getSprite() {
if(myLevel<3){
sprite=AssetLoader.s1;
}else sprite=AssetLoader.s2;
return sprite;
}
public void pickMe(){
//GameRender.batch.draw(sprite, 10, 10, size, size);
pickme = true;
}
public void createMeShape(){
//GameRender.shapeRenderer.rect(position.x, position.y, size, size);
}
public void createMe() {
//draws it
GameRender.batch.draw(sprite, position.x, position.y, size, size);
if(pickme){
//draws that im standing on it
GameRender.batch.draw(sprite, GameRender.playerx, GameRender.playery-10, size, size);
}
pickme=false;
}
public static void Update() {
}
}
Ignoring the whole (if pickMe()) in the createMe() how do I delete this specific sword when I'm on it?
If you want to delete the Sword forever after picking it up, you'll have to remove it from the ArrayList of Swords.
if (sword.myLevel<3){
sword.pickMe();
swords.remove(sword);
}

LibGDX android translating spritebatch sometimes jumps 1-2 pixels forward (lag)?

I am making my first game using LibGDX on android.
I have a background which is 288*511, Now in my game, I need to repeat that background and then translate over it. I have made that game for desktop using Slick2D, and had the same problem, just a bit lower than on my LG G2 phone (4.0.4), is there a way to fix this lag or I am doing something wrong?
The problem is that when it's translating, its moving fine and sometimes jumps 1-2 pixels forward or just stuck for 0.5 seconds or so.
This is my class:
public class Background {
private class RepeatedBackground implements GameObject {
private int x;
private int y = 0;
Sprite background;
public RepeatedBackground(int x, Sprite s) {
this.x = x;
this.background = s;
}
#Override
public void render() {
}
public float getPreferedWidth() {
int w = Gdx.graphics.getWidth();
return (float) (w / 1.15);
}
public float getPreferedHeight() {
int h = Gdx.graphics.getHeight();
return (float) (h / 1.15);
}
#Override
public int getX() {
return this.x;
}
#Override
public int getY() {
return this.y;
}
public Sprite getSprite() {
return this.background;
}
}
private int tra = 0;
private long traTime = 0;
private List<RepeatedBackground> backgrounds = new ArrayList<RepeatedBackground>();
private Level level;
private SpriteBatch backgroundRenderer;
public Background(Level level) {
this.level = level;
this.backgroundRenderer = new SpriteBatch();
}
public void generateBackgrounds() {
int x = 0;
Sprite background = this.level.getFactory().createBackgroundSprite();
for (int i = 0; i < LevelConfig.MAX_BACKGROUND_REPEATS; i++) {
this.backgrounds.add(new RepeatedBackground(x, background));
x += background.getWidth();
}
}
public void render() {
this.backgroundRenderer.getTransformMatrix().translate(-6, 0, 0);
this.backgroundRenderer.begin();
this.backgroundRenderer.setProjectionMatrix(this.level.getInstance().getCamera().combined);
Iterator<RepeatedBackground> itr = this.backgrounds.iterator();
while (itr.hasNext()) {
RepeatedBackground b = itr.next();
Sprite s = b.getSprite();
if (b.getX() - b.getPreferedHeight() < this.level.getInstance().getCamera().viewportWidth) { // this doesn't work properly, but it doesn't load all backgorunds at once but still lags..
this.backgroundRenderer.draw(s, b.getX(), b.getY(), b.getPreferedWidth(), b.getPreferedHeight());
}
}
this.backgroundRenderer.end();
}
}
My create values for camera:
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(w,h);
camera.setToOrtho(false);
batch = new SpriteBatch();
Texture.setEnforcePotImages(false);
You can use ParallaxBackground class from here
http://www.badlogicgames.com/forum/viewtopic.php?f=17&t=1795
It doesn't depends on the size of the image or camera. It just translates your background continuously
source: http://code.google.com/p/libgdx-users/wiki/ParallaxBackgound

Categories

Resources