The Key movement for a particular object

November 30th, 2011 by aabhaanjan

Lets learn today how to handle the the Key movement for a particular object.
Here I have used a ball as object wich will move 10 px according to the key:

There are following steps :
1. Create a new motodev project ‘Ballmovement’.
New_Project

2. Your AndroidManifest.xml will look like this

[xml]










[/xml]

3. Put the ball_1.png in your res/drwable folder

4. Now put following code in ManiActivity.java

[xml]
package com.ballmovent;

import android.app.Activity;

import android.os.Bundle;

import android.view.Window;

public class MainActivity extends Activity {

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(new GameView(this));

}

}
[/xml]

5. Create a new class GameView.java. Now put following code in GameView.java

[xml]

package com.ballmovent;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import com.ballmovent.ball;

public class GameView extends View {

private ball ballInstance;
public static final int UP = 0, DOWN = 1, RIGHT = 2, LEFT = 3;

public GameView(Context context) {
super(context);
ballInstance = new ball(BitmapFactory.decodeResource(context.getResources(),R.drawable.ball_1), 50, 50);
setFocusable(true);
this.setFocusableInTouchMode(true);
}

@Override

protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
ballInstance.draw(canvas);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent evt) {
boolean moved = false;
Log.v(“aks 1″+ moved, “index=” + 1);
switch(keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
moved = ballInstance.move(UP);
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
moved = ballInstance.move(DOWN);
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
moved = ballInstance.move(RIGHT);
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
moved = ballInstance.move(LEFT);
break;
default:
return super.onKeyDown(keyCode,evt);
}
if(moved) {
//the ball was moved so we’ll redraw the view
ballInstance.setX((int)ballInstance.getX());
ballInstance.setY((int)ballInstance.getY());
invalidate();
}
return true;
}

}

[/xml]

7. Create a new class ball.java. Now put following code in ball.java for varous function etc

[xml]
package com.ballmovent;

import android.graphics.Bitmap;
import android.graphics.Canvas;

public class ball {

private Bitmap bitmap;
private int x;
private int y;
public static final int UP = 0, DOWN = 1, RIGHT = 2, LEFT = 3;

public ball(Bitmap bitmap, int x, int y) {
this.bitmap = bitmap;
this.x = x;
this.y = y;
}

public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}

public void draw(Canvas canvas) {
canvas.drawBitmap(bitmap, x – (bitmap.getWidth() / 2), y – (bitmap.getHeight() / 2), null);
}

public boolean move(int direction) {
boolean moved = false;
if(direction == UP) {
y -= 10;
moved = true;
}
if(direction == DOWN) {
y += 10;
moved = true;
}
if(direction == RIGHT) {
x += 10;
moved = true;
}
if(direction == LEFT) {
x -= 10;
moved = true;
}
return moved;
}

}

[/xml]

8. Run your application and check the movement of ball by pressing keys. Your app will look like this :

ball_movement_on_key

Tags: , , , , , , , , , , ,