2017-06-22 19 views

答えて

0

、どのように私はこれを行うことができ、このテクスチャのためのタッチイベントを追加する必要があります。この上ガイドはhere

が示されているか私の好ましい方法は、以下のコードは、上でクリックしたときに画像を変更する単一のボタンの一例であり、このLibGDX simple button with image

に説明するのImageButtonをscene2dを使用し、使用することです。

package com.mygdx.gtest; 

import com.badlogic.gdx.ApplicationAdapter; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.graphics.Color; 
import com.badlogic.gdx.graphics.Pixmap; 
import com.badlogic.gdx.graphics.Pixmap.Format; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.TextureRegion; 
import com.badlogic.gdx.scenes.scene2d.Stage; 
import com.badlogic.gdx.scenes.scene2d.ui.Button; 
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle; 
import com.badlogic.gdx.scenes.scene2d.ui.Table; 
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; 

public class Test extends ApplicationAdapter{ 

    private Stage stage; 

    @Override 
    public void render() { 
     super.render(); 
     stage.act(); 
     stage.draw(); 
    } 

    @Override 
    public void create() { 
     //create images 
     Pixmap pmap = new Pixmap(100,100,Format.RGBA4444); 
     pmap.setColor(Color.RED); 
     pmap.fill(); 
     Texture buttonUpRed = new Texture(pmap); 
     pmap.setColor(Color.BLUE); 
     pmap.fill(); 
     Texture buttonDownBlue = new Texture(pmap); 
     pmap.dispose(); 

     //make button style (usually done with skin) 
     ButtonStyle tbs = new ButtonStyle(); 
     tbs.down = new TextureRegionDrawable(new TextureRegion(buttonDownBlue)); 
     tbs.up = new TextureRegionDrawable(new TextureRegion(buttonUpRed)); 

     // make button 
     Button btn = new Button(tbs); 

     //make stage 
     stage = new Stage(); 

     // ad button to layout table 
     Table table = new Table(); 
     table.add(btn); 

     //add table to stage 
     stage.addActor(table); 

     //set the stage to be the input processor so it responds to clicks 
     Gdx.input.setInputProcessor(stage); 

    } 

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

私は[リンク] https://gamedev.stackexchange.com/のようでした質問/ 121115/libgdx-simple-button-with-imageですが、ポジションをステージに設定する方法は? –

+0

Table、Stack、VerticalGroupなどのレイアウトコンポーネントを使用することができます。それらの使い方や使い方に関する素晴らしい説明は、LibGdx wiki https://github.com/libgdx/libgdx/libgdx/wiki/Scene2d.uiにあります。 #レイアウト – dfour

0

あなたはタッチ検出のための代わりにテクスチャのSpriteまたはImageを使用する必要があります。

Spriteクラスは、テクスチャ領域、描画されるジオメトリ、描画される色の両方を記述します。

scene2dを使用する場合は、Imageを使用できます。Imageは単純に描画可能オブジェクトを表示します。描画可能あなたはこの方法でタッチを検出することができますテクスチャ、テクスチャ領域、ninepatch、スプライトなど

ことができます。

public class TouchSystem extends ApplicationAdapter implements InputProcessor { 

    private SpriteBatch batch; 

    Texture up,down; 
    Sprite upSprite,downSprite; 
    OrthographicCamera camera; 
    Vector3 temp; 

    @Override 
    public void create() { 

     temp=new Vector3(); 
     camera=new OrthographicCamera(); 
     camera.setToOrtho(false,Gdx.graphics.getWidth(),Gdx.graphics.getHeight()); 

     batch=new SpriteBatch(); 
     up=new Texture("top_arrow.png"); 
     down=new Texture("down_arrow.png"); 

     upSprite=new Sprite(up); 

     downSprite=new Sprite(down); 
     downSprite.setPosition(200,100); 

     Gdx.input.setInputProcessor(this); 
    } 

    @Override 
    public void render() { 

     Gdx.gl.glClearColor(0, 0, 0, 1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

     batch.setProjectionMatrix(camera.combined); 
     batch.begin(); 
     upSprite.draw(batch); 
     downSprite.draw(batch); 
     batch.end(); 
    } 

    @Override 
    public void dispose() { 

     up.dispose(); 
     down.dispose(); 
     batch.dispose(); 
    } 

    @Override 
    public void resume() { 
     super.resume(); 
    } 

    @Override 
    public boolean keyDown(int keycode) { 
     return false; 
    } 

    @Override 
    public boolean keyUp(int keycode) { 
     return false; 
    } 

    @Override 
    public boolean keyTyped(char character) { 
     return false; 
    } 

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

     temp.set(screenX,screenY,0); 
     camera.unproject(temp); 

     if(upSprite.getBoundingRectangle().contains(temp.x,temp.y)) 
      System.out.println("Touch on upSprite"); 

     if(downSprite.getBoundingRectangle().contains(temp.x,temp.y)) 
      System.out.println("Touch on downSprite"); 

     return false; 
    } 

    @Override 
    public boolean touchUp(int screenX, int screenY, int pointer, int button) { 
     return false; 
    } 

    @Override 
    public boolean touchDragged(int screenX, int screenY, int pointer) { 
     return false; 
    } 

    @Override 
    public boolean mouseMoved(int screenX, int screenY) { 
     return false; 
    } 

    @Override 
    public boolean scrolled(int amount) { 
     return false; 
    } 
} 
関連する問題