![]() | Where to put code? |
|
16 Nov 2011, 11:49
Victoria Stewart (3 posts) |
Hi all, I’m just wondering how to know where in PuzzleView.java to put the code for overriding the onTouchEvent() method. Looking at the downloaded code, it seems this goes before the switch statement for onKeyDown(). Within this switch statement, it checks for input from the D-pad but not for input from the touchscreen? Therefore the onTouchEvent() code needs to go BEFORE the onKeyDown() function in order for the input from the touchscreen to be read and then used in onKeyDown()?? If it was after onKeyDown() there would be no input so the game wouldn’t do anything from this point or would throw up junk? I’ll have a play with placing the code in different parts of Puzzleview to see what happens, but an explanation would be helpful. Sorry coming from a C background and haven’t programmed in years! Many thanks. |
|
16 Nov 2011, 12:29
Anthony Shaw (115 posts) |
In C terms think of things like onTouchEvent() as callbacks, so it does not matter where in the code they go provided they are not in another function as this would violate the language systax. They are actually called in response to events (key presses, etc.) and are known as listeners. It’s been a while since I wrote any C code, I turned to the dark side (java programming) a couple of years ago. |
|
17 Nov 2011, 15:14
Ed Burnette (1316 posts) |
Android has many different kinds of inputs, and for each type it calls callback functions on your View to give the view a chance to handle them. The most important ones are: 1. Keys. Example: The Back button or the arrow key on a keyboard or the buttons in a D-Pad. Callback: onKeyDown, onKeyUp The order they appear in the source code doesn’t matter, any more than the order of functions in C code matters. The C equivalent would be an array of function pointers. You have two pointers for a key event handler, one for touch events, and one for other events. Internally Android takes the address of onKeyDown, onKeyUp, onTouchEvent, and onGenericMotionEvent and initializes those pointers with the address, or puts a null there if you didn’t declare them. Then when an event comes in, Android does something like this pseudo code: |
| You must be logged in to comment |

