我最近开始使用多边形,但是我很努力,因为我试图让Polygon移动到我的屏幕中心,虽然我不知道如何。 这是我的代码(我想在这里完成的是让球在屏幕的每一侧随机产生,并使它们朝着屏幕的中间碰撞,与正方形碰撞) :
public class Game extends com.badlogic.gdx.Game implements Screen{
private SpriteBatch batch;
private Texture Ball;
private Texture Up;
private OrthographicCamera cam;
private int score;
private String showScore;
private BitmapFont scoreFont;
private Sprite upSprite;
private Polygon square;
private Array<Polygon> balls1;
private Array<Polygon> balls2;
private Array<Polygon> balls3;
private Array<Polygon> balls4;
private Polygon ball1 = new Polygon();
private Polygon ball2 = new Polygon();
private Polygon ball3 = new Polygon();
private Polygon ball4 = new Polygon();
@Override
public void create(){
batch = new SpriteBatch();
Ball = new Texture("energyball.png");
Up = new Texture("up.png");
upSprite = new Sprite(Up);
upSprite.setOriginCenter();
upSprite.setX(615);
upSprite.setY(340);
upSprite.setRegionWidth(64);
upSprite.setRegionHeight(64);
square = new Polygon(new float[] {
upSprite.getX(), upSprite.getY(),
upSprite.getX(), upSprite.getY() + upSprite.getHeight(),
upSprite.getX() + upSprite.getWidth(), upSprite.getY() + upSprite.getY(),
upSprite.getX() + upSprite.getWidth(), upSprite.getY()
});
cam = new OrthographicCamera();
cam.setToOrtho(false, 1280, 720);
upSprite.setPosition(upSprite.getX(), upSprite.getY());
square.setOrigin(upSprite.getX(), upSprite.getY());
// calls the functions to spawn balls randomly
balls1 = new Array<Polygon>();
balls2 = new Array<Polygon>();
balls3 = new Array<Polygon>();
balls4 = new Array<Polygon>();
score();
//if the screen is touched sprite rotates 90 degrees clockwise
Gdx.input.setInputProcessor(new InputAdapter() {
@Override
public boolean touchDown(int x, int y, int pointer, int button) {
return true;
}
@Override
public boolean touchUp(int x, int y, int pointer, int button) {
upSprite.rotate(-90);
square.rotate(-90f);
return true;
}
});
}
//shows score
private void score() {
score = -4;
showScore = "Score: 0";
scoreFont = new BitmapFont();
}
//creates the balls and sets their position as well as the random timer for each
private void spawnBalls1() {
ball1.setPosition(MathUtils.random(639, 641), 720);
ball1.setScale(32, 32);
balls1.add(ball1);
lastDropTime = TimeUtils.nanoTime();
}
private void spawnBalls2() {
ball2.setPosition(0, MathUtils.random(359, 361));
ball2.setScale(32, 32);
balls2.add(ball2);
lastDropTime = TimeUtils.nanoTime();
}
private void spawnBalls3() {
ball3.setPosition(MathUtils.random(639, 641), 0);
ball3.setScale(32, 32);
balls3.add(ball3);
lastDropTime = TimeUtils.nanoTime();
}
private void spawnBalls4() {
ball4.setPosition(1280, MathUtils.random(359, 361));
ball4.setScale(32, 32);
balls4.add(ball4);
lastDropTime = TimeUtils.nanoTime();
}
@Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.update();
//draws the game itself as well as the balls on the screen
batch.setProjectionMatrix(cam.combined);
batch.begin();
upSprite.draw(batch);
//draws the balls
for (Polygon ball1 : balls1) {
batch.draw(Ball, ball1.getX(), ball1.getY());
}
for (Polygon ball2 : balls2) {
batch.draw(Ball, ball2.getX(), ball2.getY());
}
for (Polygon ball3 : balls3) {
batch.draw(Ball, ball3.getX(), ball3.getY());
}
for (Polygon ball4 : balls4) {
batch.draw(Ball, ball4.getX(), ball4.getY());
}
scoreFont.setColor(1, 1, 1, 1);
scoreFont.draw(batch, showScore, 25, 100);
batch.end();
// if the time minus the time of the last ball spawn is less than x then spawn another ball in a random place
if (TimeUtils.nanoTime() - lastDropTime > 1000000000) {
switch (MathUtils.random(4)) {
case 0:
spawnBalls1();
break;
case 1:
spawnBalls2();
break;
case 2:
spawnBalls3();
break;
case 3:
spawnBalls4();
break;
}
}
Iterator<Polygon> iter1 = balls1.iterator();
while(iter1.hasNext()) {
Polygon balls1 = iter1.next();
balls1.getY() -= 350 * Gdx.graphics.getDeltaTime();
if (Intersector.overlapConvexPolygons(balls1, square)) {
score++;
showScore = "Score: " + score;
iter1.remove();
}
}
Iterator<Polygon> iter2 = balls2.iterator();
while (iter2.hasNext()) {
Polygon balls2 = iter2.next();
balls2.x += 550 * Gdx.graphics.getDeltaTime();
if (Intersector.overlapConvexPolygons(balls2, square)) {
score++;
showScore = "Score: " + score;
iter2.remove();
}
}
Iterator<Polygon> iter3 = balls3.iterator();
while(iter3.hasNext()) {
Polygon balls3 = iter3.next();
balls3.y += 350 * Gdx.graphics.getDeltaTime();
if (Intersector.overlapConvexPolygons(balls3, square)) {
score++;
showScore = "Score: " + score;
iter3.remove();
}
}
Iterator<Polygon> iter4 = balls4.iterator();
while(iter4.hasNext()) {
Polygon balls4 = iter4.next();
balls4.x -= 550 * Gdx.graphics.getDeltaTime();
if (Intersector.overlapConvexPolygons(balls4, square)) {
score++;
showScore = "Score: " + score;
iter4.remove();
}
}
}
我在这里收到错误:
balls1.getY() -= 350 * Gdx.graphics.getDeltaTime();
balls2.getX() += 550 * Gdx.graphics.getDeltaTime();
balls3.getY() += 350 * Gdx.graphics.getDeltaTime();
balls4.getX() -= 550 * Gdx.graphics.getDeltaTime();
任何帮助都会很高兴。