Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (798)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (865)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to handle multi touch?  (Read 2332 times)
0 Members and 1 Guest are viewing this topic.
Offline Shuster
« Posted 2014-12-31 12:04:07 »

Hello,
I'm working on a platfrom game for android and as in most platfrom games the player will need to use multi touch, for example moving right and jumping simultaneous.
This my code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
   public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
      int action = e.getAction() & MotionEvent.ACTION_MASK;
      for (int i = 0; i < object.size(); i++) {
         tempObject = object.get(i);
         if (tempObject.getId() == ObjectId.Player) {

            switch (action) {

            case MotionEvent.ACTION_UP:
               if (moveLeft.contains(scaledX, scaledY)) {
                  tempObject.setMovingLeft(false);

               }
               if (moveRight.contains(scaledX, scaledY)) {
                  tempObject.setMovingRight(false);

               }

               break;
            case MotionEvent.ACTION_POINTER_UP:

               break;

            case MotionEvent.ACTION_DOWN:
               if (jump.contains(scaledX, scaledY)) {
                  if (tempObject.getVelY() == 0 && tempObject.isJumping() == false) {
                     tempObject.setVelY((float) -11.5);
                     tempObject.setJumping(true);
                  }
               }
               if (restart.contains(scaledX, scaledY)) {
                  restart();

               }

               break;
            case MotionEvent.ACTION_POINTER_DOWN:

               break;

            case MotionEvent.ACTION_MOVE:

               if (moveLeft.contains(scaledX, scaledY)) {
                  tempObject.setMovingLeft(true);
                  tempObject.setMovingRight(false);

               }
               if (moveLeftExit.contains(scaledX, scaledY) && !moveLeft.contains(scaledX, scaledY)) {
                  tempObject.setMovingLeft(false);

               }
               if (moveRight.contains(scaledX, scaledY)) {
                  tempObject.setMovingRight(true);
                  tempObject.setMovingLeft(false);

               }
               if (moveRightExit.contains(scaledX, scaledY) && !moveRight.contains(scaledX, scaledY)) {
                  tempObject.setMovingRight(false);

               }

               break;
            }

         }
      }

      return true;
   }


The problem is if I touch a rect while touching a different rect, the new touch wont react and the old on gets "stuck".
Thanks Smiley
Offline Elsealabs
« Reply #1 - Posted 2014-12-31 12:06:04 »

Someone probably won't be able to trouble shoot this without knowing how and when these motion events are fired. Currently I'd have to infer what each do, and my inference may not exactly be the case. Care to share what framework or library you are using, so then other users or I could look at its documentation?

Edit: This may be going over my head, and may be solvable by some other developers on the site, but I also do not see how anyone could propose a real fix without also knowing how your object, "tempObject," is designed (the contents of the movement methods, class structure, etc).

Offline Shuster
« Reply #2 - Posted 2014-12-31 12:13:15 »

Someone probably won't be able to trouble shoot this without knowing how and when these motion events are fired. Currently I'd have to infer what each do, and my inference may not exactly be the case. Care to share what framework or library you are using, so then other users or I could look at its documentation?
Thanks for answering Smiley
I am not using any framework, just the eclipse and android bundle, and for the motion event I use "android.view.MotionEvent"

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Elsealabs
« Reply #3 - Posted 2014-12-31 12:16:53 »

Thanks for answering Smiley
I am not using any framework, just the eclipse and android bundle, and for the motion event I use "android.view.MotionEvent"

Ah, I did not realize, as I have not worked with native Android before, and could not see your imports. Though it would still be beneficial to see the other classes you are using with this. Like how the "setMoving...()" methods are set up, etc.

Offline Shuster
« Reply #4 - Posted 2014-12-31 12:21:59 »

Thanks for answering Smiley
I am not using any framework, just the eclipse and android bundle, and for the motion event I use "android.view.MotionEvent"

Ah, I did not realize, as I have not worked with native Android before, and could not see your imports. Though it would still be beneficial to see the other classes you are using with this. Like how the "setMoving...()" methods are set up, etc.

What each motion event does doesnt really matter, the point is that if I touch the screen with one finger the second one wont  triger the motion event action.
Offline Elsealabs
« Reply #5 - Posted 2014-12-31 12:24:45 »

What each motion event does doesnt really matter, the point is that if I touch the screen with one finger the second one wont  triger the motion event action.

I must have misunderstood your question, then. Hopefully someone can answer it!  Smiley

Offline Elsealabs
« Reply #6 - Posted 2014-12-31 12:46:51 »

Maybe do your cases more like this?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
...

    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_DOWN: {
        ...
      break;
    }
    case MotionEvent.ACTION_MOVE: { // a pointer was moved
      ...
      break;
    }
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
    case MotionEvent.ACTION_CANCEL: {
      ...
      break;
    }

...


What I see other people online doing is similar to the following:

  • In touch down, add new pointer to list of current pointers
  • In action move, do logic
  • In touch up, remove the now inactive pointer from the list of current pointers

Offline Shuster
« Reply #7 - Posted 2014-12-31 13:53:43 »

Maybe do your cases more like this?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
...

    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_DOWN: {
        ...
      break;
    }
    case MotionEvent.ACTION_MOVE: { // a pointer was moved
      ...
      break;
    }
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
    case MotionEvent.ACTION_CANCEL: {
      ...
      break;
    }

...


What I see other people online doing is similar to the following:

  • In touch down, add new pointer to list of current pointers
  • In action move, do logic
  • In touch up, remove the now inactive pointer from the list of current pointers

This is my first android project so I have very basic knowledge in touch events.
How can I handle a list of pointers?(how to add pointers,remove and use them).
Offline Elsealabs
« Reply #8 - Posted 2014-12-31 14:07:46 »

Here's a really good tutorial on handling pointers and multi-touch in Android. Read it, and if you still have questions, of course come back and ask them.  Smiley

http://www.vogella.com/tutorials/AndroidTouch/article.html

Pages: [1]
  ignore  |  Print  
 
 

 
Riven (81 views)
2019-09-04 15:33:17

hadezbladez (4337 views)
2018-11-16 13:46:03

hadezbladez (1547 views)
2018-11-16 13:41:33

hadezbladez (4462 views)
2018-11-16 13:35:35

hadezbladez (871 views)
2018-11-16 13:32:03

EgonOlsen (4152 views)
2018-06-10 19:43:48

EgonOlsen (4855 views)
2018-06-10 19:43:44

EgonOlsen (2803 views)
2018-06-10 19:43:20

DesertCoockie (3707 views)
2018-05-13 18:23:11

nelsongames (3998 views)
2018-04-24 18:15:36
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!