这里是扩展表面视图的类,在它的run方法中我称之为动画球的方法,但是球不是动画
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);
这里我称之为方法
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();
}
这里是定义球的类
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);
}
}
帮助我这是我的第一款安卓游戏
答案 0 :(得分:1)
不判断其余代码:
ballx = canvas.getWidth()/2;
bally = canvas.getHeight()/2;
每次绘制时,都会重置球的位置。
当你重置它的位置时,难怪它不动
请改为调用update()
,以便移动球。