android - 如何通过单击 libgdx 上的按钮打开另一个类? (android开发eclipse)

标签 android eclipse class libgdx

我正在制作自己的游戏。我已经制作了启动画面和主菜单。我在我的菜单中制作了一个按钮“播放”,但我无法将它链接到我的主游戏,但我可以将它链接到其他类(class)。

这是我无法打开的类的代码:

package com.mygdx.Papermadness.screens;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureWrap;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;

public class PaperMadness extends InputAdapter implements ApplicationListener, Screen     {



float timer;
    public static Preferences prefs;  
    public static int counter;    // The variable you want to save

    private BitmapFont font;
    //public int counter = 0;
    boolean touch = false;

    SpriteBatch batch;

    SpriteBatch spriteBatch;
    Texture spriteTexture;
    Sprite sprite;

    float scrollTimer = 0.0f;

    Player player;
    Paper paper;

    Huiz huiz;

    Lijn lijn;

    String money = String.valueOf(counter);

    ShapeRenderer sr;

    public boolean kukar = false;


    public void create() {

        font = new BitmapFont();
        Gdx.input.setInputProcessor(this);

        player = new Player(new Vector2(50, 100), new Vector2(100, 100));
        huiz = new Huiz(new Vector2(200, 300), new Vector2(110, 110));
        // huiz = new Huiz(new Vector2(200, 300), new Vector2(110, 110));
        paper = new Paper(new Vector2(Gdx.input.getX(),
                Gdx.graphics.getHeight() - Gdx.input.getY()), new       Vector2(50,
                50));
        lijn = new Lijn(new Vector2(0, 200), new Vector2(600, 2));
        // sr = new ShapeRenderer();

        spriteBatch = new SpriteBatch();

        spriteTexture = new Texture("b9.png");
        spriteTexture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
        sprite = new Sprite(spriteTexture);
        sprite.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        batch = new SpriteBatch();

    }
    @Override
    public void render(float delta) {
        // bounds.set(Gdx.input.getX(),
                // Gdx.graphics.getHeight() - Gdx.input.getY(),
                // secondTexture.getWidth(), secondTexture.getHeight());

                scrollTimer += Gdx.graphics.getDeltaTime();
                if (scrollTimer > 1.0f)
                    scrollTimer = 0.0f;

                sprite.setV(scrollTimer + 2);
                sprite.setV2(scrollTimer);

                player.update();
                paper.update();
                lijn.update();
                huiz.update();

                /*
                 * if (tree.getBounds().overlaps(ball.getBounds())) {
                 * System.out.println("Swendley Programinateur"); }
                 * 
                 * if (tree.getBounds().overlaps(paper.getBounds())) {
                 * System.out.println("Souk Programinateur"); }
                 */

                spriteBatch.begin();
                sprite.draw(spriteBatch);
                spriteBatch.end();

                batch.begin();
                player.draw(batch);
                huiz.draw(batch);
                // paper.draw(batch);
                if (Gdx.graphics.getHeight() / 1.25 < Gdx.input.getY()
                        && Gdx.graphics.getWidth() / 2.7 <   Gdx.input.getX()
                        && Gdx.graphics.getWidth() / 1.7 > Gdx.input.getX()
                        && Gdx.input.isTouched() && kukar == false && touch == false) {
                    kukar = true;
                    touch = true;
                } else if (Gdx.input.isTouched() && kukar == true && touch == true) {
                    paper.draw(batch);

                    if (paper.getBounds().overlaps(huiz.getBounds())
                            || paper.getBounds().overlaps(huiz.getBounds1())) {
                        // System.out.println("Huis Geraakt!");
                        touch = false;
                        counter++;
                        checkSpeed();
                        money = Integer.toString(counter);

                        // System.out.println(counter);
                    }
                }

                if (huiz.getBounds().overlaps(lijn.getBounds())
                        || huiz.getBounds1().overlaps(lijn.getBounds())){
                    //System.out.println("Game Over");
                }

                    font.draw(batch, money, Gdx.graphics.getWidth() / 2.06f,
                            Gdx.graphics.getHeight() / 1.05f);
                font.setColor(Color.BLACK);
                font.setScale(2, 2);
                // house.draw(batch);
                // house1.draw(batch);
                lijn.draw(batch);

                batch.end();

                // sr.begin(ShapeType.Filled);
                // sr.setColor(Color.YELLOW);
                // sr.rect(Gdx.input.getX(), Gdx.graphics.getHeight() -
                // Gdx.input.getY(),
                // paper.getSize().x, paper.getSize().y);
                // sr.setColor(Color.BLACK);
                // sr.rect(huiz.getPosition().x, huiz.getPosition().y,
                // huiz.getSize().x, huiz.getSize().y);
                // sr.rect(house1.getPosition().x, house1.getPosition().y,
                // house1.getSize().x, house1.getSize().y);
                // sr.end();

            }
            public static void savePrefs(){
                prefs = Gdx.app.getPreferences("game-prefs");  // The name of your prefs files
                prefs.putInteger("counter", counter);
                prefs.flush();
                System.out.println(prefs);
            }

