如何处理空指针异常的libGDX的Box2D的ContactListener? [英] How to handle Null Pointer Exception in libGDX's Box2D ContactListener?

查看:238
本文介绍了如何处理空指针异常的libGDX的Box2D的ContactListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是怎么回事大家,

我试探性地通过生产在libGDX一个Box2D的克隆傍。空指针异常是原产于中,我想实现的分数逻辑ContactListener的beginContact()方法。

在世界的Box2D使用两个独立的EdgeShapes作为传感器与球(查看附加的图像)在碰撞递增得分变量。球和其他任何的Box2D的世界之间的球和两个EdgeShapes作品,但碰撞的碰撞逻辑导致程序崩溃。

堆栈跟踪:

 异常螺纹LWJGL应用程序显示java.lang.NullPointerException
在com.ckq3r.Pong.screens.GameScreen $ 2.beginContact(GameScreen.java:491)
在com.badlogic.gdx.physics.box2d.World.beginContact(World.java:876)
在com.badlogic.gdx.physics.box2d.World.jniStep(本机方法)
在com.badlogic.gdx.physics.box2d.World.step(World.java:602)
在com.ckq3r.Pong.screens.GameScreen.render(GameScreen.java:99)
在com.badlogic.gdx.Game.render(Game.java:46)
在com.ckq3r.Pong.PongGame.render(PongGame.java:236)
在com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:204)
在com.badlogic.gdx.backends.lwjgl.LwjglApplication $ 1.run(LwjglApplication.java:112)

有问题的code:

  / ** Box2D的接触监听器** /
私人无效createContactListener(){
    world.setContactListener(新ContactListener(){        @覆盖
        公共无效beginContact(联系方式联系我们){
            夹具fixtureA = contact.getFixtureA();
            夹具fixtureB = contact.getFixtureB();            Gdx.app.log(beginContact+ fixtureA.toString之间时()+和+ fixtureB.toString());            。如果(fixtureA.getBody()getUserData()等于(1)及;&放大器;。。fixtureB.getBody()getUserData()等于(2)|| fixtureA.getBody()getUserData()等于(2) &放大器;&放大器; fixtureB.getBody()getUserData()等于(1)){
                Gdx.app.log(HIT,1个目标的接触);
                score1 ++;
                score1String = score1 +;
            }            。如果(fixtureA.getBody()getUserData()等于(1)及;&放大器;。。fixtureB.getBody()getUserData()等于(3)|| fixtureA.getBody()getUserData()等于(3) &放大器;&放大器; fixtureB.getBody()getUserData()等于(1)){
                Gdx.app.log(HIT,目标2接触);
                score2 ++;
                score2String = score2 +;
            }
        }        @覆盖
        公共无效endContact(联系方式联系我们){
            夹具fixtureA = contact.getFixtureA();
            夹具fixtureB = contact.getFixtureB();
            Gdx.app.log(endContact+ fixtureA.toString之间时()+和+ fixtureB.toString());
        }        @覆盖
        公共无效preSOLVE(联系方式联系我们,集成块oldManifold){
        }        @覆盖
        公共无效postSolve(联系方式联系我们,ContactImpulse冲动){
        }        });
}

注:


  1. 当我beginContact()方法中注释掉两个条件语句中,code运行。取消注释时,错误被复制。

  2. 的用户数据球是circleBody.setUserData(1);

  3. 的第1个目标是用户数据goalBody.setUserData(2);

  4. 的目标2用户数据是goalBody.setUserData(3);


解决方案

设置每个身体用户数据的Box2D的世界,让您可以使用ContactListener内的条件逻辑来评估碰撞是否应该进行处理。

通过重点是就行了 paddleBody.setUserData(4); 下面。你可以选择任何数量的设置对单个对象的用户数据,只要该数是从世界中的其他对象的用户数据是唯一的。

例如:

  / ** ** paddle1 /
    BodyDef paddleDef =新BodyDef();
    paddleDef.type = BodyType.DynamicBody;
    paddleDef.position.set(宽* 0.2F,高度/ 2);
    paddleDef.fixedRotation = TRUE;    车身paddleBody = world.createBody(paddleDef);
    paddleBody.setUserData(4);    PolygonShape paddleShape =新PolygonShape();
    paddleShape.setAsBox(3.0F,15.0f); //半角和半高    FixtureDef paddleFD =新FixtureDef();
    paddleFD.shape = paddleShape;
    paddleFD.de​​nsity = 10.0f;
    paddleFD.friction = 0.0;
    paddleFD.restitution = 0.1F;    paddleBody.createFixture(paddleFD);    paddleShape.dispose();    paddleBody.setLinearVelocity(0.0,0.0);
    / **结束paddle1 ** /    / ** ** paddle2 /
    BodyDef paddleDef2 =新BodyDef();
    paddleDef2.type = BodyType.DynamicBody;
    paddleDef2.position.set(宽* 0.8f,高度/ 2);
    paddleDef2.fixedRotation = TRUE;    车身paddleBody2 = world.createBody(paddleDef2);
    paddleBody2.setUserData(5);    PolygonShape paddleShape2 =新PolygonShape();
    paddleShape2.setAsBox(3.0F,15.0f); //半角和半高    FixtureDef paddleFD2 =新FixtureDef();
    paddleFD2.shape = paddleShape2;
    paddleFD2.density = 10.0f;
    paddleFD2.friction = 0.0;
    paddleFD2.restitution = 0.1F;    paddleBody2.createFixture(paddleFD2);    paddleShape2.dispose();    paddleBody2.setLinearVelocity(0.0,0.0); //以每秒X = 0.0,Y = 0.0米的速度不通移动
    / **结束paddle2 ** /

