一、初始化方法及常见的API
background = new TextureRegionDrawable(new TextureRegion(texture, 0, 0,128,128));
knobRegion = new TextureRegionDrawable(new TextureRegion(texture,128,0,128,128));
style = new TouchpadStyle(background, knobRegion);
touchPad = new Touchpad(15, style);//初始化游戏摇杆。(摇杆触碰区域的半径大小,TouchPagStyle)
touchPad.setBounds(0, 0, 150, 150);//设置摇杆的位置和大小
public void update(){
if(touchPad.isTouched()){//判断摇杆是否被按下
x += touchPad.getKnobPercentX()*speed;//改变相应的坐标.
y += touchPad.getKnobPercentY()*speed;
}
}
二、应用举例
package com.example.groupactiontest;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Touchpad;
import com.badlogic.gdx.scenes.scene2d.ui.Touchpad.TouchpadStyle;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
public class MyGame implements ApplicationListener {
Stage stage;
//绘制游戏摇杆的相关类
Touchpad touchPad;
TouchpadStyle style;
TextureRegionDrawable background;
TextureRegionDrawable knobRegion;
Texture texture;
Texture killer;
SpriteBatch batch;
public static int speed = 3;//用于控制图片的移动
int x = 0;
int y = 0;
@Override
public void create() {
stage = new Stage();
texture = new Texture(Gdx.files.internal("touchpad.png"));
killer = new Texture(Gdx.files.internal("2.png"));
background = new TextureRegionDrawable(new TextureRegion(texture, 0, 0,128,128));
knobRegion = new TextureRegionDrawable(new TextureRegion(texture,128,0,128,128));
style = new TouchpadStyle(background, knobRegion);
touchPad = new Touchpad(15, style);//初始化游戏摇杆。(摇杆触碰区域的半径大小,TouchPagStyle)
touchPad.setBounds(0, 0, 150, 150);//设置摇杆的位置和大小
batch = new SpriteBatch();
stage.addActor(touchPad);
Gdx.input.setInputProcessor(stage);
}
public void update(){
if(touchPad.isTouched()){//判断摇杆是否被按下
x += touchPad.getKnobPercentX()*speed;//改变相应的坐标.
y += touchPad.getKnobPercentY()*speed;
}
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
update();//这个方法别漏了
batch.begin();
batch.draw(killer, x,y, 70,70);
batch.end();
stage.act();
stage.draw();
}
@Override
public void resize(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
}
三、效果图
四、源码下载
http://download.youkuaiyun.com/detail/caihongshijie6/7009921