2015-04-16 36 views
0

我對libgdx框架頗爲陌生,所以我希望我不要求任何愚蠢的東西,但是我在更新toucheEvent上的屏幕時遇到了問題。看起來觸摸事件觸發,但舞臺沒有更新,所以屏幕始終是相同的。下面是代碼Libgdx sceene2d和輸入處理器不更新屏幕

MainClass

public class MainGame implements Screen { 
public LabirintGame game; 
public Stage stage; 
public OrthographicCamera camera; 
public ActorM rigth; 
public ActorM wrong; 

public MainGame(LabirintGame game) { 
    this.game = game; 
    this.camera = new OrthographicCamera(); 

} 


@Override 
public void show() { 
    this.camera.setToOrtho(false, 800, 480); 
    stage = new Stage(new ScreenViewport()); 
    stage.clear(); 
    Words group = new Words(stage); 
    InputMultiplexer inputMultiplexer = new InputMultiplexer(); 

    inputMultiplexer.addProcessor(stage); 
    inputMultiplexer.addProcessor(new MyInputProcessor(stage, camera)); 
    Gdx.input.setInputProcessor(inputMultiplexer); 
    //Add wrong and rigth boxes 
    rigth = new ActorM("box", 0, 0, 200,200); 
    wrong = new ActorM("box",(game.width - 230), 0, 200, 200); 
    wrong.moveBy(200,200); 

    Button createButtons = new Button(); 
    createButtons.setStyle("atlas-besede/besede.atlas", "buttonOff", "buttonOn"); 
    TextButton ValidationButton = createButtons.createButton("Validate", (game.width/2), 0, 150, 150); 
    ValidationButton.addListener(new InputListener() { 
     public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { 
      game.setScreen(new Labirint(game)); 
      return true; 
     } 

    }); 


    stage.addActor(ValidationButton); 
    stage.addActor(rigth); 
    stage.addActor(wrong); 


    List<String> backgrounds = Arrays.asList("s", "z"); 
    for (int i = 0; i < backgrounds.size(); i++) { 
     Word actor = new Word(backgrounds.get(i),(i + 1) * 300, 300, 100, 100); 
     actor.setPosition((i + 1) * 300, 300); 
     actor.setName(backgrounds.get(i)); 
     group.addActor(actor); 
    } 
    stage.addActor(group); 
} 

@Override 
public void render(float delta) { 
    Gdx.gl.glClearColor(1, 0, 0, 1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    stage.act(Gdx.graphics.getDeltaTime()); 

    stage.draw(); 
    game.batch.begin(); 
    game.batch.setProjectionMatrix(camera.combined); 
    game.batch.end(); 

} 

ActorM

package com.mygdx.game; 

public class ActorM extends Actor { 
public SpriteBatch batch; 
public TextureAtlas atlas; 
public TextureAtlas.AtlasRegion region; 
Sprite sprite; 
public int x; 
public int y; 
public int width; 
public int height; 

public ActorM(String actorName, int x, int y, int width, int height) { 
    //this.region = region; 
    super(); 
    batch = new SpriteBatch(); 
    atlas = new TextureAtlas(Gdx.files.internal("atlas-start/atlas-start.atlas")); 
    sprite = atlas.createSprite(actorName); 
    this.width = width; 
    this.height = height; 
    this.x = x; 
    this.y = y; 
    this.setBounds(0, 0, sprite.getWidth(), sprite.getHeight()); 
    setTouchable(Touchable.disabled); 
    setName(actorName); 
    setPosition(x,y); 
} 

@Override 
public void draw (Batch batch, float parentAlpha) { 
    batch.draw(sprite, x,y, width, height); 
} 
public void move(int posX){ 
    this.x = this.x + posX; 
} 

}

MyInputProcessor

public class MyInputProcessor implements InputProcessor { 
private OrthographicCamera camera; 
private Stage stage; 
private Vector2 coordinates; 
private Music sound; 
public MyInputProcessor(Stage stage, OrthographicCamera camera) { 
    this.stage = stage; 
    this.camera = camera; 
} 

@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)   

    { 
     //Gdx.app.log("", "x " + screenX + " y " + screen`enter code here`Y + " pointer " + pointer); 
     Vector2 coordinates = stage.screenToStageCoordinates(new Vector2((float)screenX,(float)screenY)); 
     Actor hitactor = stage.hit(coordinates.x, coordinates.y, true); 
     Gdx.app.log("", coordinates.toString()); 
     if (hitactor != null){ 
      //Gdx.app.log("", "HIT" + hitactor.getName()); 
      Gdx.app.log("", "HIT" + hitactor.getRotation()); 
      hitactor.setRotation(hitactor.getRotation() + 1f); 
      hitactor.setPosition(5,5); 
      Gdx.gl.glClearColor(1, 0, 0, 1); 
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
     } 
     return true; 
    } 

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

    @Override 
    public boolean touchDragged(int screenX, int screenY, int pointer) { 
     coordinates = stage.screenToStageCoordinates(new Vector2((float)screenX,(float)screenY)); 
     Actor hitactor = stage.hit(coordinates.x, coordinates.y, true); 

     if (hitactor != null){ 
      Gdx.app.log("", "Drag"); 
      hitactor.setRotation(hitactor.getRotation() + 1f); 
     } 
     camera.update(); 
     return true; 

    } 

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

    @Override 
    public boolean scrolled(int amount) { 
     return false; 
    } 
} 
+0

我看不到您設置舞臺尺寸的位置。你正在創造一個相機,但你把它分配到舞臺? – z3n105

回答

0

你 「ValidationButton」 使用一個InputProcessor日總是迴歸真實,這是舞臺上的第一位演員,所以舞臺上沒有其他人能夠有機會迴應觸地事件。此外,由於您的舞臺是您InputMultiplexer中的第一個輸入處理器,因此您的其他輸入處理器也從來沒有機會響應觸擊事件。

你應該在你的按鈕上使用EventListener而不是InputListener,所以很多邏輯都會爲你照顧。

順便說一下,你的ActorM類產生了一個它永遠不會使用的SpriteBatch。 SpriteBatch佔用了大量的內存,並且不需要在遊戲中存在多於一個的內存。舞臺已經有一個SpriteBatch的引用,它傳遞給你的Actor的draw方法,所以Actor不需要創建或者引用一個SpriteBatch。

另外,您的ActorM類正在爲自身加載一個TextureAtlas的完整副本,因此將爲每個ActorM實例加載重複的紋理,並且您將失去TextureAtlas的所有優點,因爲您不會使用它爲精靈分批。您只需要加載TextureAtlas一次,並將其引用傳遞到您的ActorM類的構造函數中,以便它們都可以共享相同的Texture。

+0

感謝您的回答和提示,但問題不在於ValidationButton,因爲我可以訪問這些事件。問題是,當我去更新對象的屏幕位置改變,但屏幕沒有更新。 –