3
\$\begingroup\$

I am currently developing a game for Android. One of the things I am working on is moving the map by touch.

This is the code that handles getting speed, which is then added to the screens position and the next update draws the corresponding portion of the map.

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch(event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        if (released) {
            touchX = event.getX();
            touchY = event.getY();
            released = false;
        }
        else {
            float newX = event.getX();
            float newY = event.getY();
            speedX = (int) (newX - touchX);
            speedY = (int) (newY - touchY);
            touchX = newX;
            touchY = newY;
        }
        return true;
    case MotionEvent.ACTION_UP:
        released = true;
        return true;
    }
    return false;
}

It works fine, it does. But it seems like it is not enough of a fluid movement.

Does anyone have any idea of how I can make it more like a seamless motion?

EDIT:

And just for info, my fps is currently 80. So I do not believe it is caused by the bitmap drawing itself.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

My experience with Android and handling touch events is that it's very hard to look at the code and understand how it will work when you're running the application. It can even be a difference with different Android devices, so make sure that you test your app on an Android 2.3.5 device as well as an Android 4+ device (assuming you're targeting Android 2.3.5 as well).

From what I understand, "moving the map by touch" you mean that you are scrolling through the map, like a HorizontalScrollView combined with an ordinary (vertical) ScrollView. It is said to be possible to combine these two together to create a "two dimensional scroll view", I myself have not been able to accomplish that in a way that creates a smooth user experience (I actually think this is a bad way of doing it). I have however, managed to find a quite easy-to-use Android view class that takes care of all this scrolling. It seems however like the link has disappeared but you could try this GitHub project, if that doesn't work for you then let me know either by a comment or find me in The 2nd Monitor chatroom and I can provide the implementation I'm using.

As for your existing code, it's clean, it's straight-forward. But where/how is speedX/speedY and touchX/touchY used? Could the problem be there?

I only have one suggestion for improvement of your code:

switch(event.getAction()) {

Should be (see here)

switch(event.getActionMasked()) {

A general suggestion when working with touch events:

Log, Log, and Log Use Log.d("YOUR_TAG", "A whole bunch of information") like a maniac! The more you log, the more you will hopefully be able to understand about MotionEvents, and the better code you will be able to write.

\$\endgroup\$
2
  • \$\begingroup\$ I forgot to mention that I am using a SurfaceView. Thank you. And I removed the Log lines from the code to make it more clear and a shorter read. touchX/Y and newX/Y are used only for speedX/Y calculation. The speed is then added to the screenX/Y at the next update() call (update is called by a thread). screenX/Y gives information about which part of the map is to be drawn to the bitmap. \$\endgroup\$
    – Simon
    May 4, 2014 at 14:56
  • \$\begingroup\$ @Simon I did the best I could with what you provided. The code you posted didn't include any scrolling so it's hard to tell what's causing the lack of fluid movement for you. \$\endgroup\$ May 4, 2014 at 15:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.