liggdx 游戏摇杆类

来源:互联网 发布:2016年饮料行业数据 编辑:程序博客网 时间:2024/05/01 13:46

运行结果


核心代码:

package com.example.androidgame12mapnew;import javax.microedition.khronos.opengles.GL10;import com.badlogic.gdx.ApplicationListener;import com.badlogic.gdx.Gdx;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 {SpriteBatch batch;Stage stage;Touchpad touchpad;float x = 10;float y = 10;float speed = 1;Texture show1;@Overridepublic void create() {stage = new Stage();batch = new SpriteBatch();show1 = new Texture(Gdx.files.internal("data/show1.png"));TextureRegionDrawable background = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("data/touchpad_bg.png"))));TextureRegionDrawable knob = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("data/touchpad_knob.png"))));TouchpadStyle touchpadStyle = new TouchpadStyle(background, knob);touchpad = new Touchpad(3, touchpadStyle);touchpad.setSize(touchpad.getWidth(), touchpad.getHeight());touchpad.setPosition(touchpad.getWidth() / 4, 0);Gdx.input.setInputProcessor(stage);stage.addActor(touchpad);}public void update() {if (touchpad.isTouched()) {x += touchpad.getKnobPercentX() * speed;y += touchpad.getKnobPercentY() * speed;if (x < 20)x = 20;if (x > Gdx.graphics.getWidth() - 120)x = Gdx.graphics.getWidth() - 120;if (y < 20)y = 20;if (y > Gdx.graphics.getHeight() - 120)y = Gdx.graphics.getHeight() - 120;}}@Overridepublic void render() {Gdx.gl.glClearColor(1, 1, 1, 1);Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);update();batch.begin();batch.draw(show1, x, y, 100, 100);batch.end();stage.act();stage.draw();}@Overridepublic void resize(int arg0, int arg1) {// TODO Auto-generated method stub}@Overridepublic void resume() {// TODO Auto-generated method stub}@Overridepublic void dispose() {// TODO Auto-generated method stub}@Overridepublic void pause() {// TODO Auto-generated method stub}}

封装的摇杆工具类



0 0
原创粉丝点击