Touch Screen problems...

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Touch Screen problems...

Postby rocksolide » Fri May 16, 2008 8:52 am

Hello everyone,


I'm having some problems with a touch screen code. In fact, I'm working on a 2D game, and the aim of this project is to use TCP/IP for multi player and touch screen to move your player. Basically, I wanted to move my player by sliding my finger on the screen (well on the simulator, move the mouse..) and once I pull up my finger from the screen (release the mouse button), I would like to see my player moving to the last pointed coordinate.

I used a integer table in order to record all the coordinates while keeping my finger clicking on the mouse button, but It doesn't seem to be working. My player is teleporting when I release my finger, and I want see him moving :roll: :roll: :roll: :roll:

Here is the code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public boolean onTouchEvent(MotionEvent event) {
  2.  
  3.                 int action = event.getAction();
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.                 int X = (int)event.getX();
  12.  
  13.                 int Y = (int)event.getY();
  14.  
  15.  
  16.  
  17.                 switch (action ) {
  18.  
  19.  
  20.  
  21.                 case MotionEvent.ACTION_DOWN:
  22.  
  23.                         // test if I first pointed on my player, if yes, then move, else do nothing...
  24.  
  25.                         if(mX+15>=X && mX-15<=X && mY+15>=Y && mY-15<=Y){
  26.  
  27.                                 bal=true;
  28.  
  29.                                 mCurX = X;
  30.  
  31.                                 mCurY = Y;
  32.  
  33.                                 //X = (int)event.getX();
  34.  
  35.                                 //Y = (int)event.getY();
  36.  
  37.                         }
  38.  
  39.                         else{bal = false;}
  40.  
  41.                         break;
  42.  
  43.  
  44.  
  45.  
  46.  
  47.                 case MotionEvent.ACTION_MOVE:
  48.  
  49.  
  50.  
  51.                         // saving the coordinates
  52.  
  53.                         if(!bal){
  54.  
  55.                                 count=0;
  56.  
  57.                                 xCoord[count] = X;
  58.  
  59.                                 yCoord[count] = Y;
  60.  
  61.                                 count++;
  62.  
  63.                         }
  64.  
  65.                         break;
  66.  
  67.  
  68.  
  69.                 case MotionEvent.ACTION_UP:
  70.  
  71. //                      if (isActionMove)
  72.  
  73.                         if(!bal)
  74.  
  75.                                 // mX and mY are the coordinates of the player on the screen
  76.  
  77.                                 for (int i=0; i<count; i++) {                      
  78.  
  79.                                         mX=xCoord[i];
  80.  
  81.                                         mY=screenHeight - yCoord[i];
  82.  
  83.                                         //mPersoImage.setBounds(X,screenHeight- Y, X+15,screenHeight -Y+15);
  84.  
  85.                                         mPersoImage.setBounds(xCoord[i],yCoord[i],xCoord[i]+15,yCoord[i]+15);
  86.  
  87.                                        
  88.  
  89.                                 }
  90.  
  91.                         count=0;
  92.  
  93.                         break;
  94.  
  95.                 }
  96.  
  97.                 invalidate();
  98.  
  99.                 return true;
  100.  
  101.         }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4




If you need the whole file, let me know, I can forward it to you.


Thanks for you help!


Rocksolide
rocksolide
Junior Developer
Junior Developer
 
Posts: 11
Joined: Wed Apr 30, 2008 11:44 am
Location: Grenoble, France

Top

Postby res » Fri May 16, 2008 7:15 pm

Can you please post your drawing logic?

You have a variety of objects for X & Y co-ordinates: mCurX, mCurY, xCoord[], yCoord[], mX, mY. Since you stated that the player teleports when you release you finger I would guess you are using mX and mY to draw the players current location. However mX and mY don't get set during the ACTION_MOVE case so the player would stay still until you release your screen press.

This is just a guess though from what you have said and the code provided.
res
Senior Developer
Senior Developer
 
Posts: 146
Joined: Tue Nov 20, 2007 8:14 pm
Location: United States

Postby rocksolide » Sat May 17, 2008 11:36 am

Hello!

in fact, mCurX and mCurY are used to get the first coordinates when I click, and then I compare these coordinates to the player's coordinates. If these coordinates matches the clicked coordinates, the we can start to move the player. We have to click on the player in order to move him. While moving, I gather all the coordinates from where I pass with the mouse cursor, and once I release it the mouse button, I wand my player to move straight to the last pointed coordinates. I believe if I put mX and mY, which are the player's moving coordinates, in Action_move, then when I'll be moving the mouse, the player will be moving too at the same time....

I will try other things by the time, and if you have an idea, it's always good :wink: :wink: :wink: :wink: :wink: :wink: :wink: :wink:


Thanks again for your contribution!
rocksolide
Junior Developer
Junior Developer
 
Posts: 11
Joined: Wed Apr 30, 2008 11:44 am
Location: Grenoble, France

Postby Jesmiatka » Fri May 23, 2008 8:50 am

Hi,

I think I found your problem!
Every time the action is move you set count to 0:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.           case MotionEvent.ACTION_MOVE:
  2.  
  3.  
  4.  
  5.                // saving the coordinates
  6.  
  7.                if(!bal){
  8.  
  9.                     count=0;
  10.  
  11.                     xCoord[count] = X;
  12.  
  13.                     yCoord[count] = Y;
  14.  
  15.                     count++;
  16.  
  17.                }
  18.  
  19.                break;
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


The solution would be to set count to 0 in the ACTION_DOWN, and remove it in ACTION_MOVE.
You would get something like this:
Untested!

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.           case MotionEvent.ACTION_DOWN:
  2.  
  3.                         // test if I first pointed on my player, if yes, then move, else do nothing...
  4.  
  5.                if(mX+15>=X && mX-15<=X && mY+15>=Y && mY-15<=Y){
  6.  
  7.                     bal=true;
  8.  
  9.                     mCurX = X;
  10.  
  11.                     mCurY = Y;
  12.  
  13.                     count=0;
  14.  
  15.                     //X = (int)event.getX();
  16.  
  17.                     //Y = (int)event.getY();
  18.  
  19.                }
  20.  
  21.                else{bal = false;}
  22.  
  23.                break;
  24.  
  25.  
  26.  
  27.  
  28.  
  29.           case MotionEvent.ACTION_MOVE:
  30.  
  31.  
  32.  
  33.                // saving the coordinates
  34.  
  35.                if(!bal){
  36.  
  37.                     xCoord[count] = X;
  38.  
  39.                     yCoord[count] = Y;
  40.  
  41.                     count++;
  42.  
  43.                }
  44.  
  45.                break;
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Good luck!

Yours sincerely,
Mike
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 5 guests