Reputation: 57
I'm currently making a game that has blocks falling and the user controls them. I want to be able to detect when the blocks have stopped moving, because they have landed on another block or hit the bottom, in order to create a new block to drop (kind of like how Tetris spawns a new piece every time a block stops hits the bottom). How could I retrieve the y from this method so I know the block has stopped moving and has no velocity in the y direction?
Here is where I declare the variable and the method I'm using to change it (this moves the block downward):
int y = 0;
int ya = 0;
public void move() {
if(x + xa < game.getWidth() - WIDTH && x + xa > 0 ) {
ya = 2;
}if(y + ya > game.getHeight() - 50) {
ya = 0;
}
y = y + ya;
x = x + xa;
}
Somewhere else in the code, I would like to be able to do this:
// If the block stops moving (The y value in the move method is equal to 0)
if(y == 0) {
//Create new block object to drop, and allow the user to control this new block
}
Upvotes: 0
Views: 52
Reputation: 103
You have to declare the y variable out of all the methods, preferably after defining the class declaration( to be more readable code) and the second method also must be in the same class, if you are trying to access the y from other class you must create objects from the first class. Maybe you have tried to access to y value before running the move method (there are a lot of possibilities) Post your class(code ) here then I can help you better.
Upvotes: 1