【问题标题】:How to handling a event touch for images?如何处理图像的事件触摸?
【发布时间】:2020-01-25 08:06:31
【问题描述】:
  • 我有以下代码,但是当我触摸图像时,应用程序会崩溃。
  • 我想在触摸它之后,在某处给我画一个字。

【问题讨论】:

  • 代码在哪里?
  • 将您的代码放入您的 Question not in answer 中。我建议您在 stackOverflow 上查看 how to ask
  • @AjayKS 代码如下
  • @AlirezaBideli 代码如下

标签: java android events libgdx touch


【解决方案1】:
    SpriteBatch batch;
    int x;
    Sprite img;
    float w,h,tw,th=0;
    Camera camera;
    BitmapFont bf;

    @Override
    public void create()
    {
        bf=new BitmapFont();
        w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getHeight();
        texture = new Texture(Gdx.files.internal("android.jpg"));
        batch = new SpriteBatch();
        camera = new OrthographicCamera(w, h);
        camera.position.set(w/2, h/2, 0);
        camera.update();
        img = new Sprite(texture);
        tw = img.getWidth();
        th = img.getHeight();
        img.setBounds( camera.position.x - (tw/2), camera.position.y - (th/2),tw,th);
        Gdx.input.setInputProcessor(new InputAdapter(){

                @Override
                public boolean touchDown(int screenX, int screenY, int pointer, int button) {

                    if(img.getBoundingRectangle().contains(screenX, screenY))
                        System.out.println("Image Clicked");
                    bf.draw(batch,"click",200,200);

                    return true;
                }

            });
    }

    @Override
    public void render()
    {        
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw(texture,  x, 0);

        x++;
        if(x>=Gdx.graphics.getWidth())x=0;
        batch.end();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2014-03-19
    • 2012-01-12
    • 2016-05-28
    相关资源
    最近更新 更多