Using the touchscreen for game control

Put your problem here if it does not fit any of the other categories.

Using the touchscreen for game control

Postby FlySwat » Sun Mar 15, 2009 3:24 am

I'm working on my first video game for the Android platform as a bit of a nights and weekends project.

It is coming along nicely, but I am very unhappy with the control sensativity.

In this game, you move an object left and right on the screen. On the bottom of the screen is a "touchpad" of sorts, which is where your finger should rest.


Code: Select all
  /-------------------------
    |                               |
    |                               |
    |                               |
    |       Game Area        |
    |                               |
    |                               |
    |                               |
    |                               |
    |                               |
    /-------------------------
    |                                |
    |       Touch Area        |
    |                               |
    -------------------------/


I am currently using a state variable to hold "MOVING_LEFT, MOVING_RIGHT, NOT_MOVING" and am updating the location of the player object each frame based on that variable.

However, my code that reads the touchscreen input and sets this state variable is either too sensative, or too laggy, depending on how I tweak it:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void doTouch (MotionEvent e) {
  2.                 int action = e.getAction();
  3.                                
  4.                 if (action == MotionEvent.ACTION_DOWN) {
  5.                         this.mTouchX = (int)e.getX();
  6.                         this.mTouchY = (int)e.getY();                  
  7.                 }
  8.                 else if (action == MotionEvent.ACTION_MOVE) {
  9.                         if ((int)e.getX() >= this.mTouchX) {
  10.                                 this.mTouchX = (int)e.getX();
  11.                                 this.mTouchY = (int)e.getY();  
  12.                                 if (this.TouchRect.contains(this.mTouchX, this.mTouchY)) {                             
  13.                                         this.mTouchDirection = MOVING_RIGHT;
  14.                                 }
  15.                         }
  16.                         else if ((int)e.getX() <= this.mTouchX) {
  17.                                 this.mTouchX = (int)e.getX();
  18.                                 this.mTouchY = (int)e.getY();
  19.                                 if (this.TouchRect.contains(this.mTouchX, this.mTouchY)) {                             
  20.                                         this.mTouchDirection = MOVING_LEFT;
  21.                                 }
  22.                         }
  23.                         else {
  24.                                 this.mTouchDirection = NOT_MOVING;
  25.                         }                              
  26.                 }
  27.                 else if (action == MotionEvent.ACTION_UP) {
  28.                         this.mTouchDirection = NOT_MOVING;
  29.                 }              
  30.         }
  31.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

The idea is that when there is any movement, I check the previous location of the users finger and then figure out what direction to move the player.

This doesn't work very well, I figure there are some Android developers on here who have figured out how to do good controls via a touchscreen and can give some advice.
FlySwat
Freshman
Freshman
 
Posts: 6
Joined: Sat Mar 07, 2009 10:53 pm

Top

Postby MrSnowflake » Sun Mar 15, 2009 11:46 am

What exactly is your problem? Is it slowing down your application too much, or is your avatar moving too fast in game?
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby FlySwat » Sun Mar 15, 2009 11:48 am

MrSnowflake wrote:What exactly is your problem? Is it slowing down your application too much, or is your avatar moving too fast in game?


Avatar is moving far too fast, and turning around is rarely registered properly.
FlySwat
Freshman
Freshman
 
Posts: 6
Joined: Sat Mar 07, 2009 10:53 pm

Postby MrSnowflake » Sun Mar 15, 2009 12:00 pm

Because you are on a touch screen, your finger moves a couple hundred times a second, so you avatar might also move that much times. You should make a threshold value, for which not to move if the difference between the previous frame and this frame is smaller then this threshold. Also, you might wanna sleep the main thread for a very small time, because it tends to hog the cpu when your finger is on the screen.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 8 guests