Libgdx游戏编程之Touchpad摇杆控制角色行走

先上效果:

以下素材来源网络,人物只有4向行走,遥感的图片就没有打包了,人物行走的用GDX Texture Packer打成atlas文件。

创建touchpad的代码

Touchpad.TouchpadStyle touchpadStyle = new Touchpad.TouchpadStyle(
                new TextureRegionDrawable(new Texture(Gdx.files.internal("touchpad_bg.png"))),
                new TextureRegionDrawable(new Texture(Gdx.files.internal("touchpad_inner.png")))
);
touchpad = new Touchpad(15f, touchpadStyle);
touchpad.setSize(200, 200);
touchpad.setBounds(0, 0, 200, 200);
stage.addActor(touchpad);

Gdx.input.setInputProcessor(stage);

 处理touchpad事件

if(touchpad.isTouched()){
            float knobPercentX = touchpad.getKnobPercentX();
            float knobPercentY = touchpad.getKnobPercentY();
            Gdx.app.log(TAG, "knobX:" + touchpad.getKnobPercentX() + ",knobY:" + touchpad.getKnobPercentY());

            //判断方向
            if(Math.abs(knobPercentX) > Math.abs(knobPercentY)){
                direction = knobPercentX < 0 ? Direction.LEFT : Direction.RIGHT;
            }else{
                direction = knobPercentY < 0 ? Direction.DOWN : Direction.UP;
            }

            TextureAtlas.AtlasRegion keyFrame = null;
            if(direction == Direction.RIGHT){
                rightAnimTimer += dt;
                keyFrame = rightAnim.getKeyFrame(rightAnimTimer);
            }else if(direction == Direction.LEFT){
                leftAnimTimer += dt;
                keyFrame = leftAnim.getKeyFrame(leftAnimTimer);
            }else if(direction == Direction.DOWN){
                downAnimTimer += dt;
                keyFrame = downAnim.getKeyFrame(downAnimTimer);
            }else if(direction == Direction.UP){
                upAnimTimer += dt;
                keyFrame = upAnim.getKeyFrame(upAnimTimer);
            }
            game.batch.draw(keyFrame, GameUIDemo.V_WIDTH / 2 - keyFrame.getRegionWidth() / 2, GameUIDemo.V_HEIGHT / 2 - keyFrame.getRegionHeight() / 2);
        }

全部代码:

package com.mygdx.game.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
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.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Touchpad;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.viewport.FitViewport;

public class TouchPadHUD implements Disposable {
    private static final String TAG = TouchPadHUD.class.getSimpleName();
    private GameUIDemo game;
    private Stage stage;
    private OrthographicCamera camera;
    private FitViewport viewport;

    private enum Direction{LEFT, RIGHT, DOWN, UP};

    private Direction direction;
    private Touchpad touchpad;

    private Animation<TextureAtlas.AtlasRegion> downAnim;
    private TextureAtlas.AtlasRegion downIdle;
    private Animation<TextureAtlas.AtlasRegion> upAnim;
    private TextureAtlas.AtlasRegion upIdle;
    private Animation<TextureAtlas.AtlasRegion> leftAnim;
    private TextureAtlas.AtlasRegion leftIdle;
    private Animation<TextureAtlas.AtlasRegion> rightAnim;
    private TextureAtlas.AtlasRegion rightIdle;

    private float rightAnimTimer;
    private float leftAnimTimer;
    private float upAnimTimer;
    private float downAnimTimer;

    public TouchPadHUD(GameUIDemo game){
        this.game = game;

        //创建舞台
        camera = new OrthographicCamera();
        viewport = new FitViewport(GameUIDemo.V_WIDTH, GameUIDemo.V_HEIGHT, camera);
        stage = new Stage(viewport, game.batch);

        TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("girl.atlas"));
        downAnim = new Animation<>(0.2f, atlas.findRegions("down"), Animation.PlayMode.LOOP);
        downIdle = atlas.findRegion("down", 1);
        upAnim = new Animation<>(0.2f, atlas.findRegions("up"), Animation.PlayMode.LOOP);
        upIdle = atlas.findRegion("up", 1);
        leftAnim = new Animation<>(0.2f, atlas.findRegions("left"), Animation.PlayMode.LOOP);
        leftIdle = atlas.findRegion("left", 1);
        rightAnim = new Animation<>(0.2f, atlas.findRegions("right"), Animation.PlayMode.LOOP);
        rightIdle = atlas.findRegion("right", 1);

