在哪里可以更改 LIBGDX 中对象的颜色

问题描述 投票:0回答:1

我刚刚了解 LIBGDX 库,需要将所有对象的颜色更改为不同深浅的紫色。我认为通过将相同类型的所有对象(例如钉、墙、捕手)放入数组列表中,然后迭代它们并在

render()
int
Play
类中设置颜色,但没有任何变化。

这是我最初的想法:

sb.setColor(Color.PINK);
for (RoundPeg peg : pegs) {
    peg.draw(sb);
}

我的相关代码:

公共游戏(GameStateManager gsm){ 超级(GSM);

    world = new World(new Vector2(0, gravity), true);
    world.setContactListener(new GameContactListener());
    b2dr = new Box2DDebugRenderer();
    int x= (int) (PPM/2); //feste x Pos evtl.
    placePegs();
    placeWalls();
    placeCatchers();
   // dropBall(MathUtils.random(100, 300));
    dropBall(250);//siehe x


    // Set up camera
    b2dcam = new OrthographicCamera();
    b2dcam.setToOrtho(false, Game.WIDTH / PPM, Game.HEIGHT / PPM);

}


private void placePegs() {
    int numRows = 9;
    pegs = new ArrayList<RoundPeg>();
    for (int row = numRows; row >= 1; row--) {
        for (int col = 1; col <= row; col++) {
            int x = (int) (Game.WIDTH * (col + (numRows - row) / 2.0) * 10 / PPM); // x-Position
            int y = (int) (Game.HEIGHT * (numRows - row + 1) * 8 / PPM); // y-Position
            RoundPeg peg = new RoundPeg(x, y, world, Color.PURPLE);
            pegs.add(peg);
        }
    }
    // 9 Peg rows
     /*for (int i = 10; i < 100; i = i + 10) {
        for (int j = 10; j < 100; j = j + 10) {
            RoundPeg peg = new RoundPeg((int) (Game.WIDTH * i / PPM), (int) ((Game.HEIGHT * j) / PPM), world);

        }
    }


    // 3 Peg rows
    for (int i = 25; i < 100; i = i + 25) {
        for (int j = 120; j < 700; j = j + 160) {
            RoundPeg peg = new RoundPeg((int) (Game.WIDTH * i / PPM), (int) j, world);
        }
    }

    // 2 Peg Rows wide
    for (int i = 15; i < 100; i = i + 70) {
        for (int j = 200; j < 700; j = j + 160) {
            RoundPeg peg = new RoundPeg((int) (Game.WIDTH * i / PPM), (int) j, world);
        }
    }

    // 2 Peg rows narrow
    for (int i = 35; i < 90; i = i + 30) {
        RoundPeg peg = new RoundPeg((int) (Game.WIDTH * i / PPM), 355, world);
    } */


}

private void placeWalls() {
    BodyDef bdef;
    FixtureDef fdef;
    PolygonShape shape;

    bdef = new BodyDef();
    fdef = new FixtureDef();
    shape = new PolygonShape();

    // Left Wall
    bdef.position.set(0 / PPM, 0 / PPM);
    bdef.type = BodyDef.BodyType.StaticBody;
    Body body = world.createBody(bdef);
    shape.setAsBox(5 / PPM, Game.HEIGHT / PPM);
    fdef.shape = shape;
    fdef.filter.categoryBits = Box2dVars.BIT_WALLS;
    fdef.filter.maskBits = Box2dVars.BIT_BALL;

    body.createFixture(fdef).setUserData("wall");


    // Right wall
    bdef.position.set(Game.WIDTH / PPM, 0 / PPM);
    bdef.type = BodyDef.BodyType.StaticBody;
    Body body1 = world.createBody(bdef);
    shape.setAsBox(5 / PPM, Game.HEIGHT / PPM);
    fdef.shape = shape;
    fdef.filter.categoryBits = Box2dVars.BIT_WALLS;
    fdef.filter.maskBits = Box2dVars.BIT_BALL;
    body1.createFixture(fdef).setUserData("wall");

    // Bottom wall
    bdef.position.set(0 / PPM, 0 / PPM);
    bdef.type = BodyDef.BodyType.StaticBody;
    Body body2 = world.createBody(bdef);
    shape.setAsBox(Game.WIDTH / PPM, 5 / PPM);
    fdef.shape = shape;
    fdef.filter.categoryBits = Box2dVars.BIT_WALLS;
    fdef.filter.maskBits = Box2dVars.BIT_BALL;
    body2.createFixture(fdef).setUserData("wall");
}

