我还是相当新的android,我正在尝试创建pong。
目前我正在尝试为两个图像制作动画,一个是乒乓球,第二个是划桨。现在我有这两个图像,我已经让它们动画了。球的行为就像我希望它围绕x和y轴反弹而我让桨在x轴上来回滑动但是由于某种原因,桨表现得非常奇怪。
桨由于某种原因动画不稳定,停止并随时启动。但每当我从代码中移除球动画时,桨的动作和行为就像我想要的那样
如果有人对我做错或忽视的事情有任何建议,我会全力以赴,谢谢!
package com.example.pongtest;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.app.Activity;
public class MainActivity extends Activity {
LinearLayout lay1;
ImageView ballImg, paddleImg;
TimerTask thisTimerTask;
Timer thisTimer;
LinearLayout.LayoutParams params;
float ballPosX=10;
float ballPosY=10;
float paddlePosX=200;
float paddlePosY=10;
float ballOnLeftWall=0;
float ballOnRightWall=270;
float ballOnBottomWall=400;
float ballOnTopWall=0;
float paddleOnLeftWall=0;
float paddleOnRightWall=200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lay1 = new LinearLayout(this);
ballImg = new ImageView(this);
ballImg.setBackgroundResource(R.drawable.circlewdot);
setPosition(0,0,50,50);
ballImg.setLayoutParams(params);
paddleImg=new ImageView(this);
paddleImg.setBackgroundResource(R.drawable.paddle);
setPosition(0,0,50,10);
paddleImg.setLayoutParams(params);
lay1.addView(ballImg);
lay1.addView(paddleImg);
setContentView(lay1);
thisTimerTask = new ThisTimerClass();
thisTimer = new Timer();
//setup frame rate of timer
thisTimer.scheduleAtFixedRate(thisTimerTask, 2000, 16);
}
public void setPosition(float x, float y, float width, float height) {
params = new LinearLayout.LayoutParams(0,0);
params.topMargin = (int)y;
params.leftMargin = (int)x;
params.width = (int)width;
params.height = (int)height;
}
class ThisTimerClass extends TimerTask {
boolean directionOfBall_isRight = true;
boolean directionOfBall_isDown = true;
boolean directionOfPaddle_isLeft = true;
@Override
public void run() {
// Will be called each time the timer is fired.
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
setPosition(ballPosX,ballPosY,50,50);
ballImg.setLayoutParams(params);
setPosition(paddlePosX,paddlePosY,50,10);
paddleImg.setLayoutParams(params);
//animates ball across the screen
if (directionOfBall_isRight == true)
{ballPosX = (ballPosX + 1);}
else if (directionOfBall_isRight == false)
{ballPosX = (ballPosX - 1);}
if (directionOfBall_isDown == true)
{ballPosY = (ballPosY + 1);}
else if (directionOfBall_isDown == false)
{ballPosY = (ballPosY - 1);}
//redirects ball
if (ballPosX > ballOnRightWall)
{directionOfBall_isRight = false;}
else if (ballPosX < ballOnLeftWall)
{directionOfBall_isRight = true;}
if (ballPosY > ballOnBottomWall)
{directionOfBall_isDown = false;}
else if (ballPosY < ballOnTopWall)
{directionOfBall_isDown = true;}
//animates paddle across the screen
if (directionOfPaddle_isLeft == true)
{paddlePosX = (paddlePosX - 1);}
else if (directionOfPaddle_isLeft == false)
{paddlePosX = (paddlePosX + 1);}
//redirects paddle
if (paddlePosX < paddleOnLeftWall)
{directionOfPaddle_isLeft = false;}
else if (paddlePosX > paddleOnRightWall)
{directionOfPaddle_isLeft = true;}
}
});
}
}
}
答案 0 :(得分:0)
这可能是因为你在同一个线程上控制球和球拍。系统不能同时从单个线程做两件事!