2015-08-19 20 views
0

我正在嘗試爲向右移動的角色製作動畫。該角色顯示,但沒有動畫正在發生。代碼中的所有內容都運行良好,除了動畫。以下是該課程的代碼:Android Studio 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")); 
    } 
} 

回答

0

此行似乎是一個錯誤。這會讓時間永遠無法通過。

timePassed = Gdx.graphics.getDeltaTime(); 

它可能應該是(雖然我不知道你想要做什麼):

timePassed += Gdx.graphics.getDeltaTime(); 
+0

謝謝。我不相信我沒有看到,這顯然是錯誤的。 –