2014-09-18 4 views
0

그래서이 간단한 퐁 게임이 있지만 조금 바뀌려고합니다. 현재 게임에서 여러 가지 공을 복제하거나 만드는 데 어려움을 겪고 있습니다. 내가 원하는 방식. 나는 arraylist를 사용해야하는지 잘 모르겠다. 그렇다면 어떻게해야합니까? 어떤 제안이라도 정말 고맙겠습니다.Libgdx - 여러 개의 공을 만듭니다.

public class ColorPong implements ApplicationListener { 

private Rectangle field = new Rectangle(); 
private Ball ball = new Ball(); 
private float fieldTop, fieldBottom, fieldLeft, fieldRight; 





@Override 
public void create() { 
    field.set(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 
    fieldLeft = field.x; 
    fieldRight = field.x + field.width; 
    fieldBottom = field.y; 
    fieldTop = field.y + field.height; 
    ball.BallCreation(); 
    reset(); 


} 

@Override 
public void resize(int width, int height) { 

} 

@Override 
public void render() { 
    float dt = Gdx.graphics.getRawDeltaTime(); 
    update(dt); 
    draw(dt); 

} 

private void draw(float dt) { 
    Gdx.gl.glClearColor(0, 0, 0, 1); 
    Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT); 

    ball.DrawingBall(dt); 

} 

private void update(float dt) { 
    updateBall(dt); 
} 

private void updateBall(float dt) { 
    ball.Intergrate(dt); 
    ball.updateBounds(); 
    //------------------ 
    if(ball.Left() < fieldLeft){ 
     ball.move(fieldLeft, ball.getY()); 
     ball.Reflect(true, false); 
    } 

    if(ball.Right() > fieldRight){ 
     ball.move(fieldRight - ball.getWidth(), ball.getY()); 
     ball.Reflect(true, false); 
    } 

    if(ball.Bottom() < fieldBottom){ 
     ball.move(ball.getX(), fieldBottom); 
     ball.Reflect(false, true); 
    } 

    if(ball.Top() > fieldTop){ 
     ball.move(ball.getX(), fieldTop - ball.getHeight()); 
     ball.Reflect(false, true); 
    } 

} 

public void reset(){ 
    ball.move(field.x + (field.width - ball.getWidth())/2, (field.y + field.height)/2); 
    Vector2 velocity = ball.getVelocity(); 
    velocity.set(300, 150); 
    velocity.setAngle(360f - 45f); 
    ball.setVelocity(velocity); 



} 

@Override 
public void pause() { 

} 

@Override 
public void resume() { 

} 

@Override 
public void dispose() { 

} 

} 여기

와 내가 일반적으로 ArrayList를을 사용하여 객체의 임의의 수를 추적 할 때마다 볼 클래스

private ShapeRenderer ballRenderer; 
private Color ballColor = new Color(); 


public Ball() { 
    super(32, 32); 

} 

public void Reflect(boolean x, boolean y){ 
Vector2 velocity = getVelocity(); 
if(x) velocity.x *= -1; 
if(y) velocity.y *= -1; 
setVelocity(velocity); 
} 

public void BallCreation(){ 
    ballRenderer = new ShapeRenderer(); 
} 

public void DrawingBall(float dt){ 
    ballRenderer.begin(ShapeType.Filled); 
    drawBall(dt); 
    ballColorSwap(); 
    ballRenderer.end(); 

} 

int THRESHOLD = 900; // 4 seconds 
long lastChanged = 0; // timestamp 
public void ballColorSwap(){ 
    // maybe call it here? 
    if(System.currentTimeMillis() - lastChanged < THRESHOLD) 
     return; 
    int rnd = (int)(Math.random() * 4); 
    switch(rnd){ 
    case 0: ballColor.set(Color.GREEN);break; 
    case 1: ballColor.set(Color.BLUE);break; 
    case 2: ballColor.set(Color.RED);break; 
    case 3: ballColor.set(Color.YELLOW);break; 
    } 
    lastChanged = System.currentTimeMillis(); 
} 

private void drawBall(float dt) { 
    ballRenderer.circle(this.getX(), this.getY(), 20); 
    ballRenderer.setColor(ballColor); 

} 

} 자바에서

+0

새로운 볼을 원할 때 언제든지 메인 Pong 클래스에서 볼의 새로운 인스턴스를 만들어야합니다. 볼을 유지하기 위해서는 볼의 ArrayList가 필요합니다. 당신의 Pong 클래스의 공. 그래서 당신이 새로운 공을 만들 때 그것을 배열 목록에 넣습니다. 그러면 각 공을 그리기 위해 배열 목록을 반복해서 그려보십시오. – Barodapride

+0

볼 수는 있지만 다른 볼은 원래 볼과 같이 동일한 작업을 수행 할 수 있습니까? 아니면 각 볼에 대한 업데이트를 만들어야합니까? – ovniMode

+0

예. 지금하고있는 것처럼 updateBall()을 수행하는 대신, 볼 목록을 반복하고 각각에 updateBall을 호출해야합니다. 그러나 당신이 그것을 설정 한 방법은 정확히 작동하지 않을 것입니다. 볼 클래스에서 update()를 수행하면 각 볼을 쉽게 반복하고 .update()를 호출하면됩니다. render() 메서드에서 비슷한 작업을 수행 할 수 있습니다. 따라서 ball 클래스로 updateBall()을 옮겨야합니다. – Barodapride

답변

0

입니다 ; 그것들을 효과적으로 사용하는 법을 배우게되면 아주 편리합니다. 여기

당신이 당신의 갱신 및 updateBall 방법과 ArrayList에를 사용하는 방법의 예 : 공을 그릴

//initialize in create() 
ArrayList<Ball> balls; 


private void update(float dt) { 
    //Pretty much saying 'For every ball in Balls, assign it to 'b' and do something with it 
    for(Ball b : balls) { 
     updateBall(b, dt); 
    } 
} 
private void updateBall(Ball b, float dt) { 
    b.Intergrate(dt); 
    b.updateBounds(); 
    //------------------ 
    if(b.Left() < fieldLeft){ 
     b.move(fieldLeft, ball.getY()); 
     b.Reflect(true, false); 
    } 

    if(b.Right() > fieldRight){ 
     b.move(fieldRight - b.getWidth(), b.getY()); 
     b.Reflect(true, false); 
    } 

    if(b.Bottom() < fieldBottom){ 
     b.move(b.getX(), fieldBottom); 
     b.Reflect(false, true); 
    } 

    if(b.Top() > fieldTop){ 
     b.move(b.getX(), fieldTop - b.getHeight()); 
     b.Reflect(false, true); 
    } 

} 

물론, 단지 (다시 볼 .... 일 및 통화에 대한 작업을 수행 b.DrawingBall()

+0

이 예제에서는 복제해야 할 볼의 양을 어떻게 결정합니까? – ovniMode

+0

updateBall()에서이 볼을 복제해야하는지 확인하기 위해 체크를 추가합니다. 그렇다면 새 볼을 만들고 복제 할 위치를 설정 한 다음 ArrayList에 추가합니다. 예 : '볼 newball = 새 볼(); '이 'newball.setX이 (oldball.x)'가 'newball.setY이 (oldball.y),' 는'// 어쩌면 그것을 임의의 방향을 제시 ' 'balls.add (newball);' – Rock48

관련 문제