        Touchpad.TouchpadStyle touchpadStyle = new Touchpad.TouchpadStyle(
                new TextureRegionDrawable(new Texture(Gdx.files.internal("touchpad_bg.png"))),
                new TextureRegionDrawable(new Texture(Gdx.files.internal("touchpad_inner.png")))
        );
        touchpad = new Touchpad(15f, touchpadStyle);
        touchpad.setSize(200, 200);
        touchpad.setBounds(0, 0, 200, 200);
        stage.addActor(touchpad);

        Gdx.input.setInputProcessor(stage);
    }

    public void draw(float dt){

        ScreenUtils.clear(Color.BLACK);

        game.batch.begin();
        if(touchpad.isTouched()){
            float knobPercentX = touchpad.getKnobPercentX();
            float knobPercentY = touchpad.getKnobPercentY();
            Gdx.app.log(TAG, "knobX:" + touchpad.getKnobPercentX() + ",knobY:" + touchpad.getKnobPercentY());

            //判断方向
            if(Math.abs(knobPercentX) > Math.abs(knobPercentY)){
                direction = knobPercentX < 0 ? Direction.LEFT : Direction.RIGHT;
            }else{
                direction = knobPercentY < 0 ? Direction.DOWN : Direction.UP;
            }

            TextureAtlas.AtlasRegion keyFrame = null;
            if(direction == Direction.RIGHT){
                rightAnimTimer += dt;
                keyFrame = rightAnim.getKeyFrame(rightAnimTimer);
            }else if(direction == Direction.LEFT){
                leftAnimTimer += dt;
                keyFrame = leftAnim.getKeyFrame(leftAnimTimer);
            }else if(direction == Direction.DOWN){
                downAnimTimer += dt;
                keyFrame = downAnim.getKeyFrame(downAnimTimer);
            }else if(direction == Direction.UP){
                upAnimTimer += dt;
                keyFrame = upAnim.getKeyFrame(upAnimTimer);
            }
            game.batch.draw(keyFrame, GameUIDemo.V_WIDTH / 2 - keyFrame.getRegionWidth() / 2, GameUIDemo.V_HEIGHT / 2 - keyFrame.getRegionHeight() / 2);
        }else{
            TextureAtlas.AtlasRegion keyFrame = null;
            if(direction == Direction.RIGHT){
                keyFrame = rightIdle;
            }else if(direction == Direction.LEFT){
                keyFrame = leftIdle;
            }else if(direction == Direction.UP){
                keyFrame = upIdle;
            }else{
                //默认down方向
                keyFrame = downIdle;
            }
            game.batch.draw(keyFrame, GameUIDemo.V_WIDTH / 2 - downIdle.getRegionWidth() / 2, GameUIDemo.V_HEIGHT / 2 - downIdle.getRegionHeight() / 2);
        }

        game.batch.end();
        stage.act(dt);
        stage.draw();
    }

    @Override
    public void dispose() {
        stage.dispose();
    }
}

 Game入口类

package com.mygdx.game.ui;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;

public class GameUIDemo extends Game {
    public static final float V_WIDTH = 480;
    public static final float V_HEIGHT = 800;
    public SpriteBatch batch;
    public Skin skin;
    public BitmapFont font;

    private TouchPadHUD touchPadHUD;

    @Override
    public void create() {
        skin = new Skin(Gdx.files.internal("ui/uiskin.json"));
        font = new BitmapFont(Gdx.files.internal("bitmapfont_big.fnt"));

        batch = new SpriteBatch();
        
        touchPadHUD = new TouchPadHUD(this);

    }

    @Override
    public void render() {
        super.render();

        touchPadHUD.draw(Gdx.graphics.getDeltaTime());

    }

    @Override
    public void resize(int width, int height) {
        super.resize(width, height);

    }

    @Override
    public void dispose() {
        super.dispose();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dyyaries

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值