Android Studio LibGDX游戏没有显示障碍物和楼层

时间:2016-02-27 04:34:32

标签: java android android-studio libgdx box2d

我试图制作Avoider游戏,这是95%完成,然而,当我尝试添加无限障碍时,游戏从头开始看起来像这样(没有错误):How the game looks < / p>

我不知道我做错了什么,有人可以帮帮我吗?这将是真正的,非常感激,因为这是我需要做的最后一件事,让我的游戏运作。我的GameScreen中的代码:

    package com.circlecrashavoider;

    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.graphics.GL20;
    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.math.MathUtils;
    import com.badlogic.gdx.math.Vector2;
    import com.badlogic.gdx.physics.box2d.Contact;
    import com.badlogic.gdx.physics.box2d.ContactImpulse;
    import com.badlogic.gdx.physics.box2d.ContactListener;
    import com.badlogic.gdx.physics.box2d.Manifold;
    import com.badlogic.gdx.physics.box2d.World;
    import com.badlogic.gdx.scenes.scene2d.Stage;
    import com.badlogic.gdx.utils.viewport.FitViewport;
    import com.circlecrashavoider.entities.FloorEntity;
    import com.circlecrashavoider.entities.ObstacleEntity;
    import com.circlecrashavoider.entities.ObstacleEntity2;
    import com.circlecrashavoider.entities.PlayerEntity;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Objects;
    import java.util.Random;

    public class GameScreen extends BaseScreen {

        private Stage stage;
        private World world;
        private PlayerEntity player;
        private List<FloorEntity> floorList = new ArrayList<FloorEntity>();
        private List<ObstacleEntity> obstacleList = new ArrayList<ObstacleEntity>();
        private List<ObstacleEntity2> obstacle2List = new ArrayList<ObstacleEntity2>();

        public GameScreen(MainGame game) {
            super(game);
            stage = new Stage(new FitViewport(1024, 620));
            world = new World(new Vector2(0, -10), true);


            world.setContactListener(new ContactListener() {

                private boolean areCollided(Contact contact, Object userA, Object userB) {
                    return (contact.getFixtureA().getUserData().equals(userA) && contact.getFixtureB().getUserData().equals(userB)) ||
                            (contact.getFixtureA().getUserData().equals(userB) && contact.getFixtureB().getUserData().equals(userA));
                }

                @Override
                public void beginContact(Contact contact) {
                    if (areCollided(contact, "player", "floor")) {
                        player.setJumping(false);
                        if (Gdx.input.isTouched()) {
                            player.setMustJump(true);
                        }
                    }

                    if (areCollided(contact, "player", "obstacle")) {
                        player.setAlive(false);
                        System.out.println("GAME OVER");

                    }

                    if (areCollided(contact, "player", "obstacle2")) {
                        player.setAlive(false);
                        System.out.println("GAME OVER");
                    }
                }

                @Override
                public void endContact(Contact contact) {

                }

                @Override
                public void preSolve(Contact contact, Manifold oldManifold) {

                }

                @Override
                public void postSolve(Contact contact, ContactImpulse impulse) {

                }
            });
        }

        @Override
        public void show() {
        }

        private float spawnTime = 4f;
        private float timer = 0;

        public void update(float deltaTime) {
            timer += deltaTime;


            if (timer >= spawnTime) {
                this.spawnEntity();

                spawnTime = MathUtils.random(2f, 4f);
                timer = 0;
            }
        }


        private void spawnEntity(){

            Texture floorTexture = game.getManager().get("floor.png");
            Texture overfloorTexture = game.getManager().get("overfloor.png");
            Texture overfloor2Texture = game.getManager().get("overfloor2.png");
            Texture obstacleTexture = game.getManager().get("obstacle.png");
            Texture obstacle2Texture = game.getManager().get("obstacle2.png");
            //Spawn your object
            floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture, 0, 1000, 1));
            floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture ,8, 10 ,5));
            floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture ,10, 10 ,8));
            floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture ,34 , 3 ,5));
            floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture ,19 , 8 ,4));
            floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture ,24 , 8 ,1.5f));
            floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture ,27 , 5 , 2));
            obstacleList.add(new ObstacleEntity(world, obstacleTexture, 6, 1));

            stage.addActor(player);
            for (FloorEntity floor: floorList) {
                stage.addActor(floor);
            }
            for (ObstacleEntity obstacle : obstacleList) {
                stage.addActor(obstacle);
            }
    }

        @Override
        public void render(float delta) {
            Gdx.gl20.glClearColor(0.5f, 0.6f, 1, 3f);
            Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);



            stage.act();
            world.step(delta, 6, 2);
            stage.draw();
        }



        @Override
        public void dispose() {
            stage.dispose();
            world.dispose();
        }
    }

我的MainGame类中的代码:

    package com.circlecrashavoider;

    import com.badlogic.gdx.Game;
    import com.badlogic.gdx.assets.AssetManager;
    import com.badlogic.gdx.graphics.Texture;
    import com.circlecrashavoider.scene2d.Box2DScreen;
    import com.circlecrashavoider.scene2d.Scene2DScreen;

    public class MainGame extends Game {

        private AssetManager manager;

        public AssetManager getManager() {
            return manager;
        }

        @Override
        public void create() {
            manager = new AssetManager();
            manager.load("floor.png", Texture.class);
            manager.load("overfloor.png", Texture.class);
            manager.load("overfloor2.png", Texture.class);
            manager.load("obstacle.png", Texture.class);
            manager.load("obstacle2.png", Texture.class);
            manager.load("crash.png", Texture.class);
            manager.load("player.png", Texture.class);
            manager.finishLoading();

            setScreen(new GameScreen(this));
        }
    }

1 个答案:

答案 0 :(得分:0)

PlayerEntity,FloorEntity和ObstacleEntity类的代码在这篇文章https://stackoverflow.com/questions/35678654/issues-getting-android-studio-game-to-run中。在此链接上,您将找到您需要的代码Fuat Coskun