private void placeCatchers() {
    BodyDef bdef;
    FixtureDef fdef;
    PolygonShape shape;

    bdef = new BodyDef();
    fdef = new FixtureDef();
    shape = new PolygonShape();

    for (int i = 10; i < 100; i = i + 10) {
        bdef.position.set((Game.WIDTH * i / PPM) / PPM, 0);
        bdef.type = BodyDef.BodyType.StaticBody;
        Body body = world.createBody(bdef);
        shape.setAsBox(5 / PPM, 30 / PPM);
        fdef.shape = shape;
        fdef.filter.categoryBits = Box2dVars.BIT_WALLS;
        fdef.filter.maskBits = Box2dVars.BIT_BALL;
        body.createFixture(fdef).setUserData("wall");
        System.out.println("placing catcher");
    }
}

@Override
public void handleInput() {


}
//Bitmap
private void dropBall(int posX){

    BodyDef bdef = new BodyDef();
    FixtureDef fdef = new FixtureDef();

    bdef.position.set(posX / PPM, 790 / PPM);
    bdef.type = BodyDef.BodyType.DynamicBody;

    // Randomly drop ball
    Body body = world.createBody(bdef);
    CircleShape cshape = new CircleShape();
    cshape.setRadius(13 / PPM);
    fdef.shape = cshape;
    fdef.filter.categoryBits = Box2dVars.BIT_BALL;
    fdef.filter.maskBits = Box2dVars.BIT_WALLS | Box2dVars.BIT_PEG;
    fdef.restitution = 0.7f;
    fdef.density = 0.9f;
    fdef.friction = 0.1f;
    body.createFixture(fdef).setUserData("ball");
}

@Override
public void update(float dt) {
    world.step(dt, 6, 2);


}

@Override
public void render() {
    Gdx.gl20.glClearColor(1, 1, 1, 1);
    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
    b2dr.render(world, b2dcam.combined);
}

@Override
public void dispose() {
    world.dispose();
    b2dr.dispose();

}


public class Game implements ApplicationListener {

public static final String TITLE = "Plinko";
public static final int WIDTH = 480;
public static final int HEIGHT = 800;

public static final float STEP = 1 / 60f;
private float accum;

private SpriteBatch sb;
private OrthographicCamera cam;
private OrthographicCamera hudCam;

private GameStateManager gsm;

@Override
public void create() {
    sb = new SpriteBatch();
    cam = new OrthographicCamera();
    cam.setToOrtho(false, WIDTH, HEIGHT);
    hudCam = new OrthographicCamera();
    hudCam.setToOrtho(false, WIDTH, HEIGHT);

    gsm = new GameStateManager(this);
}

@Override
public void render() {
    accum += Gdx.graphics.getDeltaTime();
    while (accum >- STEP) {
        accum -= STEP;
        gsm.update(STEP);
        gsm.render();
    }
}

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

}


@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void dispose() {
    sb.dispose();
}

// Getters And Setters
public SpriteBatch getSpriteBatch() {
    return sb;
}
public OrthographicCamera getCamera() {
    return cam;
}

public OrthographicCamera getHUDCamera() {
    return hudCam;
}

RoundPeg
班:

public class RoundPeg {

private final  com.badlogic.gdx.graphics.Color color;
private BodyDef bdef;
private FixtureDef fdef;
private PolygonShape shape;
private CircleShape cshape;

public RoundPeg(int posX, int posY, World world, com.badlogic.gdx.graphics.Color color) {

   bdef = new BodyDef();

    bdef.position.set(posX / PPM, posY / PPM);
    fdef = new FixtureDef();
    bdef.type = BodyDef.BodyType.StaticBody;
    Body body = world.createBody(bdef);

    cshape = new CircleShape();
    cshape.setRadius(4 / PPM);
    fdef.shape = cshape;

    fdef.filter.categoryBits = Box2dVars.BIT_PEG;
    fdef.filter.maskBits = Box2dVars.BIT_BALL;
    body.createFixture(fdef).setUserData("peg");
    this.color= color;

}

}

java libgdx
1个回答
0
投票

您需要为单个精灵而不是精灵批次设置颜色。

© www.soinside.com 2019 - 2024. All rights reserved.