如何在box2d体的同时创建带有纹理的libgdx精灵?

时间:2017-12-22 14:32:43

标签: java opengl libgdx box2d scene2d

我一直在研究这个问题,并决定在这个问题上寻求帮助。

要点:
我有一个bullet类扩展Actor,并且有一个makeBullet方法,可以生成一个box2d正文和夹具。
在我的bullet类的draw方法中,我有一个 if / else 语句,用于检查我的shootButton是否被按下,如果是,则调用makeBullet方法,使用纹理创建一个新的Sprite,将位置设置为box2d项目符号,并将线性冲动应用于我的项目符号。

问题:
精灵的纹理仅在我按下shootButton并且仅在播放器旁边时出现,但box2d子弹似乎工作得很好。如何将图像附加到每个子弹?

很抱歉,如果我不是很清楚,我会非常乐意进一步详细解释或提供更多代码。

bullet上课:

package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.scenes.scene2d.Actor;

import static com.badlogic.gdx.physics.box2d.BodyDef.BodyType.DynamicBody;

public class bullet extends Actor{
    static BodyDef bodyDef;
    static Body body;
    static FixtureDef fixtureDef;
    static PolygonShape groundBox;
    Sprite bulletThing;

    //method that makes box2d bullet bodies, takes world and direction from main class, MyGdxGame
    public static void makeBullet(World world, int direction) {
        //makes body def
        bodyDef = new BodyDef();
        bodyDef.type = DynamicBody;
        // sets bullet at player (MyActor)'s location
        bodyDef.position.set(MyActor.body.getPosition().x, MyActor.body.getPosition().y);
        // put body in the world
        body = world.createBody(bodyDef);
        // makes fixture for bullet body
        fixtureDef = new FixtureDef();
        groundBox = new PolygonShape();
        fixtureDef.shape = groundBox;
        fixtureDef.density = 0.9f;
        fixtureDef.friction = 0.6f;
        fixtureDef.restitution = 0.1f; 
        groundBox.setAsBox(1f, 1f);
        // attach fixture to body
        body.createFixture(fixtureDef);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);

        if (MyGdxGame.shootButton.isPressed()) {  // if my shootButton is pressed
            //makes a bullet, takes the direction from main class
            bullet.makeBullet(MyGdxGame.world, MyGdxGame.direction);
            // makes texture for sprite
            Pixmap pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
            pixmap.setColor(Color.WHITE);
            pixmap.fill();
            bulletThing = new Sprite(new Texture(pixmap));
            // sets sprite texture to box2d bullet, supposedly
            bulletThing.setBounds(bullet.body.getPosition().x - bulletThing.getWidth()/2, bullet.body.getPosition().y - bulletThing.getHeight()/2, 4f, 4f);
            bulletThing.setRotation(MathUtils.radiansToDegrees * bullet.body.getAngle());
            bulletThing.setOriginCenter();
            bulletThing.draw(batch);
            //fires the bullet
            body.applyLinearImpulse(MyGdxGame.direction * 1f,0,body.getWorldCenter().x, body.getWorldCenter().y, true);
        }        
    }       
}
如果要求,可以使用

主班级。谢谢你的帮助。

0 个答案:

没有答案