Coordinate Movement for a Ball

January 2nd, 2012 by aabhaanjan

Step 1 : Create a new project using Motodev Studio.
1
Step 2: Put an image of ball named “ball_1.png” into res/drwable Folder.

Step 3: Open you MainActivity.java and put following code :

[xml]
package com.movement;

import android.app.Activity;

import android.os.Bundle;

import android.view.Window;

public class MainActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new GameController(this));

}

@Override

protected void onDestroy() {
super.onDestroy();

}

@Override

protected void onStop() {
super.onStop();

}

}

[/xml]

Step 3: Create a class Ball.java and put following code :

[xml]

package com.movement;

import android.graphics.Bitmap;

import android.graphics.Canvas;

public class Ball {

private Bitmap bitmap;

private int x;
private int y;
private SpeedManager speed;

public Ball(Bitmap bitmap, int x, int y) {

this.bitmap = bitmap;

this.x = x;

this.y = y;

this.speed = new SpeedManager();

}

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 SpeedManager getSpeed() {

return speed;

}

public void setSpeed(SpeedManager speed) {

this.speed = speed;

}

public void draw(Canvas canvas) {

canvas.drawBitmap(bitmap, x – (bitmap.getWidth() / 2), y – (bitmap.getHeight() / 2), null);

}

public void update() {

x += (speed.getvelocity() * speed.getxDirection());
}

}

[/xml]
Step 4: Create a class GameController.java and put following code :
[xml]
package com.movement;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameController extends SurfaceView implements
SurfaceHolder.Callback {
private GameThread thread;
private Ball ballInstance;

public GameController(Context context) {
super(context);
getHolder().addCallback(this);
ballInstance = new Ball(BitmapFactory.decodeResource(getResources(), R.drawable.ball_1), 80, 80);
thread = new GameThread(getHolder(), this);
setFocusable(true);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
thread.setRunning(true);
thread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {

}
}

}

public void render(Canvas canvas) {
canvas.drawColor(Color.BLACK);
ballInstance.draw(canvas);
}

public void update() {
if (ballInstance.getSpeed().getxDirection() == SpeedManager.DIRECTION_RIGHT
&& ballInstance.getX() + ballInstance.getBitmap().getWidth() / 2 >= getWidth()) {
ballInstance.getSpeed().toggleXDirection();
}

if (ballInstance.getSpeed().getxDirection() == SpeedManager.DIRECTION_LEFT
&& ballInstance.getX() – ballInstance.getBitmap().getWidth() / 2 <= 0) { ballInstance.getSpeed().toggleXDirection(); } ballInstance.update(); } } [/xml] Step 5: Create a thread GameThread.java and put following code : [xml] package com.movement; import android.graphics.Canvas; import android.view.SurfaceHolder; public class GameThread extends Thread { private SurfaceHolder surfaceHolder; private GameController gameController; private boolean running; public void setRunning(boolean running) { this.running = running; } public GameThread(SurfaceHolder surfaceHolder, GameController gameController) { super(); this.surfaceHolder = surfaceHolder; this.gameController = gameController; } @Override public void run() { Canvas canvas; while (running) { canvas = null; try { canvas = this.surfaceHolder.lockCanvas(); synchronized (surfaceHolder) { this.gameController.update(); this.gameController.render(canvas); } } finally { if (canvas != null) { surfaceHolder.unlockCanvasAndPost(canvas); } } } } } [/xml] Step 6: Create a thread SpeedManager.java and put following code : [xml] package com.movement; public class SpeedManager { public static final int DIRECTION_RIGHT = 1; public static final int DIRECTION_LEFT = -1; private float velocity = 10; private int xDirection = DIRECTION_RIGHT; public SpeedManager() { this.velocity = 1; } public SpeedManager(float velocity, float yv) { this.velocity = velocity; } public float getvelocity() { return velocity; } public void setvelocity(float velocity) { this.velocity = velocity; } public int getxDirection() { return xDirection; } public void setxDirection(int xDirection) { this.xDirection = xDirection; } public void toggleXDirection() { xDirection = xDirection * -1; } } [/xml] Step 7:Run your application It will look like this : 2