What's up everyone,

I am heuristically producing a Pong clone in Box2D via libGDX. The Null Pointer Exception is originating in the ContactListener's beginContact() method in which I am trying to implement score logic.

The Box2D world uses two separate EdgeShapes as sensors for incrementing a score variable upon collision with the Ball (view attached image). The collision logic between the Ball and the two EdgeShapes works, but collision between the Ball and anything else in the Box2D world crashes the program.

The stack trace:

Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.ckq3r.Pong.screens.GameScreen$2.beginContact(GameScreen.java:491)
at com.badlogic.gdx.physics.box2d.World.beginContact(World.java:876)
at com.badlogic.gdx.physics.box2d.World.jniStep(Native Method)
at com.badlogic.gdx.physics.box2d.World.step(World.java:602)
at com.ckq3r.Pong.screens.GameScreen.render(GameScreen.java:99)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.ckq3r.Pong.PongGame.render(PongGame.java:236)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:204)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:112)

The problematic code:

    /**Box2D contact listener**/
private void createContactListener() {
    world.setContactListener(new ContactListener() {

        @Override
        public void beginContact(Contact contact) {
            Fixture fixtureA = contact.getFixtureA();
            Fixture fixtureB = contact.getFixtureB();

            Gdx.app.log("beginContact", "between " + fixtureA.toString() + " and " + fixtureB.toString());

            if(fixtureA.getBody().getUserData().equals(1) && fixtureB.getBody().getUserData().equals(2) || fixtureA.getBody().getUserData().equals(2) && fixtureB.getBody().getUserData().equals(1)){
                Gdx.app.log("HIT", "goal1 contact");
                score1++;
                score1String = score1 + "";
            }

            if(fixtureA.getBody().getUserData().equals(1) && fixtureB.getBody().getUserData().equals(3) || fixtureA.getBody().getUserData().equals(3) && fixtureB.getBody().getUserData().equals(1)){
                Gdx.app.log("HIT", "goal2 contact");
                score2++;
                score2String = score2 + "";
            }
        }

        @Override
        public void endContact(Contact contact) {
            Fixture fixtureA = contact.getFixtureA();
            Fixture fixtureB = contact.getFixtureB();
            Gdx.app.log("endContact", "between " + fixtureA.toString() + " and " + fixtureB.toString());
        }

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

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

        });
} 

Notes:

  1. When I comment out the two conditional statements within the beginContact() method, the code runs. When uncommented, the error is reproduced.
  2. The Ball userData is circleBody.setUserData(1);
  3. The goal1 userData is goalBody.setUserData(2);
  4. The goal2 userData is goalBody.setUserData(3);

解决方案

Set the userdata for each body in the Box2D world so that you can use conditional logic within the ContactListener to assess whether or not the collision should be handled.

With the emphasis being on the line paddleBody.setUserData(4); below. You can pick any number to set to the userdata of the individual object, so long as the number is unique from the userdata of the other objects within your world.

For example:

    /**paddle1**/
    BodyDef paddleDef = new BodyDef();
    paddleDef.type = BodyType.DynamicBody;
    paddleDef.position.set(width * 0.2f, height / 2);
    paddleDef.fixedRotation = true;

    Body paddleBody = world.createBody(paddleDef);
    paddleBody.setUserData(4);

    PolygonShape paddleShape = new PolygonShape();
    paddleShape.setAsBox(3.0f, 15.0f); //half-width and half-height

    FixtureDef paddleFD = new FixtureDef();
    paddleFD.shape = paddleShape;
    paddleFD.density = 10.0f;
    paddleFD.friction = 0.0f;
    paddleFD.restitution = 0.1f;

    paddleBody.createFixture(paddleFD);

    paddleShape.dispose();

    paddleBody.setLinearVelocity(0.0f, 0.0f);
    /**end paddle1**/

    /**paddle2**/
    BodyDef paddleDef2 = new BodyDef();
    paddleDef2.type = BodyType.DynamicBody;
    paddleDef2.position.set(width * 0.8f, height / 2);
    paddleDef2.fixedRotation = true;        

    Body paddleBody2 = world.createBody(paddleDef2);
    paddleBody2.setUserData(5);             

    PolygonShape paddleShape2 = new PolygonShape();
    paddleShape2.setAsBox(3.0f, 15.0f); //half-width and half-height

    FixtureDef paddleFD2 = new FixtureDef();
    paddleFD2.shape = paddleShape2;
    paddleFD2.density = 10.0f;
    paddleFD2.friction = 0.0f;
    paddleFD2.restitution = 0.1f;

    paddleBody2.createFixture(paddleFD2);

    paddleShape2.dispose();

    paddleBody2.setLinearVelocity(0.0f, 0.0f); // Move nowhere at a rate of x = 0.0f, y = 0.0f meters per second
    /**end paddle2**/

这篇关于如何处理空指针异常的libGDX的Box2D的ContactListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