zoukankan      html  css  js  c++  java
  • libgdx学习记录3——动画Animation

    libgdx动画采用Animation实现,即通过帧动画实现。

    代码如下:

     1 package com.fxb.newtest;
     2 
     3 import com.badlogic.gdx.ApplicationAdapter;
     4 import com.badlogic.gdx.Gdx;
     5 import com.badlogic.gdx.graphics.GL10;
     6 import com.badlogic.gdx.graphics.Texture;
     7 import com.badlogic.gdx.graphics.g2d.Animation;
     8 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
     9 import com.badlogic.gdx.graphics.g2d.TextureRegion;
    10 
    11 public class Lib002_Animation extends ApplicationAdapter{
    12 
    13     Texture texture;
    14     Animation animation;
    15     SpriteBatch batch;
    16     float currentTime = 0;
    17     
    18     @Override
    19     public void create() {
    20         // TODO Auto-generated method stub
    21         batch = new SpriteBatch();
    22         texture = new Texture( Gdx.files.internal( "data/koalio.png" ) );
    23         TextureRegion region = new TextureRegion( texture );
    24         TextureRegion[] regions = region.split( 18, 26 )[0];
    25         
    26         animation = new Animation( 0.1f, regions[1], regions[2], regions[3], regions[4] );
    27     }
    28 
    29     @Override
    30     public void render() {
    31         // TODO Auto-generated method stub
    32         Gdx.gl.glClearColor( 1, 1, 1, 1 );
    33         Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT );
    34         
    35         currentTime += Gdx.graphics.getDeltaTime();
    36         TextureRegion region = animation.getKeyFrame( currentTime, true );
    37         
    38         batch.begin();
    39         batch.draw( region, 100, 100, region.getRegionWidth(), region.getRegionHeight() );
    40         batch.end();        
    41     }
    42 
    43     @Override
    44     public void dispose() {
    45         // TODO Auto-generated method stub
    46         super.dispose();
    47     }
    48 
    49 }

    运行效果:

  • 相关阅读:
    const 深悟理解
    深拷贝与浅拷贝深究
    结队开发-最大子数组
    软件工程个人作业02
    四则运算关于加括号的思路
    实践出真知-所谓"java没有指针",那叫做引用!
    写代码的好习惯—先构思
    团队合作
    阿超超的四则运算 想啊想啊
    Github+阿超运算
  • 原文地址:https://www.cnblogs.com/MiniHouse/p/3739615.html
Copyright © 2011-2022 走看看