libgdx多点触摸

来源:互联网 发布:坏男孩软件靠谱吗 编辑:程序博客网 时间:2024/04/29 18:18

多点触摸。点中两个按钮,同时放开两个手指的时候,只有一个touchUp响应。在ios上尤其明显。解决方案:每隔一段时间就检查当前屏幕有几个手指处于按压状态。


package test.touch;import com.badlogic.gdx.Gdx;import com.badlogic.gdx.InputProcessor;import com.badlogic.gdx.graphics.GL20;import com.badlogic.gdx.graphics.g2d.Batch;import com.badlogic.gdx.scenes.scene2d.Actor;import com.badlogic.gdx.scenes.scene2d.Group;import com.badlogic.gdx.utils.Pools;/** * Created by apple on 16/9/8. */public class MultiTouch extends Group implements InputProcessor {    private static final String  TAG        = MultiTouch.class.getName();    private              Actor   btnQuit    = null;    private              Actor   btnRestart = null;    private              boolean isTouchUp  = false;    private              float   timer      = 0;    public MultiTouch() {        super();        setSize(1280, 800);        setPosition(0, 0);    }    @Override    public void act(float delta) {        super.act(delta);        if (isTouchUp) {            timer += delta;            if (timer > 5) {                timer = 0;                StringBuffer buffer = Pools.obtain(StringBuffer.class);                buffer.setLength(0);                buffer.append("isTouched = ").append(Gdx.input.isTouched());                for (int i = 0; i < 4; i++) {                    buffer.append(", i =")                          .append(i)                          .append(", ")                          .append(Gdx.input.isTouched(i));                }                MyLog.log(buffer.toString());                Pools.free(buffer);            }        }    }    @Override    public void draw(Batch batch, float parentAlpha) {        Gdx.gl20.glBlendColor(1, 1, 1, 1);        Gdx.gl20.glClear(GL20.GL_ALPHA);        super.draw(batch, parentAlpha);    }    public boolean keyDown(int keycode) {        String hint = "keyDown";        MyLog.log(TAG, hint, keycode);        return false;    }    public boolean keyUp(int keycode) {        String hint = "keyUp";        MyLog.log(TAG, hint, keycode);        return false;    }    public boolean keyTyped(char character) {        String hint = "keyTyped";        MyLog.log(TAG, hint, character);        return false;    }    public boolean touchDown(int screenX, int screenY, int pointer, int button) {        String hint = "touchDown";        MyLog.log(TAG, hint, pointer, button, screenX, screenY);        isTouchUp = false;        return false;    }    public boolean touchUp(int screenX, int screenY, int pointer, int button) {        String hint = "touchUp";        MyLog.log(TAG, hint, pointer, button, screenX, screenY);        isTouchUp = true;        return false;    }    public boolean touchDragged(int screenX, int screenY, int pointer) {        String hint = "touchDragged";        MyLog.log(TAG, hint, pointer);        return false;    }    public boolean mouseMoved(int screenX, int screenY) {        String hint = "mouseMoved";        MyLog.log(TAG, hint);        return false;    }    public boolean scrolled(int amount) {        String hint = "scrolled";        MyLog.log(TAG, hint, amount);        return false;    }}

package test.touch;import com.badlogic.gdx.utils.Pools;/** * Created by apple on 16/9/9. */public class MyLog {    public static void log(Object... hints) {        StringBuffer buffer = Pools.obtain(StringBuffer.class);        buffer.setLength(0);        for (Object o : hints) {            if (buffer.length() != 0) {                buffer.append(" ");            }            String s = "";            if (o == null) {                s = "null";            } else if (o instanceof String) {                s = (String) o;            } else {                s = o.toString();            }            buffer.append(s);        }        System.out.print(buffer.toString() + "\r\n");        Pools.free(buffer);    }}


多按钮触摸,同时抬起只有一个touchUp响应的状况。 解决方案:用

Gdx.input.isTouched(i)

检测有几根手指按压屏幕,每根手指的代号参照touchDown中的point参数。


