Make sure your view has input focus.
On Wed, Mar 18, 2009 at 7:51 PM, djr <[email protected]> wrote:
>
> I have tried all day to capture a trackball event in a View. Basically
> I want to use the trackball to move a Sprite in a 2D game left and
> right. The onTouchEvent works perfectly well in the View to move the
> Sprite with my finger but it would be nicer and cleaner to use the
> trackball.
>
> The onTouchEvent looks like:
>
> @Override
> public boolean onTouchEvent (MotionEvent motionEvent)
> {
> int action = motionEvent.getAction ();
> int currentX = (int) motionEvent.getX ();
> int currentY = (int) motionEvent.getY ();
> if (action == MotionEvent.ACTION_MOVE)
> {
> do stuff here and it works great
> }
> return true;
> }
>
> The onTrackballEvent looks like:
>
> @Override
> public boolean onTrackballEvent (MotionEvent motionEvent)
> {
> int action = motionEvent.getAction ();
> int currentX = (int) motionEvent.getX ();
> int currentY = (int) motionEvent.getY ();
>
> if (action == MotionEvent.ACTION_MOVE)
> {
> do stuff here and it never gets here
> }
>
> return true;
> }
>
> In the debugger I can never get a trackball event to enter the above
> code. I also tried setting up an onKey listener in the View that looks
> like:
>
> this.setOnKeyListener (
> new OnKeyListener ()
> {
> public boolean onKey (View view, int keyCode,
> KeyEvent keyEvent)
> {
> //int currentX = (int) motionEvent.getX ();
> //int currentY = (int) motionEvent.getY ();
>
> if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT)
> {
> do stuff
> }
> else if (keyCode ==
> KeyEvent.KEYCODE_DPAD_RIGHT)
> {
> do stuff
> }
> return true;
> }
> });
>
> Can anyone provide me with a simple example that uses the trackball to
> move something in a View in a 2D type game world (not a menu or UI
> type of example)? Or can someone explain what I'm not doing. Thanks so
> much for any help you can give me.
>
> PS - I did try and study the LunarLander code and I don't see what I'm
> doing differently.
>
> >
>
--
Dianne Hackborn
Android framework engineer
[email protected]
Note: please don't send private questions to me, as I don't have time to
provide private support. All such questions should be posted on public
forums, where I and others can see and answer them.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---