【问题标题】:Android Studio Libgdx Animation not workingAndroid Studio Libgdx 动画不起作用
【发布时间】:2015-08-19 20:51:02
【问题描述】:

我正在尝试为向右移动的角色制作动画。显示角色,但没有动画发生。代码中的所有内容都运行良好,除了动画。下面是类的代码:

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"));
    }
}

【问题讨论】:

    标签: android animation libgdx


    【解决方案1】:

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

    timePassed = Gdx.graphics.getDeltaTime();
    

    它可能应该是(虽然我不知道你到底想做什么):

    timePassed += Gdx.graphics.getDeltaTime();
    

    【讨论】:

    • 谢谢。我不敢相信我没有看到,这显然是错误的。
    猜你喜欢
    • 2015-11-20
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 2016-08-17
    • 2023-02-07
    • 1970-01-01
    • 2015-02-20
    相关资源
    最近更新 更多