HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Tile map movement by touch

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
movementmaptouchtile

Problem

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.

Solution

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.

Code Snippets

switch(event.getAction()) {
switch(event.getActionMasked()) {

Context

StackExchange Code Review Q#48860, answer score: 2

Revisions (0)

No revisions yet.