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
Here is the code:
Using java Syntax Highlighting
- public boolean onTouchEvent(MotionEvent event) {
- int action = event.getAction();
- int X = (int)event.getX();
- int Y = (int)event.getY();
- switch (action ) {
- case MotionEvent.ACTION_DOWN:
- // test if I first pointed on my player, if yes, then move, else do nothing...
- if(mX+15>=X && mX-15<=X && mY+15>=Y && mY-15<=Y){
- bal=true;
- mCurX = X;
- mCurY = Y;
- //X = (int)event.getX();
- //Y = (int)event.getY();
- }
- else{bal = false;}
- break;
- case MotionEvent.ACTION_MOVE:
- // saving the coordinates
- if(!bal){
- count=0;
- xCoord[count] = X;
- yCoord[count] = Y;
- count++;
- }
- break;
- case MotionEvent.ACTION_UP:
- // if (isActionMove)
- if(!bal)
- // mX and mY are the coordinates of the player on the screen
- for (int i=0; i<count; i++) {
- mX=xCoord[i];
- mY=screenHeight - yCoord[i];
- //mPersoImage.setBounds(X,screenHeight- Y, X+15,screenHeight -Y+15);
- mPersoImage.setBounds(xCoord[i],yCoord[i],xCoord[i]+15,yCoord[i]+15);
- }
- count=0;
- break;
- }
- invalidate();
- return true;
- }
Parsed in 0.035 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



