2012-08-31 6 views
0

내 간단한 2D 안드로이드 게임 엔진에 비트 맵 애니메이션을 추가하려고합니다 (아직 D ..라고 부르는 경우). 미리 정의 된 FPS/속도로 프레임을 순환하는 문제에 직면하고 있습니다.안드로이드 비트 맵 애니메이션

내가 지금해온 것은 단순히 currentTimeMillis를 가져 와서 참조로 사용하는 시스템이지만, 그게 최선의 결정이 아니라고 확신한다. 나는 현재 프레임을 드로잉 메서드에 전달하여이를 사용할 수 있다고 생각했다. 그리기 방법.

현재 나는 당신이에서 드로어 블을받을 객체를 가지고 있고이 같은 렌더러를 사용

canvas.drawBitmap(animation.getDrawable(), (int)x, (int)y, null); 

및 getDrawable는()을 heres 애니메이션의 현재 프레임 또는 유일한 프레임 경우의 정지 영상과를 반환 코드

 if(frames.size() > 1) { 
      long current = System.currentTimeMillis(); 
      if (currentFrame == frames.size() - 1 && frameDirection == 1) { 
       frameDirection = -1; 
      } else if (currentFrame == 0 && frameDirection == -1) { 
       frameDirection = 1; 
      } 
      if ((current - lastFrame) > delay) { 
       currentFrame += frameDirection; 
       lastFrame = current; 
      } 
     } 
     return frames.get(currentFrame); 

과 너무 나쁜 unefficient 보이는 내 눈에

.. 내가 이것에 대해 무엇을 할 수 있습니까? getDrawable에 현재 프레임을 전달하는 유일한 방법입니까, 아니면 어떻게해야합니까?

답변

1

이 권리를 이해한다면 게임 엔진을위한 애니메이션 클래스를 만들고 싶습니까?

내 게임 엔진에는 SpriteSheetAnimation이라는 클래스가 있습니다.이 메서드는 기본적으로 업데이트 메서드가 호출 될 때 애니메이션의 모든 프레임을 반복합니다. 그 뒤에있는 논리를 이해할 수있을 것이라고 확신합니다. 그것을 사용하는 방법 :

public class SpriteSheetAnimation extends GameObjectTemplate { 

    int animationSheetLength; 
    int animationSheetHeight; 
    int columns; 
    int rows; 
    Rect srcRect; 

    int left; 
    int bottom; 
    int lengthOfSpriteInSheet; 
    int heightOfSpriteInSheet; 

    boolean repeatAnimation; 
    boolean animationCompleted; 
    boolean animationStarted; 

    Bitmap bitmap; 

    /** 
    * You input a bitmap and the number of sprites (by defining the number of rows and columns), and this class will 
    * take care of animating the sheet. 
    * 
    */ 

    public SpriteSheetAnimation(WorldTemplate world, Bitmap animationSheet, int amountOfRows, int amountOfColumns) { 
     super(world); 
     this.animationSheetLength = animationSheet.getWidth(); 
     this.animationSheetHeight = animationSheet.getHeight(); 
     this.columns = amountOfColumns; 
     this.rows = amountOfRows; 
     this.lengthOfSpriteInSheet = this.animationSheetLength/this.columns; 
     this.heightOfSpriteInSheet = this.animationSheetHeight/this.rows; 
     srcRect = new Rect(); 

     left = 0; 
     bottom = this.heightOfSpriteInSheet; 

     repeatAnimation = false; 
     animationCompleted = false; 
     animationStarted = false; 

     this.bitmap = animationSheet; 
     this.drawShapesOfSprites = true; 

     this.gameObjectShape.addRectangle(new Rect(0, 0, 0 + this.heightOfSpriteInSheet, 0 + this.lengthOfSpriteInSheet)); 

    } 

    @Override 
    public void defineGameObjectPositionShape() { 
     this.gameObjectShape.addRectangle(new Rect()); 

    } 

    /** 
    * <b><i>public Rect getSrcRect()</i></b> 
    * <br> 
    * Since: API 1 
    * <br> 
    * <br> 
    * Retrieve the rect that will cover the current source of the sheet. 
    * 
    * @return 
    * The current rect that will cover the current source of the sheet. 
    * 
    */ 

    public Rect getSrcRect() { 
     return srcRect; 
    } 

    /** 
    * <b><i>public Rect getDstRect()</i></b> 
    * <br> 
    * Since: API 1 
    * <br> 
    * <br> 
    * Retrieve the rect where the bitmap will be scaled to fit into. 
    * 
    * @return 
    * The current rect where the bitmap will be scaled to fit into. 
    * 
    */ 

    public Rect getDstRect() { 
     return this.getGameObjectPositionRect(); 
    } 

    /** 
    * <b><i>public void repeatAnimation(boolean repetition)</i></b> 
    * <br> 
    * Since: API 1 
    * <br> 
    * <br> 
    * This method will set if the animation should be repeated or not. 
    * 
    * @param state 
    * <br><br> 
    * 1. True = The animation will repeat once completed and keep on doing so. 
    * <br><br> 
    * 2. False = The animation will play only once. 
    * 
    *  
    */ 

    public void repeatAnimation(boolean repetition) { 
     this.repeatAnimation = repetition; 
    } 

    /** 
    * <b><i>public boolean isAnimationFinished()</i></b> 
    * <br> 
    * Since: API 1 
    * <br> 
    * <br> 
    * Retrieve if the animation has finished. 
    * 
    * @return 
    * If the animation has finished. 
    * 
    */ 

    public boolean isAnimationFinished() { 
     return animationCompleted; 
    } 

    /** 
    * <b><i>public boolean hasBeenStarted()</i></b> 
    * <br> 
    * Since: API 1 
    * <br> 
    * <br> 
    * Retrieve if the animation has started. 
    * 
    * @return 
    * If the animation has started. 
    * 
    */ 

    public boolean hasBeenStarted() { 
     return animationStarted; 
    } 

    /** 
    * <b><i>public void render()</i></b> 
    * <br> 
    * Since: API 1 
    * <br> 
    * <br> 
    * This method will render the animation (the current picture of the sheet). 
    * 
    */ 

    public void render() { 
     world.game.getGraphics().getCanvasGameScreen().drawBitmap(this.bitmap, this.getSrcRect(), this.getGameObjectPositionRect(), null); 
    } 

    /** 
    * <b><i>public void update()</i></b> 
    * <br> 
    * Since: API 1 
    * <br> 
    * <br> 
    * This method will update the animation. 
    * 
    */ 

    @Override 
    public void update() { 
     // Every update will advance to the next frame of the animation 
     // Set if animation has started 
     if(!animationStarted) { 
      animationStarted = true; 
     } 

     // Set the frame 
     srcRect.set(left, (bottom - this.heightOfSpriteInSheet), (left + this.lengthOfSpriteInSheet), bottom); 
     // Change the location, so the next time the frame is set, it will be set accordingly 
     left = left + this.lengthOfSpriteInSheet; 

     if(left == this.animationSheetLength && bottom == this.animationSheetHeight) { 
      if(repeatAnimation == true) { 
       left = 0; 
       bottom = 0; 
      } 
      else { 
       animationCompleted = true; 
      } 
     } 

     if(left == this.animationSheetLength) { 
      bottom = bottom + this.heightOfSpriteInSheet; 
      left = 0; 
     } 

    } 


} 

나는 이것이 당신을 도우며 당신을 시작할 수 있기를 바랍니다.

+0

감사합니다. 스프라이트 시트를 거의 잊어 버렸습니다. 정말 고마워요! – Ruuhkis

관련 문제