here is the class which extends surface view , in its run method i've called the method to animate ball but the ball isnt animating
public class OurView extends SurfaceView implements Runnable
{
Canvas c;
Thread t = null;
SurfaceHolder holder;
boolean isItOk = false;
Rect racketTop,racketBottom;
int TopLeft ;
int TopRight ;
int TopTop ;
int TopBottom;
int bottomLeft;
int bottomRight;
int bottomTop;
int bottomBottom;
GameState state;
public OurView(Context context) {
super(context);
holder = getHolder();
state = new GameState();
}
@Override
public void run() {
while (isItOk == true) {
if (!holder.getSurface().isValid())
continue;
c = holder.lockCanvas();
c.drawARGB(0, 0, 0, 0);
TopLeft = ((c.getWidth() / 2) / 2) + 50;
TopRight = (c.getWidth() / 2) + ((c.getWidth() / 2) / 2) - 50;
TopTop = getTop();
TopBottom = 10;
bottomLeft = ((c.getWidth() / 2) / 2) + 50;
bottomRight = (c.getWidth() / 2) + ((c.getWidth() / 2) / 2) - 50;
bottomTop = getBottom() - 10;
bottomBottom = getBottom();
racketTop = new Rect(); // for top racket
racketTop.set(TopLeft, TopTop, TopRight, TopBottom); //left = ((c.getWidth()/2)/2)+50
//top = getTop()
//right = (c.getWidth()/2)+((c.getWidth()/2)/2)-50
//bottom = 10
racketBottom = new Rect(); // FOR BOTTOM RACKET
racketBottom.set(bottomLeft, bottomTop, bottomRight, bottomBottom); //left = ((c.getWidth()/2)/2)+50
// top = getBottom()-10
// right = (c.getWidth()/2)+((c.getWidth()/2)/2)-50
// bottom = getBottom()
Paint white = new Paint();
white.setColor(Color.WHITE);
white.setStyle(Paint.Style.FILL);
c.drawRect(racketTop, white);
c.drawRect(racketBottom, white);
here i called the method
state.update();
state.draw(c,white);
holder.unlockCanvasAndPost(c);
}
}
public void pause()
{
isItOk = false;
while(true)
{
try
{
t.join();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
break;
}
t = null;
}
public void resume()
{
isItOk = true;
t = new Thread(this);
t.start();
}
here is the class where ball is defined
public class GameState {
int ballx,bally; //ball x and y position
int ball_size = 20; // size of ball
static int ballvelx = 3; // velocity x of ball
static int ballvely = 3; // velocity y of ball
public GameState() {
}
public void update()
{
ballx += ballvelx;
bally += ballvely;
}
public void draw(Canvas canvas, Paint paint)
{
//objects color
paint.setColor(Color.WHITE);
ballx = canvas.getWidth()/2;
bally = canvas.getHeight()/2;
//ball
canvas.drawCircle(ballx,bally,ball_size,paint);
}
}
help me this is my first android game
Without judging on the rest of your code:
ballx = canvas.getWidth()/2;
bally = canvas.getHeight()/2;
This resets the position of your ball every time you draw it.
No wonder it does not move, when you reset its position.
Call update()
instead, so the ball is moved.