简体   繁体   中英

LibGDX Setting sprite to a circle shape?

I am trying to make a ball image bind to a circle shape in LibGDX.

Currently I have this code for in the render method

    spriteBatch.begin();
    backWorld.getBodies(backBodies);
    for(Body body : backBodies){
        if(body.getUserData() instanceof Sprite){
            Sprite sprite = (Sprite) body.getUserData();
            sprite.setPosition(body.getPosition().x, body.getPosition().y);
            sprite.setSize(1000, 1200);
            sprite.draw(spriteBatch);
        }
    }

    world.getBodies(tmpBodies);
    for(Body bodyBall : tmpBodies){
        if(bodyBall.getUserData() instanceof Sprite){
            Sprite ballsprite = (Sprite) bodyBall.getUserData();
            ballsprite.setPosition(bodyBall.getPosition().x, bodyBall.getPosition().y);
            ballsprite.draw(spriteBatch);
        }
    }
    spriteBatch.end();

The first loop is getting an image and setting it to a world in the background and that works fine the image is shown. But when I included the second loop which should do the same, which is looping through the body and getting the sprites then applying it to the body it gives a nullpointerexeption.

Using this code to send the sprites to the bodies.

ball.setUserData(ballSprite);
back.setUserData(backgroundSprite);

也许您超出了SpriteBatch的最大大小?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM