Day 6: Let’s improve the touch control.
Posted by Plaidman on April 1st, 2010
I decided to publish the game as it is and hang up my Android development hat. Back to playing WoW. There’s a little late April Fools joke for you; I hope you enjoyed it.
There’s one more game control feature we left out from yesterday that I’d like to add.
The touch screen, as we left it yesterday, requires you to tap the screen every time you want to move Red a space. The TOUCH event only fires once, even if we hold our finger on the screen. We want to make it so if you tap and hold, it keeps moving your guy in the same direction. For extra credit we’ll make it so when you move your finger, the guy will start moving in the new direction. The thing is, Android doesn’t come with a HOLD touch event, so we’ll have to get creative with the TOUCH, MOVE, and RELEASE events.
First we should update our TOUCH event to store the touched coordinates into a variable (we’ll call it touchCoordinates), and set a flag that says we’re holding our finger on the screen (touchHolding), then we’ll call the analysis routine to periodically check if we’re still touching the screen and which direction we need to move. Now we’ll make the new RELEASE event to unset the touchHolding flag, so the analysis routine knows to stop checking. Finally, we’ll define the MOVE event to update the touchCoordinates location. Our analysis routine now knows when we touch and release the screen, and when we move our finger to a new direction. Clever huh?!
Next update I’ll write about adding enemies.
Current demo: We can tap-hold to move Red now. I’ve also added enemies with very simple AI (wanders randomly) and no stats. Here’s a video if you want to see it in action: http://www.youtube.com/watch?v=VsK2LJ9BmqA
Now the good stuff. Let’s make some enemies!
We’re going to extend the Character class to make the Monsters, so the Monster will inherit everything from the Character class (our movement functions, most importantly). The Monster will add two things that the Character didn’t have: a link to the Player and an Artificial Intelligence routine. The link to the player is used so the AI routine can know where the player is, and potentially try to pursue or run away from the player depending on if he’s aggressive or wounded.
For now we’re not going to worry about super complex AI, let’s keep it simple. We’ll use a random number generator to give us a number between 0 and 15. If the number is between 0 and 7, let’s say our enemy should go in a random direction. If it’s 8-15, Red should stand still. Since we have the movement functions already inherited from the Character object, we don’t need to do anything special! The monster will automatically check to see that it’s not trying to move on a wall tile, another monster or the player character.
Now let’s update our Player object. Since we have enemies, we want this character to be able to kill them and level up. The Player is inheriting the movement routine from the Character class, but we want to override it so we can check to see if we’re close to a monster, and if so, kill it. The nice thing about inheritance is overriding the Player’s movement routine won’t affect the Monster routine or anything else that’s inheriting from the Character class.
Back to the main game handler, we’ll keep track of a list of monsters, so we can go through them and perform some action – draw them, AI routine, etc. In fact, let’s add Monsters’ AI to the movement routine so after Red moves, the monsters can move too. While we’re at it, we should add a section to the drawing routine to draw the monsters, so we can actually see them. Now there’s just one more update to give the game an official objective: let’s add one monster at the start of the game, and add more monsters each time Red clears the map. Sounds fun doesn’t it? Yes, it does.