先上效果:
以下素材来源网络,人物只有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();
}
}