您的位置:首页 > 其它

Libgdx专题系列:对象篇 纹理的旋转,放缩,位移

2013-11-23 14:23 351 查看
声明:

本系列文章使用的Libgdx版本均为0.99版本

Libgdx游戏开发交流群 323876830



下面我们将重点介绍操作纹理。大部分情况下是这种情况, 不是使用整张图片而是部分区域,这个需要怎么操作呢?看下面的代码。

下面的代码实现了纹理部分区域的旋转,放缩,和位移操作

public class FirstGame implements ApplicationListener
{

    private Pixmap mPixmap;
    private Texture mTexture;
    private TextureRegion mTextureRegion;
    private SpriteBatch mSpriteBatch;
    private OrthographicCamera mCamera;

    private float mScaleX;
    private float mScaleY;
    private float mRotation;
    private float mTransfer;
    private float timer;

    @Override
    public void create()
    {
        mPixmap = new Pixmap(Gdx.files.internal("gremlins.png"));
        mTexture = new Texture(mPixmap);
        mTextureRegion = new TextureRegion(mTexture, 0, 0, 30, 30);
        mSpriteBatch = new SpriteBatch();
        // 镜头需要设定宽高和镜头中间位置
        mCamera = new OrthographicCamera(800, 480);
        mCamera.position.set(800 / 2, 480 / 2, 0);

        mScaleX = 1;
        mScaleY = 1;
        mRotation = 0;
        mTransfer = 0;
    }

    @Override
    public void resize(int width, int height)
    {
        // 确保AndroidManifest.xml里面的Activity配置为横屏
        // 宽和高才是横屏的值的返回,默认为竖屏的值
    }

    @Override
    public void render()
    {
        // 镜头的更新与设置矩阵到SpriteBatch
        mCamera.update();
        mSpriteBatch.setProjectionMatrix(mCamera.combined);

        timer += Gdx.graphics.getDeltaTime();
        // 1.移动
        if (timer < 3)
        {
            //每秒移动50像素
            mTransfer += 50 * Gdx.graphics.getDeltaTime();
        }
        // 2.放缩
        else if (timer > 3 && timer < 6)
        {
            mTransfer=0;
            //每秒xy方向上放大1倍
            mScaleX += 1 * Gdx.graphics.getDeltaTime();
            mScaleY += 1 * Gdx.graphics.getDeltaTime();
        }
        // 3.翻转
        else if (timer > 6 && timer < 9)
        {
            mTransfer=0;
            mScaleX=1;
            mScaleY=1;
            //每秒旋转90度
            mRotation += 90 * Gdx.graphics.getDeltaTime();
        }
        else
        {
            timer = 0;
            mTransfer = 0;
            mScaleX = 1;
            mScaleY = 1;
            mRotation=0;
        }
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        mSpriteBatch.begin();
        //这里的15,15是放缩旋转的中心点, 他是根据纹理区域左下角来计算的
        //30,30是要要放缩到的大小,这里和剪切区域大小一致
        mSpriteBatch.draw(mTextureRegion, 400 + mTransfer, 240, 15, 15, 30, 30,
                mScaleX, mScaleY, mRotation);
        mSpriteBatch.end();
    }

    @Override
    public void pause()
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void resume()
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void dispose()
    {
        // 所有实现Disposable接口的都需要释放资源
        mPixmap.dispose();
        mTexture.dispose();
        mSpriteBatch.dispose();
    }

}




这里执行的动作是3秒的位移,3秒的放大, 3秒的旋转,循环执行。SpriteBatch提供了一个public void draw (TextureRegion region, float x, float y, float originX, float originY, float width, float height,

float scaleX, float scaleY, float rotation)这个方法,来进行纹理区域的位移,放缩,旋转操作。

工程代码下载



转载请链接原文地址 http://blog.csdn.net/wu928320442/article/details/16900125
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: