Android Studio Libgdx动画无效

时间:2015-08-19 20:51:03

标签: android animation libgdx

我正在尝试为向右移动的角色制作动画。显示该字符,但没有动画正在进行。除了动画之外,代码中的所有内容都能正常运行。以下是该类的代码:

package com.NeverMind.DontFall.android;



import android.util.Log;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;

/**
 * Created by user on 19-Aug-15.
 */
public class PlayerClass {
    private float posX, posY, frameSpeed, leftX,  leftY, leftSizeX, leftSizeY, speed, timePassed;
    private int rightX, rightY, rightSizeX, rightSizeY;
    private Animation rightMovementAnim;
    private TextureAtlas rightMovementAt;
    private Texture stillPos, leftTexture, rightTexture;
    private SpriteBatch spriteBatch;
    public PlayerClass(int scaleX, int scaleY) {
        rawData(scaleX, scaleY);
    }

    public void update()
    {
        if (getDirection() == 0 && Gdx.input.isTouched())
            posX -= speed;
        if (getDirection() == 1 && Gdx.input.isTouched())
            posX += speed;
    }
    public void draw()
    {
        spriteBatch.begin();
        timePassed = Gdx.graphics.getDeltaTime();
        spriteBatch.draw(rightTexture, rightX, rightY, rightSizeX, rightSizeY);
        spriteBatch.draw(leftTexture, leftX, leftY, leftSizeX, leftSizeY);
        if ((getDirection() == 0 || getDirection() == 1) && Gdx.input.isTouched()) {
            timePassed += Gdx.graphics.getDeltaTime();
            spriteBatch.draw(rightMovementAnim.getKeyFrame(timePassed, true), posX, posY);
        }
        else
            spriteBatch.draw(stillPos, posX, posY);
        spriteBatch.end();

    }
    // Obtin directia in care merge Playerul
    private int getDirection()
    {
        if (leftButtonTouched(Gdx.input.getX(), Gdx.input.getY()))
            return 0;
        if (rightButtonTouched(Gdx.input.getX(), Gdx.input.getY()))
            return 1;
        return 2;
    }
    // Verific daca a fost apasat butonul pentru deplasare in stanga
    private boolean leftButtonTouched(int x, int y)
    {
        if (x > leftX && y < Gdx.graphics.getHeight() - leftY && x < leftSizeX + leftX && y > Gdx.graphics.getHeight() - leftSizeY - leftY)
            return true;
        return false;
    }
    // Verific daca a fost apasat butonul pentru deplasa in dreapta
    private boolean rightButtonTouched(int x, int y)
    {
        if (x > rightX && y < Gdx.graphics.getHeight() - rightY && x < rightSizeX + rightX && y > Gdx.graphics.getHeight() - rightSizeY - rightY)
            return true;
        return false;
    }
    // Date brute puse la sfarsit pentru a nu umple codul
    public void rawData (int scaleX, int scaleY){
        posX = 300 * scaleX;
        posY = 100 * scaleY;
        speed = 5 * scaleX;
        frameSpeed = 1 / 6f;
        rightMovementAt = new TextureAtlas(Gdx.files.internal("charMovement.atlas"));
        rightMovementAnim = new Animation(frameSpeed, rightMovementAt.getRegions());
        leftX = 50; leftY = 0; leftSizeX = 150; leftSizeY = 150;
        rightX = 250; rightY = 0; rightSizeX = 150; rightSizeY = 150;
        stillPos = new Texture(Gdx.files.internal("char2.png"));
        spriteBatch = new SpriteBatch();
        leftTexture = new Texture (Gdx.files.internal("leftButton.png"));
        rightTexture = new Texture (Gdx.files.internal("rightButton.png"));
    }
}

1 个答案:

答案 0 :(得分:0)

这一行似乎是一个错误。这导致时间永远不会真正过去。

timePassed = Gdx.graphics.getDeltaTime();

可能应该是(虽然我不确切地知道你要做什么):

timePassed += Gdx.graphics.getDeltaTime();