libgdx [TextureAtlas]

本文介绍了一个使用 LibGDX 实现游戏角色动画的示例程序,通过加载纹理包并利用 SpriteBatch 进行绘制,实现了角色动画在不同位置上的显示。此程序展示了如何加载纹理集、创建动画帧序列及进行屏幕适配。
package com.mygdx.game;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.maps.MapLayer;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.MapProperties;
import com.badlogic.gdx.maps.objects.EllipseMapObject;
import com.badlogic.gdx.maps.objects.PolygonMapObject;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject;
import com.badlogic.gdx.maps.tiled.renderers.IsometricTiledMapRenderer;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile;
import com.badlogic.gdx.math.Ellipse;
import com.badlogic.gdx.math.Polygon;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.utils.viewport.Viewport;

public class MyGdxGame extends ApplicationAdapter {
    private static final float WORLD_TO_SCREEN = 1.0f / 100.0f;
    private static final float SCENE_WIDTH = 19.20f;
    private static final float SCENE_HEIGHT = 10.80f;
    private static final float ANIMATION_SPEED = 0.2f;

    private OrthographicCamera camera;
    private Viewport viewport;
    private SpriteBatch batch;
    private TextureAtlas atlas;
    private TextureRegion[] playerTextureRegionList = new TextureRegion[4];

    private Animation playerAnimation;
    private float currentFrameTime = 0.0f;

    @Override
    public void create() {
        camera = new OrthographicCamera();
        viewport = new FitViewport(SCENE_WIDTH, SCENE_HEIGHT, camera);
        batch = new SpriteBatch();

        // Load atlas and find regions
        atlas = new TextureAtlas(Gdx.files.internal("data/texturepacker1.atlas"));

        playerTextureRegionList[0] = atlas.findRegion("2");
        playerTextureRegionList[1] = atlas.findRegion("3");
        playerTextureRegionList[2] = atlas.findRegion("4");
        playerTextureRegionList[3] = atlas.findRegion("5");

        playerAnimation = new Animation(ANIMATION_SPEED, playerTextureRegionList);

        int maxSize = GL20.GL_MAX_TEXTURE_SIZE;
        Gdx.app.log("TextureAtlasSample", "Max texture size: " + maxSize + "x" + maxSize);
    }

    @Override
    public void dispose() {
        batch.dispose();
        atlas.dispose();
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(1, 1, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.setProjectionMatrix(camera.combined);

        batch.begin();

        currentFrameTime += Gdx.graphics.getDeltaTime();
        TextureRegion frame = playerAnimation.getKeyFrame(currentFrameTime, true);
        float width = frame.getRegionWidth();
        float height = frame.getRegionHeight();
        float originX = width * 0.5f;
        float originY = height * 0.5f;
        // center
        batch.draw(frame,
                -originX, -originY,
                originX, originY,
                width, height,
                WORLD_TO_SCREEN, WORLD_TO_SCREEN,
                0.0f);
        // left top
        batch.draw(frame,
                0-(SCENE_WIDTH/2), (SCENE_HEIGHT/2)-height,
                0, height,
                width, height,
                WORLD_TO_SCREEN, WORLD_TO_SCREEN,
                0.0f);
        // left bottom
        batch.draw(frame,
                0-(SCENE_WIDTH/2), 0-(SCENE_HEIGHT/2),
                0, 0,
                width, height,
                WORLD_TO_SCREEN, WORLD_TO_SCREEN,
                0.0f);
        // right top
        batch.draw(frame,
                (SCENE_WIDTH/2)-width, (SCENE_HEIGHT/2)-height,
                width, height,
                width, height,
                WORLD_TO_SCREEN, WORLD_TO_SCREEN,
                0.0f);
        // right bottom
        batch.draw(frame,
                (SCENE_WIDTH/2)-width, 0-(SCENE_HEIGHT/2),
                width, 0,
                width, height,
                WORLD_TO_SCREEN, WORLD_TO_SCREEN,
                0.0f);

        batch.end();
    }

    @Override
    public void resize(int width, int height) {
        viewport.update(width, height);
    }
}

效果图:
这里写图片描述

texturepacker1.atlas:

texturepacker1.png
size: 81, 358
format: RGBA8888
filter: Linear,Linear
repeat: none
2
  rotate: false
  xy: 1, 1
  size: 79, 88
  orig: 79, 89
  offset: 0, 0
  index: -1
3
  rotate: false
  xy: 1, 181
  size: 79, 87
  orig: 79, 89
  offset: 0, 1
  index: -1
4
  rotate: false
  xy: 1, 270
  size: 79, 87
  orig: 79, 89
  offset: 0, 1
  index: -1
5
  rotate: false
  xy: 1, 91
  size: 79, 88
  orig: 79, 89
  offset: 0, 0
  index: -1

texturepacker1.png:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值