![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
1 class, 1 problem = 1 big headache
So i'm working on this big project in java (tetris) and i came across this error which could effect my entire program. Anyone who has a pretty good understanding of java's classes (simple stuff i would think) should continue reading.
I've narrowed down my code just to show the main problem causing the error. class Tetris
{
public SmallBox smBox [] [];
public Tetris ()
{
SmallBox smBox [] [] = new SmallBox [xMax] [yMax];
}
public void dltLine (int y)
{
// temp removed
}
public void movePiece (int x)
{
// temp removed
}
}
class Piece
{
public SmallBox smPiece [] [];
public SmallBox smBox [] [];
public Piece ()
{
SmallBox smPiece [] [] = new SmallBox [xPiece] [yPiece];
for (int y = 0 ; y < 4 ; y++)
{
//smPiece [x] [y] = -10;
}
}
public void extractPiece () // this is used to take the piece from 'The Grid' and assign it back into the 'pieceGrid' (so that we can work with it)
{
boolean tempPiece = false;
int tempX1 = 0, tempY1 = 0; // used to mark the first tetris block found on the main grid.
for (int y = 0 ; y < yMax ; y++)
{
for (int x = 0 ; x < xMax ; x++)
{
if (smBox [x] [y].getActive = true) // once we find a moving block on the grid that belongs to piece.. we mark it.
{
tempX1 = x; // marks x cord of moving block
tempY1 = y; // marks y cord of moving block
SearchStart:
for (int startY = tempY1 ; startY < yMax ; startY++) // runs through the vertical part of the main grid to top of grid
{
if (smBox [x] [startY].getActice = true) // if the piece above the marked one is there we move to the left on the x-axis
{
break SearchStart;
}
}
}
}
}
}
}The orange font is where the error occurs. Apparently, i can't use the "smbox[][]" array from the tetris class. So how am I supposted to use it then? One other thing, is it okay to use smBox[][] in both classes this way? or should i pass it in from main() to the class?
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
Didn't really read through the code, mainly just the hightlighted line. Did you copy this straight from your file? Because you're using = instead of == in the if. This also happens 7 lines earlier. Also, the highlighted getActive is spelled wrong...
|
|
|
|
|
|
#3 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Quote:
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
|
#4 |
|
Professional Programmer
|
You also have another line above that one using '=' instead of '==', so if that line works then this one should too. Maybe if you post ALL of your code we can take a look at it for you.
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Yes.. as I said, "the problem is still there after your tips", meaning that I looked into the matter. The statements are now "=="
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 377
Rep Power: 0
![]() |
To use a variable method from another class, you will have to instantiate an object of that class to access it. You should also make smbox[][] private, and use a getter method to access it.
__________________
I am Addicted to Linux! |
|
|
|
|
|
#7 |
|
Expert Programmer
|
SmallBox smBox[][] = new SmallBox[xMax][yMax]; will create an array of null values of the specified dimensons. You have to initialize each index individually, eg, smBox[0][0] = new SmallBox();.
|
|
|
|
|
|
#8 |
|
Expert Programmer
|
As far how to access the smBox[][] array in the Tetris class from the Piece class, I would suggest passing the array as an argument to the Piece constructor.
Alternatively, you could make smBox[][] static. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|