glTranslatef not working?

Started by ChaoticCactus, April 14, 2013, 15:27:21

Previous topic - Next topic

ChaoticCactus

Ok, I tried all three of those methods that you described and none of them worked, so then I added e.printStackTrace() to my try catch loops and now when the level is loading this pops up in the console:

java.lang.ArrayIndexOutOfBoundsException: 60
	at com.akkami.sidescroller.BlockGrid.<init>(BlockGrid.java:82)
	at com.akkami.sidescroller.Component.<init>(Component.java:55)
	at com.akkami.sidescroller.Component.main(Component.java:198)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
java.lang.ArrayIndexOutOfBoundsException: -1
	at com.akkami.sidescroller.BlockGrid.<init>(BlockGrid.java:39)
	at com.akkami.sidescroller.Component.<init>(Component.java:55)
	at com.akkami.sidescroller.Component.main(Component.java:198)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)


Except there's more of them.  It definitely has to do with the fact that my screen is only 640 x 480 and the world is 60 x 60 (each block is 16 x 16 pixels).  So I am almost entirely sure that it has something to do with the world size, but I am not sure how that correlates to not allowing glTranslatef to work.

Thank you for the consistent help by the way! 

quew8

Exception means exceptional circumstances. There is no way we can guess what could be happening while exceptions are being thrown. Like I said before, one bug at a time, and this bug is telling you where it is.
I'm glad you can't see me because the smug look on my face... I was taking a complete shot in the dark saying there could be ArrayIndexOutOfBoundsExceptions being thrown. Anyway, I'll take a look at that section of the code soon.

quew8

I've looked through your code and I think I see the error.
For the second two nested for loops in the BlockGrid constructor (paste bin lines 117 and 186), you use y < worldW and x < worldH instead of x < worldW and y < worldH. Also you get the block above the previous one using [y - 1]. When y = 0 this gives -1 which is also an out of bounds index.

ChaoticCactus

Yeah, I imagine that I would have a smug grin on my face too  :).  But for the for loops (on lines 117 and 186) I have the y first so that the generation looks more random and ... natural (if that's the word for it).  And how would I check for the block above it without using [y - 1]? 

Oy, I really hope that this will fix the problem, because this is getting slightly annoying. 

quew8

It wasn't that you were iterating the y before the x, it was that your conditional statement in the y loop said "whilst y is less than width" Whereas it should be less than height.
Create a method like so:
public static int getIndexInRange(int index, int range) {
    return index % range;
    //MORE READABLE VERSION:
    //int i = index;
    //while(i <= range) {
    //    i -= range;
    //}
    //while(i > 0) {
    //    i += range;
    //}
    //return i;
}

Whenever you pass an index to your arrays, pass them through this method first.
Like:
    blocks[getIndexInRange(x, worldW)][getIndexInRange(y, worldH)] = new Block(...);