Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Solved]ArrayOutOfBoundsException help.  (Read 872 times)
0 Members and 1 Guest are viewing this topic.
Offline _Scyth_

Junior Devvie





« Posted 2013-01-16 21:03:02 »

Before you say anything, I have googled this problem.

I'm getting an ArrayIndexOutOfBoundException every time my
1  
int pX;


gets to 14/15. I do not understand why, because technically the
1  
int[][] tileID = new int[15][20];


and the pX should refer to the 20. I do not know how to approach this problem, this is my code for moving my player, it includes a check to see if the player is on a tile it shouldn't be on, which i believe is where the problem is coming from.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
public void movePlayer(int pSX, int pSY)
   {
      // Set the previous positions of the x, y coordinates
      prevX = pX;
      prevY = pY;
     
      // Set The Player Speed
      pSpeedX = pSX;
      pSpeedY = pSY;
     
      // Move player then check if they are on a tile
      if( pSY == 0)
      {
            pX += pSpeedX;
            if( tiles.tileID[pY][pX] == 1)
            {
               pX = prevX;
               pY = prevY;
            }
      }
      if( pSX == 0)
      {
            pY += pSpeedY;
            if( tiles.tileID[pY][pX] == 1)
            {
               pX = prevX;
               pY= prevY;
            }
      }
   }


I know I'm posting a lot, but I'm really not sure how to do these things :/
Offline Pickleninja

JGO Coder


Medals: 10
Projects: 1


I'm tired of working for someone else.


« Reply #1 - Posted 2013-01-16 21:45:43 »

Will pSX only be equal to 1,0, or -1?

If not, that would explain why it's going out of bounds. Basically you need to ensure that pX and pY aren't greater than the length of your array-1 since arrays start with 0.



-Pickle:)

Offline Best Username Ever

Junior Devvie





« Reply #2 - Posted 2013-01-17 00:27:17 »

Such exceptions always have a line number associated with them. Put a breakpoint on that line and execute that program using a debugger. Look at your local variables and ask "What is the max index of the array? What value am I trying to use? How did the program get to this state?"
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline _Scyth_

Junior Devvie





« Reply #3 - Posted 2013-01-18 08:48:15 »

I got it sorted, don't know how but it works, and its been tested a lot so it should be good.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 139
Projects: 4
Exp: 3 years



« Reply #4 - Posted 2013-01-18 17:42:00 »

If you don't know how it works than it's not good, if you don't understand your code you'll have no way to debug it and you'll continually ask questions on forums like this...
Offline _Scyth_

Junior Devvie





« Reply #5 - Posted 2013-01-19 23:19:13 »

the players pX was the equal to the [20] part of tileID[15][20], so i added an extra column to it, so its tileID[15][21].

I understand it now, the pX wasn't equal to tileID[y].length - 1 it was equal to tileID[y].length.

i've got a little engine set up now, so it's speeding up my game, all i need to do is create the levels using matrices which means its speeding up the production of it. It's for blackberry smartphones so it doesn't have to be amazing Smiley just really good
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (36 views)
2014-12-15 09:26:44

Mr.CodeIt (29 views)
2014-12-14 19:50:38

BurntPizza (61 views)
2014-12-09 22:41:13

BurntPizza (98 views)
2014-12-08 04:46:31

JscottyBieshaar (58 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (76 views)
2014-11-29 21:32:03

toopeicgaming1999 (137 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (37 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!