Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're mixing up two different concepts, I'll explain both in A) and B).</p> <p>You have two options:</p> <p>A) If you want to continue using SurfaceView (which might be the right choice for game, since it gives you more control over how things are drawn), your Card object should extend Drawable, not View. Drawable is a dumb class that you just draw to a canvas or pass to widgets as their background etc. Android doesn't need to know much more about it. In this case you will need to check what card was hit yourself, like you described. Android can not handle this for you. You're doing this with a View instead of Drawable, this is wrong! Or at least a huge waste. See below for how to do it properly with views.</p> <p>B) Use views, which will handle touch events and many other things for you.</p> <p>View is a complex class that is supposed to exist in android's view hierarchy. The way you're using it you're throwing away most of it's functionality - when you draw a view straight to a canvas android isn't really aware of the view at all, so it can't handle touch events.</p> <p>A proper view-based implementation of your cards could look like this: Use a viewgroup as your base, this is what you set in setContentView(). This ViewGroup could just be a LinearLayout, or if you want the ability to set an arbitrary position and size on your cards of your cards, you could use AbsoluteView, although that's deprecated.</p> <p>Your cards are then added to the ViewGroup using addView(). The positioning of your Card view within this is governed by a LayoutParams object that you pass to addView(). This way android is aware of it's existence, and will handle touch events like this:</p> <ul> <li>The touch event first goes to your ViewGroup</li> <li>The ViewGroup onTouchEvent will send the event down to down to it's children's onTouchEvent (which have been added via addView()). So you just override this method in your Card class. You inherently know which Card's onTouchEvent was called since it gets called on the object itself.</li> <li>In your Card's onTouchEvent, you also let the parent know that you've handled the touch event by returning true. If you don't do this, the parent can handle the event instead.</li> </ul> <p>To be perfectly clear: You were NOT adding the views to SurfaceView, in fact, you can't since it is a not a ViewGroup. It's just a complex View object that allows you to do arbitrary drawing operations within it. But it is always a leaf of the View hierarchy, and can only handle it's own onTouchEvents, it will not send it down to children because it can't have any.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload