java—如何在重置所有值的情况下跳转,对于新值,而不是旧的libgdx

9avjhtql  于 2021-07-11  发布在  Java
关注(0)|答案(0)|浏览(192)

这是代码,我想要一个新的圆,但旧的圆应该是跳跃计数,而一个新的,刚刚创建的开始从初始位置,就像水果忍者,那里有很多水果来跳。当我用旧方法为每个圆构造新函数时

import com.badlogic.gdx.ApplicationAdapter;
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
    import com.badlogic.gdx.utils.Array;

    import java.util.Random;

    public class MyGdxGame extends ApplicationAdapter {
        private ShapeRenderer renderer;

        private Array<Obstacle> obstacles = new Array<Obstacle>();
        private float obstacleTimer;

        public float delta;

        private float timeSeconds = 0;
        public float velocity = 0;
        private int gravity = 2;
        private float randomNumber = 0;
        private float y = 0;

        @Override
        public void create() {
            renderer = new ShapeRenderer();
        }

        @Override
        public void render() {

            delta = Gdx.graphics.getRawDeltaTime();

            // update world
            update(delta);

            // render debug graphics
            renderDebug();

        }

        private void update(float delta) {

            updateObstacles(delta);

        }

        private void updateObstacles(float delta) {

            for (Obstacle obstacle : obstacles) {
                obstacle.update();

            }

            createNewObstacle(delta);

        }

        private void createNewObstacle(float delta) {
            obstacleTimer += delta;

            Random Random = new Random();
            randomNumber = 30 + Random.nextInt(40);

            timeSeconds += Gdx.graphics.getRawDeltaTime();

            if (timeSeconds > 2) {

                //timeseconds - period

                timeSeconds = 0;

                //velocity assigned the negative random number 1

                velocity = -randomNumber;

            }

            //if condition is true

            if (y > 0 || velocity < 0) {

                velocity = velocity + gravity;

                //birdy - velocity

                y -= velocity;

            }

            float obstacleX = Gdx.graphics.getWidth() / 4;
            float obstacleY = (float) (Gdx.graphics.getHeight() / 4) + y;

            Obstacle obstacle = new Obstacle();
            obstacle.setPosition(obstacleX, obstacleY);

            obstacles.add(obstacle);

        }

        private void renderDebug() {

            renderer.begin(ShapeRenderer.ShapeType.Line);

            drawDebug();

            renderer.end();
        }

        private void drawDebug() {

            for (Obstacle obstacle : obstacles) {
                obstacle.drawDebug(renderer);
            }
        }

        @Override
        public void dispose() {

        }
    }

这是另一个名为障碍物.java的类

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Circle;

public class Obstacle {
    private static final float SIZE = 32;

    public float x = Gdx.graphics.getWidth() / 4;
    public float y = 0;

    private Circle bounds;

    public Obstacle() {
        bounds = new Circle(x, y, SIZE);
    }

    public void drawDebug(ShapeRenderer renderer) {
        renderer.circle(bounds.x, bounds.y, bounds.radius, 30);
    }

    public void setPosition(float x, float y) {

        this.x = x;
        this.y = y;
        updateBounds();
    }

    public void update() {

        //getting value of score on screen

        setPosition(x, y);
    }

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }

    private void updateBounds() {
        bounds.setPosition(x, y);
    }

    public float getWidth() {
        return SIZE;
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题