使用按钮时,程序关闭,libgdx

时间:2014-05-28 00:55:08

标签: android libgdx

我遇到了我的按钮问题。我的代码如下。

public class rarsterinco implements ApplicationListener {
    Texture background;
    Texture background2;
    Texture background3;
    Vector2 backgroundmove;
    Vector2 backgroundmove2;
    SpriteBatch batch;
    Rectangle bounds;
    CharSequence line = "got touched."; // = myScore;
    Rectangle bounds2;
    boolean check;
    boolean collision;
    int beginning;
    CharSequence headup = "got touched";
    Vector2 pos;
    Vector2 position;
    Vector2 size;
    Vector2 size2;
    BitmapFont font;
    Texture mysteryCloud;
    Texture mario;

    Texture water;
    Vector2 watermove;
    Vector2 watersize;
     Stage stage;
     TextureAtlas buttonAtlas;
        TextButtonStyle buttonStyle;
        TextButton button;
        Skin skin;


    @Override
    public void create() {
        BitmapFont font = new BitmapFont();
        skin = new Skin();
        buttonAtlas = new TextureAtlas("buttons/pause.pack");
        skin.addRegions(buttonAtlas);

        buttonStyle = new TextButtonStyle();
        buttonStyle.up = skin.getDrawable("imgres");
        buttonStyle.over = skin.getDrawable("imgres");
        buttonStyle.down = skin.getDrawable("imgres");


        button = new TextButton("", buttonStyle);


        stage.addActor(button);


        mysteryCloud = new Texture(Gdx.files.internal("data/mysteryCloud.jpg"));
        batch = new SpriteBatch();
        mario = new Texture(Gdx.files.internal("data/mario.jpg"));
        background = new Texture(Gdx.files.internal("data/background1.jpg"));
        background2 =  new Texture(Gdx.files.internal("data/background2.jpg"));
        background3 =  new Texture(Gdx.files.internal("data/background3.jpg"));
        position = new Vector2();
        pos = new Vector2();
        backgroundmove = new Vector2();
        backgroundmove2 = new Vector2();
        size2 = new Vector2(mario.getWidth() ,mario.getHeight() );
        //size2.y = trash.getHeight();
        //size2.x = trash.getWidth();
        size = new Vector2(mysteryCloud.getWidth() ,mysteryCloud.getHeight());
        bounds= new Rectangle(pos.x, pos.y, size.x, size.y);
        bounds2= new Rectangle(position.x, position.y, 32, 32);

            }

    @Override
    public void dispose() {
    //font.dispose();

    }
    public void update(){

        bounds.set(pos.x, pos.y, 32, 32);
        bounds2.set(position.x, position.y, size2.x, size2.y);
        position.x = position.x + 5* Gdx.input.getAccelerometerY();


        //float pos1=Gdx.input.getAccelerometerY();

        //position.x = position.x - 5*pos1;

        //float pos2=Gdx.input.getAccelerometerY();

        //position.y = position.y - 5*pos2;
    }
    @Override
    public void render() {  

        beginning = beginning+ 1;
        if(beginning == 1){
            backgroundmove.x = Gdx.graphics.getWidth();
        }
        position.y = position.y -10;
        if(position.x<0){
            Gdx.input.vibrate(250);
        }
        if(position.x>Gdx.graphics.getWidth()){
            Gdx.input.vibrate(250);
        }   
        //if(position.y<0){
            //Gdx.input.vibrate(250);
        //}
        if(position.y>Gdx.graphics.getHeight()){
            Gdx.input.vibrate(250);
        }   
        if(bounds.overlaps(bounds2)){
            collision=true; 
            }else{
                collision= false;
            }

        if(position.x<0){
            position.x= position.x+80;
        }
        if(position.x>Gdx.graphics.getWidth() - mario.getWidth()){
            position.x= position.x-80;
        }

        if(Gdx.input.isTouched()){
            position.y = position.y + 30;
        }

        if(position.y<0){
            position.y= position.y+80;
        }
        if(position.y>Gdx.graphics.getWidth() +mario.getHeight()){
            position.y= position.y-80;
        }
        pos.x = pos.x - 10;
        if(pos.x <0 && collision == false){
            pos.x = Gdx.graphics.getWidth();
            Random randomGenerator = new Random();
            pos.y = randomGenerator.nextInt(Gdx.graphics.getHeight()-30);
        }

        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        if(Gdx.input.getAccelerometerX() == 10){

        }
        if(collision){
            pos.x = Gdx.graphics.getWidth();
            Random randomGenerator = new Random();
            pos.y = randomGenerator.nextInt(Gdx.graphics.getHeight()-30);
        }
        backgroundmove.x = backgroundmove.x - 10;
        backgroundmove2.x = backgroundmove2.x - 10;
        if(backgroundmove.x <-Gdx.graphics.getWidth()){
            backgroundmove.x = Gdx.graphics.getWidth();
        }
        if(backgroundmove2.x <-Gdx.graphics.getWidth()){
            backgroundmove2.x = Gdx.graphics.getWidth();
        }
        update();
        batch.begin();



//      if(!collision){
//          batch.draw(mysteryCloud, pos.x, pos.y, 64, 64);
//      }           

//      batch.draw(mysteryCloud, pos.x, pos.y, 64, 64);
//              if(collision){
//                  mysteryCloud.dispose(); 
//              }
//              if(pos.x<0 && collision == true){
//                  batch.draw(mysteryCloud, pos.x, pos.y, 64, 64);
//              }
//          

        //batch.draw(background3, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());         
        //batch.draw(background, backgroundmove.x, backgroundmove.y, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());    
        //batch.draw(background2, backgroundmove2.x, backgroundmove2.y, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 

        batch.draw(mario, position.x,position.y);
        batch.draw(mysteryCloud, pos.x, pos.y, 64, 64);
        font.setScale(4);
        font.setColor(0.0f, 1.0f, 1.0f,1.0f);
        stage.draw();  
        batch.end();

    }


    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }
}

我意识到当我评论stage.draw()以及我的应用程序工作的其他按钮代码时,但当我取消注释代码时,它说mylibgdxgame已停止工作。

按钮代码如下:

TextureAtlas buttonAtlas;
TextButtonStyle buttonStyle;
TextButton button;
Skin skin;


@Override
public void create() {
    BitmapFont font = new BitmapFont();
    skin = new Skin();
    buttonAtlas = new TextureAtlas("buttons/pause.pack");
    skin.addRegions(buttonAtlas);

    buttonStyle = new TextButtonStyle();
    buttonStyle.up = skin.getDrawable("imgres");
    buttonStyle.over = skin.getDrawable("imgres");
    buttonStyle.down = skin.getDrawable("imgres");


    button = new TextButton("", buttonStyle);


    stage.addActor(button);

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: No Drawable, NinePatch, TextureRegion, Texture, or Sprite registered with name: imgres at com.badlogic.gdx.scenes.scene2d.ui.Skin.getDrawable(Skin.java:291) at com.me.rarsterinco.rarsterinco.create(rarsterinco.java:64) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:‌​136) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114‌​)
Exception in thread "LWJGL Application" java.lang.IllegalArgumentException: Missing LabelStyle font. at com.badlogic.gdx.scenes.scene2d.ui.Label.setStyle(Label.java:77) at com.badlogic.gdx.scenes.scene2d.ui.Label.<init>(Label.java:71) at com.badlogic.gdx.scenes.scene2d.ui.TextButton.<init>(TextButton.java:48) at com.me.rarsterinco.rarsterinco.create(rarsterinco.java:69) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:‌​136) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114‌​)

0 个答案:

没有答案