Bug with touch movement in a game

I’m working on a 2d platfrom game for android and I’m having some trouble with the touch events. this is my code:


public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
    if (e.getAction() == MotionEvent.ACTION_DOWN) {
        for (int i = 0; i < object.size(); i++) {
            tempObject = object.get(i);
            if (tempObject.getId() == ObjectId.Player) {
                if (moveRight.contains(scaledX, scaledY)) {
                    Log.d("PlayState", "Move button is pressed!");
                    tempObject.setMovingRight(true);

                } else {
                    tempObject.setMovingRight(false);
                }

            }
        }
    }
    if (e.getAction() == MotionEvent.ACTION_UP) {
        for (int i = 0; i < object.size(); i++) {
            tempObject = object.get(i);
            if (tempObject.getId() == ObjectId.Player) {
                if (moveRight.contains(scaledX, scaledY)) {
                    Log.d("PlayState", "Move button is released!");
                    tempObject.setMovingRight(false);

                }
            }
        }
    }
    return true;
}


When I touch the rectangle and release inside the rectangle it works great. The problem is that if I touch the rectangle and move my finger somewhere else while touching, the character keeps on moving. My question is how can I program it so “movingRight” will only be true while the touch is happening inside the rectangle. Thanks !! :slight_smile: