-3
\$\begingroup\$

I recently started using the Polygons but I'm have struggle as I'm trying to make a Polygon move towards the center of my screen, although I don't know how. Here's the code I have(What I'm trying to accomplish here is to make the balls spawn randomly by every side of the screen and make them go towards the middle of the screen to collide against a square):

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

    }

}

And I'm getting an error here:

balls1.y -= 350 * Gdx.graphics.getDeltaTime();

balls2.x += 550 * Gdx.graphics.getDeltaTime();

balls3.y += 350 * Gdx.graphics.getDeltaTime();

balls4.x -= 550 * Gdx.graphics.getDeltaTime();

Basically move this ball to the square

Any help would be gladly appreciated.

\$\endgroup\$
  • \$\begingroup\$ Could you post the error message? \$\endgroup\$ – Eric Jul 12 '16 at 2:28
  • \$\begingroup\$ error: y has private access in Polygon for every single one of them respectively \$\endgroup\$ – gafflx Jul 12 '16 at 2:37
  • \$\begingroup\$ That suggests you have a problem with your scope. Have you added the public/protected modifier on your instance variables? ( public Array<Polygon> balls1; and public Polygon ball1 = new Polygon(); ) \$\endgroup\$ – Eric Jul 12 '16 at 2:43
  • \$\begingroup\$ i just did and keeps giving me the same error \$\endgroup\$ – gafflx Jul 12 '16 at 2:47
  • 2
    \$\begingroup\$ Please edit to reflect the actual problem you're trying to solve. This seems to have nothing to do with "movement toward a point" and far more to do with "I can't get this thing to compile". \$\endgroup\$ – Anko Jul 21 '16 at 14:11
2
\$\begingroup\$

error: y has private access in Polygon

This simply means, that the y field is private in the Polygon class. Instead of trying to use balls1.y to reach that variable, use setPosition(float x, float y), getX() and getY()

\$\endgroup\$
  • \$\begingroup\$ If I use any of those I don't think it will make the ball move towards the square, but now after changing it I get this error: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 \$\endgroup\$ – gafflx Jul 12 '16 at 13:54
  • \$\begingroup\$ @gafflx If you don't know what that means, then you should learn java \$\endgroup\$ – Bálint Jul 12 '16 at 14:28
  • \$\begingroup\$ I know what it means, and I think what is causing that is the fact that I did this:balls1.setPosition(ball1.getX(), ball1.getY()), then did balls1.getY() -= 550 * Gdx.graphics.getDeltaTime(); \$\endgroup\$ – gafflx Jul 12 '16 at 14:35
  • \$\begingroup\$ but I cant use balls1.getY() with -= 550 * Gdx.graphics.getDeltaTime(); \$\endgroup\$ – gafflx Jul 12 '16 at 14:36
  • \$\begingroup\$ @gafflx You need to use ball1.setPositon(ball1.getX(), ball1.getY() - 550 * Gdx.graphics.getDeltaTime()) \$\endgroup\$ – Bálint Jul 12 '16 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.