2017-04-06 7 views
0

私はBallScreenクラスのShow()メソッドでBouncingBallクラスのコンストラクタを使ってinit()メソッドを呼び出しても、ボールが作成されない単純なバウンスボールゲームを作っています。ボールを作成するには、Resize()メソッドでBouncingBallのinit()メソッドを呼び出す必要があります。どうして?それはShow()メソッドで作成されるはずですか?Resize()メソッドでボールを初期化する必要があるのはなぜですか? ###Libgdx

ここでここでバウンドするボールのためのコード

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); 
} 
} 

は、Screenクラスがバウンドするボールのためです

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(); 

} 
} 
+0

ボールが作成されていないか、画面に表示されていませんか? – Aryan

+0

@AbhishekAryanはい、ビューポートには表示されません。 –

答えて

0

問題は、この行である:

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

あなたがFitViewPortを作成BouncingBallに渡すと、その時点ではwoしかないビューポートあなたが設定したrldWidthとworldHeightは480です。その時、ビューポートのスクリーン幅とスクリーン高さはゼロですので、半径は0で初期化されます。

しかし、私は init()メソッドを呼び出す場合 update()方法:

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

あなたがそのscreenwidth、あなたが使用しているビューポートのどのタイプに応じていくつかの値を持つビューポートのscreenheight後、ビューポートでupdateメソッドを呼び出すとき。

update()の後にball.init(viewport);と呼んだ場合、ボールは画面上に表示されるように半径はゼロではありません。

+0

ありがとう!私はscreenWidthとworldWidthの違いを逃したと思う... –

関連する問題