            public static void loadPrefs(){

                prefs = Gdx.app.getPreferences("game-prefs");
                counter = prefs.getInteger("counter",0);  //Load counter, default to zero if not found

            }
            public void checkSpeed() {
                if (counter <= 7) {
                    huiz.huisVelocity = 500f;
                }
                if (counter > 7 && counter <= 17) {
                    huiz.huisVelocity = 550f;
                }
                if (counter > 17 && counter <= 30) {
                    huiz.huisVelocity = 650f;
                }
                if (counter > 30 && counter <= 50) {
                    huiz.huisVelocity = 750;
                }
                if (counter > 50 && counter <= 75) {
                    huiz.huisVelocity = 900;
                }
                if (counter > 75 && counter <= 100) {
                    huiz.huisVelocity = 1000;
                }

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



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

    }

    @Override
    public void show() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {

    }
    @Override
    public void render() {

    }
}

这是菜单类:

package com.mygdx.Papermadness.screens;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.mygdx.Papermadness.Papermadness;

public class MainMenu implements Screen {

    private Stage stage;// done
    private TextureAtlas atlas;// done
    private Skin skin;// done
    private Table table;// done
    private TextButton buttonPlay, buttonExit;
    private BitmapFont white, black;// done
    private Label heading;

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

        Table.drawDebug(stage);

        stage.act(delta);
        stage.draw();

    }

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


    }

    @Override
    public void show() {
        stage = new Stage();

        Gdx.input.setInputProcessor(stage);

        atlas = new TextureAtlas("ui/button.pack");
        skin = new Skin(atlas);

        table = new Table(skin);
        table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        white = new BitmapFont(Gdx.files.internal("font/white.fnt"), false);
        black = new BitmapFont(Gdx.files.internal("font/black.fnt"), false);

        // maakt buttons
        TextButtonStyle textButtonStyle = new TextButtonStyle();
        textButtonStyle.up = skin.getDrawable("button.up");
        textButtonStyle.down = skin.getDrawable("button.down");
        textButtonStyle.pressedOffsetX = 1;
        textButtonStyle.pressedOffsetY = -1;
        textButtonStyle.font = black;

        buttonExit = new TextButton("EXIT", textButtonStyle);
        buttonExit.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                Gdx.app.exit();
            }
        });

        buttonExit.pad(15);

        buttonPlay = new TextButton("PlAY", textButtonStyle);
        buttonPlay.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                ((Game) Gdx.app.getApplicationListener())
                        .setScreen(new PaperMadness());
            }

        });
        buttonPlay.pad(15);

        // maakt header
        heading = new Label(Papermadness.TITLE, new LabelStyle(white,
                Color.WHITE));
        heading.setFontScale(2);

        table.add(heading);
        table.getCell(heading).spaceBottom(100);
        table.row();
        table.add(buttonPlay);
        table.getCell(buttonPlay).spaceBottom(15);
        table.row();
        table.add(buttonExit);
        // table.debug();
        stage.addActor(table);

    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {
        stage.dispose();
        atlas.dispose();
        skin.dispose();
        white.dispose();
        black.dispose();

    }
}

最佳答案

    public class MyGdxGame extends Game{
        public static MenuScreen menuScreen;
        public static GameScreen gameScreen;

      @Override    
      public void create(){
        menuScreen = new MenuScreen(this);
        gameScreen = new GameScreen (this);
        setScreen(menuScreen);
    }
}

这是菜单界面

public class MenuScreen implements Screen{
   MyGdxGame game;

   public MenuScreen(MyGdxGame game){
      this.game = game;
   }
   ////////////when you want to change screen type game.setScreen(game.gameScreen)
   ...........
   ...........
}

这是GameScreen

public class GameScreen implements Screen{
   ...........
   ...........
   ...........
}

这是一个简单的例子,尝试在你的代码中做同样的事情。
这是一个很好的教程 https://github.com/libgdx/libgdx/wiki/Extending-the-simple-game 如果有帮助,请告诉我。

关于android - 如何通过单击 libgdx 上的按钮打开另一个类? (android开发eclipse),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24452623/

相关文章:

android - 如何清除 Android 中的 Activity 堆栈

java - 存在布局问题

eclipse - BIRT:pdf 发射器,从相对路径或 jar 文件加载/使用字体

c++ - 检查 C++11 枚举类的总顺序(如何实现 operator<)

ruby - Ruby如何区分Method和Method=(parameter)(同名getter/setter方法)?

Android Studio 拒绝接受已安装 Google Repository

java - 优化 XY 绘图数据

java - Eclipse 中的 ObservableList<Node> 方法引用错误

eclipse - golang 与其他语言的 Goclipse 工作区

python - Python中的静态类