所以我有这个简单的乒乓球比赛,但是我试图改变一下,事情是我现在正在努力不得不在比赛中复制或创造各种球以使其工作我想要的方式。我不确定是否应该使用arraylist,如果是,我应该怎么做?任何建议都会非常感激。
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() {
}
}
这是Ball类
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 :(得分:0)
在Java中,每当我想跟踪任意数量的对象时,我通常都会使用ArrayList;一旦你学会了如何有效地使用它们,它们就会非常方便。
以下是如何在您的update和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()