package test.touch;import com.badlogic.gdx.Gdx;import com.badlogic.gdx.graphics.Color;import com.badlogic.gdx.graphics.GL20;import com.badlogic.gdx.graphics.g2d.Batch;import com.badlogic.gdx.scenes.scene2d.Actor;import com.badlogic.gdx.scenes.scene2d.Group;import com.badlogic.gdx.scenes.scene2d.InputEvent;import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;import com.badlogic.gdx.utils.Pools;import dy.gdx.util.DrawShapUtil;/** * Created by apple on 16/9/8. */public class MultiTouchB extends Group {    private static final String  TAG        = MultiTouchB.class.getName();    private              Actor   btnQuit    = null;    private              Actor   btnRestart = null;    private              boolean isTouchUp  = false;    private              float   timer      = 0;    public MultiTouchB() {        super();        setSize(1280, 800);        setPosition(0, 0);        initial();    }    private void initial() {        final String hint = "initial";        btnQuit = new Actor();        btnQuit.setSize(300, 200);        btnQuit.setPosition(100, 200);        btnRestart = new Actor();        btnRestart.setSize(300, 200);        btnRestart.setPosition(btnQuit.getX() + btnQuit.getWidth() + 100, 200);        addActor(btnRestart);        addActor(btnQuit);        btnRestart.addListener(new ClickListener() {            final String btnName = "btnRestart";            @Override            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {                MyLog.log(TAG, hint, btnName, ", touchDown ", "pointer = ", pointer, ", button = ", button);                isTouchUp = false;                return super.touchDown(event, x, y, pointer, button);            }            @Override            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {                MyLog.log(TAG, hint, btnName, ", touchUp ", "pointer = ", pointer, ", button = ", button);                super.touchUp(event, x, y, pointer, button);                isTouchUp = true;            }        });        btnQuit.addListener(new ClickListener() {            final String btnName = "btnQuit";            @Override            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {                MyLog.log(TAG, hint, btnName, ", touchDown ", "pointer = ", pointer, ", button = ", button);                isTouchUp = false;                return super.touchDown(event, x, y, pointer, button);            }            @Override            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {                MyLog.log(TAG, hint, btnName, ", touchUp ", "pointer = ", pointer, ", button = ", button);                isTouchUp = true;                super.touchUp(event, x, y, pointer, button);            }        });    }    @Override    public void act(float delta) {        super.act(delta);        timer += delta;        if (timer > 5) {            timer = 0;            StringBuffer buffer = Pools.obtain(StringBuffer.class);            buffer.setLength(0);            buffer.append("isTouched = ").append(Gdx.input.isTouched());            for (int i = 0; i < 4; i++) {                buffer.append(", i =")                      .append(i)                      .append(", ")                      .append(Gdx.input.isTouched(i));            }            MyLog.log(buffer.toString());            Pools.free(buffer);        }    }    @Override    public void draw(Batch batch, float parentAlpha) {        Gdx.gl20.glBlendColor(1, 1, 1, 1);        Gdx.gl20.glClear(GL20.GL_ALPHA);        super.draw(batch, parentAlpha);        Actor[] actors = {btnRestart, btnQuit};        for (Actor child : actors) {            if (child.getParent() == this) {                DrawShapUtil.drawEdge(batch, child, Color.GREEN);            }        }    }}


package dy.gdx.util;import com.badlogic.gdx.graphics.Camera;import com.badlogic.gdx.graphics.Color;import com.badlogic.gdx.graphics.g2d.Batch;import com.badlogic.gdx.graphics.glutils.ShapeRenderer;import com.badlogic.gdx.math.Vector2;import com.badlogic.gdx.scenes.scene2d.Actor;/** * Created by apple on 16/9/6. * 用来绘制图形(不是贴纹理,是绘制图形) */public class DrawShapUtil {    private static ShapeRenderer render      = new ShapeRenderer();    private static Vector2       leftBottom  = new Vector2();    private static Vector2       leftTop     = new Vector2();    private static Vector2       rightTop    = new Vector2();    private static Vector2       rightBottom = new Vector2();    /**     * 将actor的边缘绘制出来,调试的时候方便定位     *     * @param batch     * @param actor     * @param color     */    public static void drawEdge(Batch batch, Actor actor, Color color) {        if (actor == null) {            return;        }        batch.end();        Camera camera = actor.getStage().getCamera();        camera.update();        render.setProjectionMatrix(camera.combined);        render.setAutoShapeType(true);        render.begin(ShapeRenderer.ShapeType.Line);        leftBottom = actor.localToStageCoordinates(leftBottom.set(0, 0));        leftTop = actor.localToStageCoordinates(leftTop.set(0, actor.getHeight()));        rightTop = actor.localToStageCoordinates(rightTop.set(actor.getWidth(), actor.getHeight()));        rightBottom = actor.localToStageCoordinates(rightBottom.set(actor.getWidth(), 0));        render.setColor(color);        render.line(leftBottom.x, leftBottom.y, leftTop.x, leftTop.y);        render.line(leftTop.x, leftTop.y, rightTop.x, rightTop.y);        render.line(rightTop.x, rightTop.y, rightBottom.x, rightBottom.y);        render.line(rightBottom.x, rightBottom.y, leftBottom.x, leftBottom.y);//        render.flush();        render.end();        batch.begin();    }}


0 0
原创粉丝点击