1

私はおそらく単純なものを見逃していますが、何がわからないのでしょう。Pong を練習して再作成し、それがどのようになるかを確認しているところです。

私はこれを持っています:

package com.gibbo.pong;

enter code here
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.World;

public class GameScreen implements Screen {

    // Object instances
    Pong pong;
    Paddle paddle;
    Walls wall;
    Ball ball;
    OrthographicCamera cam;
    public static World world;
    Box2DDebugRenderer debug;

    // Booleans for paddles
    private boolean playerOne = false;
    private boolean playerTwo = false;

    // Booleans for walls
    private boolean wallTop = false;
    private boolean wallBottom = false;

    // Fields for the balls
    private boolean ballExists = false;
    private int ballTotal;

    //Array to hold ball objects
    Ball[] ballArray = new Ball[3];

    // GameScreen default constructor
    public GameScreen(Pong pong) {
        this.pong = pong;

        // Initiate the camera
        cam = new OrthographicCamera(20f, 14f);
        cam.position.set(10f, 7f, 0);

        // Initiate the world, no gravity
        world = new World(new Vector2(0, -10), true);

        // Initiate the renderer
        debug = new Box2DDebugRenderer();

        // Initiate the paddle
        paddle = new Paddle();

        //Initiate the walls
        wall = new Walls();

        //Initiate the ball
        ball = new Ball();



    }

    public void render(float delta) {
        // Clear the screen and fill it black
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        // Update the camera
        cam.update();

        // Render the debug lines
        debug.render(world, cam.combined);

        // Check if paddles exist, if not create them
        if (!playerOne) {
            paddle.createPaddle(1, 7);
            playerOne = true;
            System.out.println("Creating player ones paddle");
        }
        if (!playerTwo) {
            paddle.createPaddle(19, 7);
            playerTwo = true;
            System.out.println("Creating player twos paddle");
        }

        //Check if walls exist, if not create them
        if(!wallBottom){
            wall.createWalls(0, 0.1f);
            wallBottom = true;
            System.out.println("Creating top wall");
        }
        if(!wallTop){
            wall.createWalls(0, 13f);
            wallTop = true;
            System.out.println("Creating bottom wall");
        }

        //Checks if ball exists
        if(!ballExists){
            ball.createBall(10f, 7f);
            ballExists = true;
            if(ballTotal == 0){
            System.out.println("Creating ball number 1");
            ballTotal += 1;
            }else{
                System.out.println("Creating ball number "+ballTotal + 1);
            }
        }
        if(ball.ballBody.getPosition().x < 0){
            System.out.println("Player twos point");
            ball.destroyBall();
        }

        boolean test = true;
        if(test){
        ball.ballBody.setLinearVelocity(-10, 0);
        System.out.println("Does this work?");
        }



        // Simulate the world with frame rate
        // Keep at bottom of render when possible
        world.step(1 / 60, 6, 2);
    }

ボール用、壁用、パドル用の 3 つのクラスを使用しています。主に、それらを連携させたり、引数を渡したりする方法を練習することです。ここに書かれていることはすべて、LibGDX サイトのチュートリアルと C++ box2d マニュアルの組み合わせです。

これは私のボールクラスです:

package com.gibbo.pong;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;

public class Ball {

    //Instance of the world
    World world;

    //Body for the ball
    Body ballBody;

    //Fixture for the ball
    Fixture ballFixture;

    public void createBall(float posX, float posY){
        //Define a body for the ball
        BodyDef ballBodyDef = new BodyDef();
        ballBodyDef.type = BodyType.DynamicBody;
        ballBodyDef.position.set(posX, posY);

        //Define a shape for the ball
        CircleShape ballShape = new CircleShape();
        ballShape.setRadius(0.50f);

        //Define a fixture for the ball
        FixtureDef ballFixtureDef = new FixtureDef();
        ballFixtureDef.shape = ballShape;
        ballFixtureDef.density = 1;

        //Create a ball
        ballBody = GameScreen.world.createBody(ballBodyDef);
        ballFixture = ballBody.createFixture(ballFixtureDef);

        ballShape.dispose();

        ballBody.setLinearVelocity(-10, 0.1f);


    }

かなり簡単で、クラス内で簡単に実装できますがGameScreen、練習と整頓のために、これを使用しました. ここで LinearVelocity を設定しようとしましたが、ボールの GameScreen で、ボールがパドルに当たった場合に何が起こるか、どのように反応するか、画面外でボールを破壊する方法が機能するかどうかをシミュレートしようとしました。

しかし、何らかの理由で、すべてが「立ち往生」しています。あたかもすべてが静的であるかのように。オブジェクト配列でボールを作成しようとしたこともありましballs[0].setxxxxたが、それはすべておかしなことでした。

ボールや他のクラスのコンストラクターを作成する必要がありますか? パドル、壁、ボールが継続的にレンダリングされ、同じ位置に留まるように強制されていますか?

print メソッドを含むランダムな if ステートメントに関しては、そのメソッドに到達するかどうかをテストするだけだったので、それを使用して強制的に実行されました。

ここで私の失敗はありますか?

前もって感謝します。

4

1 に答える 1

1

私を撃ってください、整数除算がひどいことがわかりました。

私は変更しなければなりませんでした:

world.step(1/60, 6, 2)

に:

world.step(1f/60f, 6, 2)

うわー、それは面倒でしたが、とても簡単です(笑)。

于 2013-09-10T23:12:10.337 に答える