2014-10-28 77 views
2

請原諒我的英文。libGDX演員draw()不叫

開始探索libGDX並遇到問題。當我在舞臺上添加actor時,方法draw()不會被調用。 試圖應用該方法繪製直線,成功繪製紋理,但它不是演員,並且此方法不正確。

請幫忙。

SpiderHunt.java

package com.spiderhunt; 

import com.badlogic.gdx.Game; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.spiderhunt.screens.MainMenu; 

    public class SpiderHunt extends Game{ 
     private SpriteBatch batch; 
     public MainMenu mainMenu; 

     private static SpiderHunt instance = new SpiderHunt(); 

     public SpiderHunt(){ 

     } 

     public static SpiderHunt getInstance() { 
      return instance; 
     } 

     public void create() {  

      //load textures 
      Assets.load(); 

      batch = new SpriteBatch(); 

      mainMenu = new MainMenu(batch); 
      this.setScreen(mainMenu); 
     } 

     public void showMainMenu(){  
      setScreen(mainMenu); 
     } 

     public void render (float delta) { 

     } 

     public void resize(int width, int height) { 

     } 

     public void pause() { 

     } 

     public void resume() { 

     } 

     public void dispose() { 

     } 
    } 

MainMenu.java

package com.spiderhunt.screens; 

import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.Screen; 
import com.badlogic.gdx.graphics.GL20; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.scenes.scene2d.Group; 
import com.badlogic.gdx.scenes.scene2d.InputEvent; 
import com.badlogic.gdx.scenes.scene2d.Stage; 
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; 
import com.badlogic.gdx.utils.viewport.StretchViewport; 
import com.spiderhunt.Assets; 
import com.spiderhunt.buttons.btnPlay; 

public class MainMenu implements Screen { 

    public btnPlay playButton; 
    public Stage stage; 
    public SpriteBatch batch;  

    class GoToGameListener extends ClickListener { 
     @Override 
     public void clicked(InputEvent event, float x, float y) { 
      //some code for click or push 
     } 
    } 

    public MainMenu(SpriteBatch batch_1) { 
     batch = batch_1; 

     stage = new Stage(new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()), batch); 
     Gdx.input.setInputProcessor(stage); 

     playButton = new btnPlay(); //make actor 

     stage.addActor(playButton);  
    }  

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

     //make background 
     batch.begin();   
     batch.draw(Assets.bgMenuRegion, 0, 0, 540, 960); 
     batch.end(); 

     stage.act(Gdx.graphics.getDeltaTime()); 
     stage.draw(); //this action must do method draw() from actor but it not called !!!!!!!!!! 

     //batch.begin(); 
     //playButton.draw(batch, 0); THIS CODE DRAW BUTTON, BUT IT NOT CORRECTLY ACTOR 
     //batch.end(); 
    } 

    @Override 
    public void resize(int width, int height) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void show() { 
     Gdx.input.setInputProcessor(stage); 
    } 

    @Override 
    public void hide() { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void pause() { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void resume() { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void dispose() { 
     // TODO Auto-generated method stub 

    } 

} 

btnPlay.java

Assets.java

package com.spiderhunt; 

import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.TextureRegion; 

public class Assets { 
    public static Texture atlas;  
//backgrounds 
    public static TextureRegion bgMenuRegion; 
    public static TextureRegion bgSelectLevelRegion; 
//buttons 
    public static TextureRegion btnPlayRegion; 
//objects 
    public static TextureRegion objFlyRegion; 

    public static void load(){ 
     atlas = new Texture("atlas.png"); 

     bgMenuRegion = new TextureRegion(atlas, 0, 0, 540, 960); 
     btnPlayRegion = new TextureRegion(atlas, 1111, 1244, 418, 112); 
    } 
} 
+0

「不叫」 在你的代碼不執行或沒有什麼可見的?你是否嘗試過在你的按鈕的繪製調用周圍'batch.begin/end'? – cfrick 2014-10-28 20:32:04

+0

,你已經用空方法覆蓋了你的'Game'類中的所有方法。對於較短的代碼?否則什麼都不會呈現。 – cfrick 2014-10-28 21:00:17

回答

5

感謝您的幫助。

現在我發現我的錯誤。

我將SpriteBatch替換爲批處理,它工作。

變化

public void draw(SpriteBatch batch, float parentAlpha) {  
     //!!!!!!!!!!!!!!!Error in this place. Draw() not called from stage 
     batch.setColor(getColor());   
     batch.draw(Assets.btnPlayRegion, 0, 0); 
} 

@Override 
public void draw(Batch batch, float parentAlpha) {  
     //!!!!!!!!!!!!!!!Error in this place. Draw() not called from stage 
     batch.setColor(getColor());   
     batch.draw(Assets.btnPlayRegion, 0, 0); 
} 
+0

+1,解決了自己的問題 – 2014-10-28 20:58:46

+0

不錯。我記得在升級舊項目的Libgdx庫時遇到這樣的問題 – z3n105 2014-10-28 20:59:25

+5

這是'@ Override'關鍵字可以提供幫助的地方。把它放在任何你壓倒一切的方法之前,它會在你沒有把握的時候發出警告。 – Tenfour04 2014-10-28 22:35:38

0

我相信這個問題是在這一行:

new Stage(new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()), batch); 

從舞臺初始化刪除批量更改如下:

new Stage(new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight())); 

你從屏幕傳遞批次的階段,你是在stage.draw()之前調用batch.end()。然而舞臺有它自己的批次,所以你不必通過批處理來繪製舞臺。如果由於你自己的原因(例如批處理投影矩陣配置),你仍然希望你的屏幕批處理被傳遞到舞臺,不要在stage.draw()之前調用batch.end(),因爲批處理階段和主批次是相同的。在stage.draw()後調用batch.end()