libgdx 游戏摇杆

来源:互联网 发布:c语言算法 指导书 编辑:程序博客网 时间:2024/05/01 06:25

核心代码

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 FirstGame implements ApplicationListener {   Stage stage;   Touchpad touchpad;   Texture texture;   Texture killer;   SpriteBatch batch;      TextureRegionDrawable background;   TextureRegionDrawable knobRegion;   TouchpadStyle touchpadStyle;   int speed = 3;   int x = 0;   int y = 0;   @Override   public void create() {       stage = new Stage();              Gdx.input.setInputProcessor(stage);              batch = new SpriteBatch();              texture = new Texture(Gdx.files.internal("data/touchpad.png"));              killer = new Texture(Gdx.files.internal("data/2.png"));              background = new TextureRegionDrawable(new TextureRegion(texture, 0, 0,               128, 128));       knobRegion = new TextureRegionDrawable(new TextureRegion(texture, 128,               0, 128, 128));              touchpadStyle = new TouchpadStyle(background, knobRegion);              touchpad = new Touchpad(15, touchpadStyle);              touchpad.setBounds(0, 0, 150, 150);          stage.addActor(touchpad);   }   public void update() {       if (touchpad.isTouched()) {           x += touchpad.getKnobPercentX() * speed;           y += touchpad.getKnobPercentY() * speed;       }   }   @Override   public void dispose() {   }   @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, 50, 50);       batch.end();              stage.act();       stage.draw();   }   @Override   public void resize(int width, int height) {   }   @Override   public void pause() {   }   @Override   public void resume() {   }}


0 0
原创粉丝点击