[LIBGDX学习经验]-Touchpad类,摇杆完整篇(附带项目)

还是我,不过换了blog。

嗯,本人偷懒,在原来的项目上改了点儿东西。简洁了许多。

关于配置和一些用法,传送门:http://www.cnblogs.com/esdesign/archive/2013/05/10/3071670.html  

我在摇杆上投入的心思也不算很多,一天左右,看了看官方的API文档,悲剧的发现了一个问题。

我们要监听摇杆啊?对不对,他摇杆摇了,我得要有个事件通知我。然后再实现自己想要的功能。

我做了一个小测试:

Touchpad touchpad = new Touchpad(0f, skin);
        
touchpad.setPosition(0, 0);
        
touchpad.setSize(300, 300);    
touchpad.addListener(new InputListener(){

            @Override
            public boolean touchDown(InputEvent event, float x, float y,
                    int pointer, int button) {
                // TODO Auto-generated method stub
                Gdx.app.log("DOWN", x+":"+y);
                return super.touchDown(event, x, y, pointer, button);
            }

            @Override
            public void touchUp(InputEvent event, float x, float y,
                    int pointer, int button) {
                // TODO Auto-generated method stub
                Gdx.app.log("UP", x+":"+y);
                super.touchUp(event, x, y, pointer, button);
            }

            @Override
            public void touchDragged(InputEvent event, float x, float y,
                    int pointer) {
                // TODO Auto-generated method stub
                Gdx.app.log("DRAG", x+":"+y);
                super.touchDragged(event, x, y, pointer);
            }
            
            
            
        });

我们会惊奇的发现。控制台输出的内容只有  DOWN,坐标值。

我们想象中的UP ,和 DRAG 根本没实现。到底是不是我做错了什么~好吧,我承认我懒,没有深入研究下去。做了一个取巧的方法。

自己写了一个Touchpad,当然,继承于libgdx的Touchpad,代码如下

package com.cavacn.game;

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

public class cavaTouchpad extends Touchpad {

    private cavaTouchEvent event;
    
    public void addCavaListenner(cavaTouchEvent listener){

        event = listener;
        
    }
    
    public void draw(SpriteBatch batch, float parentAlpha) {
        
        if(event!=null){
            
            if(this.isTouched()){
            
                event.handle();
            
            }
        }
        
        // TODO Auto-generated method stub
        super.draw(batch, parentAlpha);
    }

    public cavaTouchpad(float deadzoneRadius, Skin skin, String styleName) {
        super(deadzoneRadius, skin, styleName);
        // TODO Auto-generated constructor stub
    }

    public cavaTouchpad(float deadzoneRadius, Skin skin) {
        super(deadzoneRadius, skin);
        // TODO Auto-generated constructor stub
    }

    public cavaTouchpad(float deadzoneRadius, TouchpadStyle style) {
        super(deadzoneRadius, style);
        // TODO Auto-generated constructor stub
    }

    


}

当然,在这之前我们需要写一个类似委托的东西

package com.cavacn.game;


import com.badlogic.gdx.scenes.scene2d.ui.Touchpad;

public class cavaTouchEvent {
    
    private Touchpad touchpad;
    
    public cavaTouchEvent(Touchpad touchpad){
        
        this.touchpad = touchpad;
        
    }
    
    public boolean handle() {
        
        if(this.touchpad==null){return false;}
        
        cavaTouchDragged(this.touchpad.getKnobPercentX(),this.touchpad.getKnobPercentY());
        
        return true;
    }
    
    public void cavaTouchDragged(float x,float y){
        
    }

    
    
}

我就这样,自己添加了两个类。然后场景实现代码如下:

package com.cavacn.game;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Touchpad;

public class cavaGame implements ApplicationListener {
    
    
    private Stage stage;

    
    @Override
    public void create() {        
    
        float width = 1280f;
        
        float height = 720f;
        
        stage = new Stage(width,height,false);
        
        Skin skin = new Skin(Gdx.files.internal("ui/uiskin.json"));
        
        /*Touchpad touchpad = new Touchpad(0f, skin);
        
        touchpad.setPosition(0, 0);
        
        touchpad.setSize(300, 300);*/

        cavaTouchpad touchpad = new cavaTouchpad(0f, skin);
        
        touchpad.setPosition(0, 0);
        
        touchpad.setSize(400, 400);
        
        touchpad.addCavaListenner(new cavaTouchEvent(touchpad){

            @Override
            public void cavaTouchDragged(float x,float y) {
                // TODO Auto-generated method stub
                Gdx.app.log("CAVACN", x+":"+y);
            }
            
            
            
        });
        
        /*touchpad.addListener(new InputListener(){

            @Override
            public boolean touchDown(InputEvent event, float x, float y,
                    int pointer, int button) {
                // TODO Auto-generated method stub
                Gdx.app.log("DOWN", x+":"+y);
                return super.touchDown(event, x, y, pointer, button);
            }

            @Override
            public void touchUp(InputEvent event, float x, float y,
                    int pointer, int button) {
                // TODO Auto-generated method stub
                Gdx.app.log("UP", x+":"+y);
                super.touchUp(event, x, y, pointer, button);
            }

            @Override
            public void touchDragged(InputEvent event, float x, float y,
                    int pointer) {
                // TODO Auto-generated method stub
                Gdx.app.log("DRAG", x+":"+y);
                super.touchDragged(event, x, y, pointer);
            }
            
            
            
        });*/
        
        stage.addActor(touchpad);
    }

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

    @Override
    public void render() {    
        
        Gdx.input.setInputProcessor(stage);
        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        stage.act();
        stage.draw();
    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }
}

非常简单的一个Demo。不过总算我们可以在控制台当中看到  CAVACN  坐标值

 

特别声明,我这里出来的结果 x 是 the x-position of the knob as a percentage from the center of the touchpad to the edge of the circular movement area.

                                      y 是 the y-position of the knob as a percentage from the center of the touchpad to the edge of the circular movement area.

从字面上理解是,x和y都是由中心点为原点的正负百分比。

到了文章的最后,我把demo发给大家,传送门:http://pan.baidu.com/share/link?shareid=413706&uk=2936223302

posted @ 2013-05-10 22:42  咔哇世界  阅读(1058)  评论(0编辑  收藏  举报