Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
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 685 times)
0 Members and 1 Guest are viewing this topic.
Offline _Scyth_

Junior Member





« Posted 2013-01-16 22: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 22: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 Member





« Reply #2 - Posted 2013-01-17 01: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 Member





« Reply #3 - Posted 2013-01-18 09: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: 114
Projects: 4
Exp: 3 years



« Reply #4 - Posted 2013-01-18 18: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 Member





« Reply #5 - Posted 2013-01-20 00: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.

xsi3rr4x (25 views)
2014-04-15 18:08:23

BurntPizza (20 views)
2014-04-15 03:46:01

UprightPath (36 views)
2014-04-14 17:39:50

UprightPath (18 views)
2014-04-14 17:35:47

Porlus (34 views)
2014-04-14 15:48:38

tom_mai78101 (60 views)
2014-04-10 04:04:31

BurntPizza (118 views)
2014-04-08 23:06:04

tom_mai78101 (218 views)
2014-04-05 13:34:39

trollwarrior1 (185 views)
2014-04-04 12:06:45

CJLetsGame (192 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!