1
    package net.gfx;

public class TileSet {
    public final int TILES = 627;

    class Tiles {
        int x = 0, y = 0;
        int w = 0, h = 0;
    }

    public Tiles tiles[] = new Tiles[TILES];

    public TileSet() {
        for (int i = 0, y = 0; i < TILES; i++) {
            for (int x = 0; x < 1280; x =+ 25) {
                if (x > 1280) {
                    x = 0;
                    y += 40;
                }

                tiles[i] = new Tiles(); //Program Freezes here
                tiles[i].x = x;
                tiles[i].y = y;
                tiles[i].w = 40;
                tiles[i].h = 40;
            }
        }
    }
}

What I'm trying to do is basically create an array of tiles on a screen. I've fixed the original error i was getting here Error Setting Object Array Values but now it freezes up when i run it. More details in code.

6
  • You don't have a default constructor in the tile class. Commented Sep 5, 2013 at 23:58
  • @progenhard Yes he does. Commented Sep 5, 2013 at 23:59
  • "Freezes"? You sure it's not throwing an exception, maybe ArrayIndexOutOfBoundsException? Commented Sep 5, 2013 at 23:59
  • 3
    You're setting x equal to positive 25 in the loop. Possibly you meant to increment it. Commented Sep 5, 2013 at 23:59
  • @chrylis He will never go out of bounds because he's infinite on the inner loop. He'll be setting the 0th index of the array every time because i never increments. Commented Sep 6, 2013 at 0:02

2 Answers 2

4

=+ is not a java operator. You are setting x to the value 25 every time in the x loop. So you will never get into the case where x > 1280 because every time through the loop (except the first) x will be 25. You should use the += operator if you wish to increment x by 25 each time through the loop.

Sign up to request clarification or add additional context in comments.

2 Comments

You are amazing for finding this i love you! :D
You should also know that even with the += operator you will never hit that inner condition in your loop where you are testing for x > 1280.
1

The last clause of the for is the problem. it is x =+ 25. If we wiggle the whitespace a bit we get that it is the same as x = +25. Yes, each time, x is set to positive 25. You got the = and + the wrong way around. If you replace it with x += 25, your problem should be fixed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.