0

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
}
4
  • This doesn't make sense. You don't need to "extract" the variables, as they're not local variables (i.e. you declared them elsewhere). Commented Mar 5, 2015 at 23:19
  • @August, the variables are all stored outside the method, but when I print out their values they never change Commented Mar 5, 2015 at 23:20
  • Then please construct a minimal test-case that demonstrates this. Commented Mar 5, 2015 at 23:21
  • @OliverCharlesworth I tried to update the question the best I could (sorry for the poor question it's my first one) Commented Mar 5, 2015 at 23:29

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.