1

如何取消绑定更新向量到精灵?我正在使用 Libgdx 框架。这是我的代码

public class VectorSample extends GDX_TEST implements InputProcessor {

    Texture ball,bullet,guider;
    OrthographicCamera camera;
    Vector2 vector = new Vector2();
    Vector2 vectoralt = new Vector2();

    long lastDropTime;
    SpriteBatch batch;
    Rectangle rect = new Rectangle();
    float angle = 0,anglealt = 0;
    private ShapeRenderer renderer;
    TextureRegion tr;
    Sprite sprite,sprite2;


    ShapeRenderer sr;
    Ship ship;
    BallShit shit;
    Vector2 nvec;

    protected Vector2 center = new Vector2();
    Array<Vector2> bullets;
    Array<Rectangle> bulletsRect;
    Boolean fire = false;
    Rectangle tmpRect = new Rectangle();




    @Override
    public void create() {
        renderer = new ShapeRenderer();
        ball = new Texture(Gdx.files.internal("data/player.png"));

        bullet = new Texture("data/ball_black.png");

        tr = new TextureRegion(ball);

        camera = new OrthographicCamera();
        camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        ship = new Ship(new Vector2(5, 50), 1, 1, 0, 5f);
        sr = new ShapeRenderer();

        batch = new SpriteBatch();
        Gdx.input.setInputProcessor(this);

        rect.width = ball.getWidth();
        rect.height = ball.getHeight();
        vector.add(200,200);


        sprite = new Sprite(ball);

        vectoralt.add(200 + sprite.getWidth(),200);


        shit = new BallShit(new Vector2(10,0),50f,50f);
        bullets = new Array<Vector2>();
        getCenter();


        bulletsRect = new Array<Rectangle>();

        fireatwill2();


    }

    @Override
    public void render() {



        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        camera.update();
        batch.setProjectionMatrix(camera.combined);


        batch.begin();
        {

            batch.draw(sprite,vector.x,vector.y,sprite.getWidth()/2,sprite.getHeight()/2,sprite.getWidth(),sprite.getHeight(),1,1,angle);
            if(fire == true){


                for(Rectangle raindrop: bulletsRect) {

                    batch.draw(bullet,raindrop.x,raindrop.y);
                }

            }

        }

        batch.end();

        if(Gdx.input.isKeyPressed(Input.Keys.D)){
            vector.add(1,0);
            if(vector.x > Gdx.graphics.getWidth()){
                Gdx.app.log("x","x");
            }
        }
        if(Gdx.input.isKeyPressed(Input.Keys.A)){
            vector.add(-1,0);
            if(vector.x < 0){
                Gdx.app.log("x","-x");
            }
        }

        float w = camera.frustum.planePoints[1].x - camera.frustum.planePoints[0].x;
        float distance =   w - (vector.x + sprite.getWidth());
        if(distance <=0){
            vector.x = w - sprite.getWidth();
        }
        Gdx.app.log("camera x" , " " + distance);

        if(distance >= Gdx.graphics.getWidth() - sprite.getWidth()) {
            vector.x = 0;
        }



        if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){


               fire = true;

            if(TimeUtils.nanoTime() - lastDropTime > 100000000) fireatwill2();

        }


        Iterator<Rectangle> iter = bulletsRect.iterator();

            while(iter.hasNext()) {
                Rectangle raindrop = iter.next();

                double angletry = getAngle() * MathUtils.degreesToRadians;
                float speed = 5;
                float scale_x = (float)Math.cos(angletry);
                float scale_y = (float)Math.sin(angletry);
                float velocity_x = (speed* scale_x);
                float velocity_y = (speed* scale_y);
                raindrop.x += velocity_x;
                raindrop.y += velocity_y;
                if(raindrop.y  < 0) iter.remove();
                if(raindrop.y  > Gdx.graphics.getHeight() || raindrop.x  > Gdx.graphics.getWidth()) iter.remove();


            }



        //getting the angle
            float angle = findAngle(Gdx.input.getX(),
                    Gdx.graphics.getHeight() - Gdx.input.getY());

            this.angle = angle % 360;


    }

    private float getAngle(){
        float angle = findAngle(Gdx.input.getX(),
                Gdx.graphics.getHeight() - Gdx.input.getY());


       return angle;
    }

    private void fireatwill2() {

        Rectangle raindrop = new Rectangle();


        raindrop.x =   vector.x + sprite.getWidth() / 2 - (bullet.getWidth() / 2);
        raindrop.y =    vector.y + sprite.getHeight() / 2 - (bullet.getHeight() / 2);



        bulletsRect.add(raindrop);
        lastDropTime = TimeUtils.nanoTime();

    }



    public Vector2 getCenter() {
        center.x = vector.x + sprite.getWidth() / 2;
        center.y = vector.y + sprite.getHeight() / 2;
        return center.cpy();
    }





    @Override
    public void pause() {
        super.pause();    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public void resume() {
        super.resume();    //To change body of overridden methods use File | Settings | File Templates.
    }

    public float findAngle(float x1, float y1) {
        Vector2 center = getCenter();
        float x0 = center.x;
        float y0 = center.y;

        float a = MathUtils.atan2(y1 - y0, x1 - x0);

        return a * MathUtils.radiansToDegrees;
    }

}

这是正在运行的演示,源代码在这里

我不能很好地解释它,但如果你能运行演示,你就会明白我在说什么。

我真的被困在这里..谢谢。

4

0 回答 0