简体   繁体   English

为什么我必须在Resize()方法中初始化球? -Libgdx

[英]Why do i have to initialize the ball in Resize() method? - Libgdx

I'm making a simple Bouncing Ball game, even if i call the init() method via BouncingBall class constructor in Show() method of the BallScreen class, the ball isn't created. 我正在制作一个简单的Bouncing Ball游戏,即使我通过BallScreen类的Show()方法中的BouncingBall类构造函数调用init()方法,也不会创建球。 So to create a ball, i should call BouncingBall's init() method in Resize() method. 因此,要创建一个球,我应该在Resize()方法中调用BouncingBall的init()方法。 Why? 为什么? Isn't it supposed to be created in Show() method ? 它不是应该在Show()方法中创建的吗?

Here is the code for Bouncing Ball 这是弹跳球的代码

public class BouncingBall {

public static final float RADIUS_RATIO = 0.04f;
public static final float START_KICK = 500.0f;
private static final float KICK_INTERVAL = 3f;
private static final float DRAG = 1f;


private Vector2 position;
private Vector2 velocity;
float radius;
float lastKick;

public BouncingBall(Viewport viewport) {
    init(viewport);
}

public void init(Viewport viewport) {
    position = new Vector2();
    position.x = viewport.getWorldWidth() / 2;
    position.y = viewport.getWorldHeight() / 2;

    velocity = new Vector2(0,0);

    radius = RADIUS_RATIO * Math.min(viewport.getScreenWidth(), viewport.getScreenHeight());
    startKick();
}

public void update(float delta, Viewport viewport) {

    float elapsedSeconds = MathUtils.nanoToSec * (TimeUtils.nanoTime() - lastKick);

    if (elapsedSeconds > KICK_INTERVAL) {
        lastKick = TimeUtils.nanoTime();
        startKick();
    }

    velocity.x -= delta * DRAG * velocity.x;
    velocity.y -= delta * DRAG * velocity.y;

    position.x += velocity.x * delta;
    position.y += velocity.y * delta;
    collision(radius,viewport);
}


public void collision(float radius, Viewport viewport) {
    if (position.x + radius > viewport.getWorldWidth()) {
        position.x = viewport.getWorldWidth() - radius;
        velocity.x = -velocity.x;
    }

    if (position.x - radius < 0) {
        position.x = radius;
        velocity.x = -velocity.x;
    }

    if (position.y + radius > viewport.getScreenHeight()) {
        position.y = viewport.getWorldHeight() - radius;
        velocity.y = -velocity.y;
    }

    if (position.y - radius < 0) {
        position.y = radius;
        velocity.y = -velocity.y;
    }
}

public void startKick() {
    Random random = new Random();
    float angle = random.nextFloat() * MathUtils.PI2;
    velocity.x = START_KICK * MathUtils.cos(angle);
    velocity.y = START_KICK * MathUtils.sin(angle);
}

public void render(ShapeRenderer renderer) {
    renderer.setColor(Color.RED);
    renderer.circle(position.x, position.y, radius);
}
}

Here is the Screen Class for Bouncing Ball 这是弹跳球的屏幕类

public class BallScreen extends ScreenAdapter {


private static final float WORLD_SIZE = 480f;
private static final String TAG = BallScreen.class.getSimpleName();

private BouncingBall ball;
private Viewport viewport;
private ShapeRenderer renderer;


@Override
public void show() {
    Gdx.app.log(TAG, "Show");
    viewport = new FitViewport(WORLD_SIZE, WORLD_SIZE);
    renderer = new ShapeRenderer();
    ball = new BouncingBall(viewport);
}

@Override
public void resize(int width, int height) {
    Gdx.app.log(TAG, "resize" + width + " " + height);
    viewport.update(width,height,true);
    ball.init(viewport);
}

@Override
public void dispose() {
    Gdx.app.log(TAG, "dispose");
    renderer.dispose();
}

@Override
public void render(float delta) {
    viewport.apply();
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);

    renderer.setProjectionMatrix(viewport.getCamera().combined);
    renderer.begin(ShapeType.Filled);

    ball.update(delta, viewport);
    ball.render(renderer);

    renderer.end();

}
}

Problem is in this line : 问题在这一行:

radius = RADIUS_RATIO * Math.min(viewport.getScreenWidth(), viewport.getScreenHeight());

When you create FitViewPort and pass to BouncingBall , at that time viewport having only worldWidth and worldHeight that is what you set 480. At that time screenwidth and screenheight of viewport is zero so your radius initialise with zero value. 当您创建FitViewPort并传递给BouncingBall时,此时的视口仅具有您设置的480的worldWidth和worldHeight。那时,视口的screenwidth和screenheight为零,因此您的半径初始化为零值。

But After update() method if I call init() method : 但是在update()方法之后,如果我调用init()方法:

viewport.update(width,height,true);  // this method set value to screenwidth/height

when you call update method on viewport after that screenwidth and screenheight of viewport having some value according to what type of ViewPort you're using. 当您在视口的屏幕宽度和屏幕高度之后视视口调用更新方法时,根据您所使用的视口类型,视口的屏幕宽度和屏幕高度具有一定的值。

After update() if I call ball.init(viewport); update()如果我调用ball.init(viewport); then radius is not zero value so that ball is visible on screen. 则半径不是零值,以便在屏幕上可见球。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我必须初始化“ j”? - Why do I have to initialize “j”? 为什么我有 GridPane 调整大小问题? - Why do I have GridPane resize issue? (Java LibGDX)如何在LibGDX中调整纹理大小? - (Java LibGDX) How do I resize my textures in LibGDX? 为什么我观察到了球的动量,尽管我已经写了frame.repaint()从循环中调用paintComponent()方法 - why the movemet of ball is observerd although i have wriiten frame.repaint() to invoke paintComponent() method from the loop 为什么我必须在堆栈中将顶部初始化为 -1 两次? - Why do I have to initialize the top at -1 two times in stacks? 当我有特定的坐标来检测球与砖块之间的碰撞时,为什么球物体正在与不可见的墙壁碰撞? - Why is it that the ball object is colliding with invisible walls when i have specific coordinates to detect collision between the ball and the bricks? 为什么我在 LIbgdx 中创建物体时有时会出现 box2Dcrash - Why do I sometimes have a box2Dcrash when I create bodies in LIbgdx 如何在LibGDX中调整纹理大小?(Android) - How do I resize my textures in LibGDX?(Android) 为什么我使用Tiled在我的libgdx游戏中有线? - Why do I have lines going across my libgdx game using Tiled? 在libgdx的单元格内部对齐窗口小部件时,为什么必须调用expand? - Why do I have to call expand when aligning widget inside the cell in libgdx?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM