Frage

I am developing an android game. in this i have two moving images on the screen, am able to drag this images, but my further requirement is to pause the image when i touch the image. am not finding any event to pause the image , and than restart moving at the same position where image have been paused.

Can any body tell me the solution. Thanks.

am using onTouch to drag and move the image.

public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            Parcelable state=null;
            // delegating event handling to the droid
            droid1.handleActionDown((int)event.getX(), (int)event.getY());
            droid2.handleActionDown((int)event.getX(), (int)event.getY());
            // check if in the lower part of the screen we exit
            if (event.getY() > getHeight() - 50) {

                thread.setRunning(false);
                ((Activity)getContext()).finish();
            } else {
                Log.d(TAG, "Coords: x=" + event.getX() + ",y=" + event.getY());

            }
        } if (event.getAction() == MotionEvent.ACTION_MOVE) {
            // the gestures
            if (droid1.isTouched()) {
                // the droid was picked up and is being dragged
                droid1.setX((int)event.getX());
                droid1.setY((int)event.getY());

            }
            if (droid2.isTouched()) {
                // the droid was picked up and is being dragged
                droid2.setX((int)event.getX());
                droid2.setY((int)event.getY());
            }

        } if (event.getAction() == MotionEvent.ACTION_UP ) {
            // touch was released
            if (droid1.isTouched()) {
                droid1.setTouched(false);
            }
            if (droid2.isTouched()) {
                droid2.setTouched(false);
            }
        }


        return true;
    }
War es hilfreich?

Lösung

Based on your code and de link in your comment, there are a few things that need to be changed in order to accomplish your goal. First of all, you will need to be able to differentiate between a single tap and drag movement. The former will 'pause' an image/droid, whereas the latter will allow you to drag it around.

One way to go at this would be to keep a flag that determines the action. Something like this:

private boolean mIsDragging = false;

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        // delegating event handling to the droid
        droid1.handleActionDown((int)event.getX(), (int)event.getY());
        droid2.handleActionDown((int)event.getX(), (int)event.getY());
        ...
        mIsDragging = false;
    } if (event.getAction() == MotionEvent.ACTION_MOVE) {
        // the gestures
        if (droid1.isTouched()) {
            // the droid was picked up and is being dragged
            droid1.setX((int)event.getX());
            droid1.setY((int)event.getY());
        }
        if (droid2.isTouched()) {
            // the droid was picked up and is being dragged
            droid2.setX((int)event.getX());
            droid2.setY((int)event.getY());
        }
        mIsDragging = true;  
    } if (event.getAction() == MotionEvent.ACTION_UP ) {
        if (!mIsDragging) {
            // Not dragging means a 'click', thus toggle pausing
            if (droid1.isTouched()) {
                droid1.setPaused(!droid1.isPaused());
            }
            if (droid2.isTouched()) {
                droid2.setPaused(!droid2.isPaused());
            }
        }
        // Always reset on up action
        if (droid1.isTouched()) {
            droid1.setTouched(false);
        }
        if (droid2.isTouched()) {
            droid2.setTouched(false);
        }
        mIsDragging = false;
    }
    return true;
}

To actually stop the image/droid from making anymore movements, you should add some method to set the speed in both horizontal and vertical direction to '0'. Alternatively, you could set a flag that indicates whether you want to update the position.

Based on which approach you take, you may also have to change the update logic to take into account the 'paused' state. If you simply choose to set the speed to (0,0), then you'll probably won't have to do anything as the displacement will then result in '0' for both directions.

In the code snippet I'm assuming there's a setPause(boolean) and isPaused() method. These should either work on a flag or the speed values directly.

For example, using a flag:

private boolean mIsPaused = false;

public void setPaused(boolean pause) {
    mIsPaused = pause;
}

public boolean getPaused() {
    return mIsPaused;
}

...

public void update() {
    // Only update if we're not dragging and not 'paused'
    if (!touched && !mIsPaused) {
        x += (speed.getXv() * speed.getxDirection());
        y += (speed.getYv() * speed.getyDirection());
    }
}

Or directly using the speed values:

private Speed mSpeed = new Speed(1,1);

public void setPaused(boolean pause) {
    pause ? mSpeed.setSpeed(0,0) : mSpeed.setSpeed(1,1);
}

public boolean getPaused() {
    return mSpeed.getX() == 0 && mSpeed.getY() == 0;
}

...

public void update() {
    // No change necessary as multiplying by '0' results in a zero displacement
}

Note that I have not tested any of the code nor is it intented to be a complete example.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top