Android Game Development – Part 2 Player & Input controls
Input Controls
In this section we will modify the GameEngineView to handle input controls. We do this by calling setFocusable(true); on the constructor of the view. All that is left is to override the onKeyDown and onKeyUp events.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == android.view.KeyEvent.KEYCODE_DPAD_LEFT )
{
st.setCurrentAnimation("walk_left", true);
st.AnimateTo(new Point( st.getXpos()-40, st.getYpos()));
}
if(keyCode == android.view.KeyEvent.KEYCODE_DPAD_RIGHT)
{
st.setCurrentAnimation("walk_right", true);
st.AnimateTo(new Point( st.getXpos()+40, st.getYpos()));
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(keyCode == android.view.KeyEvent.KEYCODE_DPAD_LEFT )
st.setCurrentAnimation("idle_left", false);
if(keyCode == android.view.KeyEvent.KEYCODE_DPAD_RIGHT)
st.setCurrentAnimation("idle_right", false);
st.MoveTo(new Point( st.getXpos(), st.getYpos()));
return super.onKeyUp(keyCode, event);
}
In this snippet the “st” object is an Instance of the player object which will be discussed shortly. Note that on key down animate player, on key up we return him to the idle state.
Player
The player class houses the characteristics that a player consists of. We will use the Sprite Tile as the base object, this will take care of the visual part of our player. We will add function and attributes for animation
package com.warriormill.warriorengine;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import com.warriormill.warriorengine.drawable.SpriteTile;
public class Player extends SpriteTile {
public float lifebar=100.0f;
private Point move_to = new Point(10,10);
public Player(int BitmapResourceId, int XmlAnimationResourceId,
Context context) {
super(BitmapResourceId, XmlAnimationResourceId, context);
}
public void AnimateTo(Point pt)
{
move_to = pt;
}
public void MoveTo(Point pt)
{
move_to =pt;
this.setXpos(pt.x);
this.setYpos(pt.y);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
updateLocation();
}
private void updateLocation()
{
if(move_to.y > this.getYpos())
this.setYpos(getYpos()+1);
else if(move_to.y < this.getYpos())
this.setYpos(getYpos()-1);
if(move_to.x > this.getXpos())
this.setXpos(getXpos()+1);
else if(move_to.x < this.getXpos())
this.setXpos(getXpos()-1);
}
}
Preview


November 8th, 2009 at 6:48 am
very very good tutorial
November 12th, 2009 at 11:25 pm
Thanks for the tutorials, I am looking forward to seeing more.
April 19th, 2010 at 6:31 am
I went through both of ur tutorials and they are simply awesome…I am making a game engine for android and your tutorials really helped me in making the sprite animation part of the engine. Thanx once again… =D ^_^
April 25th, 2010 at 6:45 am
Thanks I’m glad you enjoyed it. I’m thinking of re-tweaking to be muli-platform and OpenGL driven.
April 29th, 2010 at 10:21 pm
Some people asked to post the updated source code to the video i put up for the 3rd part of the serious.
Download Source Code
[up dated link]
June 8th, 2010 at 2:35 pm
The link u posted in ur comment is not working. It’s nice to know that someone out there is making android game tuts. I’ve been searching them for ages.
June 8th, 2010 at 2:55 pm
http://warriormill.com/wp-content/uploads/2010/04/part-3.zip
June 9th, 2010 at 2:09 pm
There one thing in the code. I did a little testing in the Code using System.currentTimeMillis() and I found that the average time between each iteration of the game loop was 16ms, which was to be expected since the Android device is not capable of doing extremely fast calculations:
http://www.youtube.com/watch?v=U4Bk5rmIpic
So I found that using 8ms sleep is abt optimal. Also the engine could be made better by making the position changes time dependent as explained in the video. Thats abt all the suggestions I can come up with.
June 11th, 2010 at 3:14 am
this is very interesting,
i hope you can find time to continue your tutorial series
June 12th, 2010 at 12:00 am
Thanks in going to do some OpenGL es ones
July 8th, 2010 at 9:25 pm
Good job, Hope for your next tutorial.
November 7th, 2010 at 3:43 pm
Hi, what awesome tutorials!
Ive always wanted to make a game, and now maybe i can start
I just wanted to knoe, if i did make a game, and i sold it on the android market place.. would i have to pay you or would referencing your site be enough? :S
thanks
cha
January 2nd, 2011 at 9:10 am
plz upload next tutorial…..we r eagerly waiting….:D
April 3rd, 2011 at 3:46 pm
@Chachaji nope feel free to do what ever you want
April 30th, 2011 at 9:29 pm
many thanks, very straightforward and intuitive! I’ve looked at so many examples that left me with more questions than answers – including the ones in the android sdk.
June 22nd, 2011 at 3:15 pm
How do I use this on a droid x? I don’t have the up, down, left, right buttons
June 22nd, 2011 at 4:02 pm
re-map the keys…this made during the g1 days. at this point it might be worth putting in on screen controls