如何解决如何使物体在触摸时漂浮?
我正在尝试在 android 中制作这个游戏,其中警察应该没有重力并因此坠落,直到用户每次点击屏幕时它都会向上移动,类似于飞扬的小鸟。
这是警察如何静止在底部并且触摸屏幕没有任何反应的屏幕截图。
click on me to find the screenshot of the gamescreen
这是相同的代码: 这是我的看法
public class PoliceView extends View {
private Bitmap police[] = new Bitmap[2];
private int policeX = 10;
private int policeY;
private int policeSpeed;
private int canvasWidth,canvasHeight;
private boolean touch = false;
private Bitmap background;
private Paint scorePaint = new Paint();
private Bitmap life[] = new Bitmap[2];
public PoliceView(Context context) {
super(context);
police[0] = BitmapFactory.decodeResource(getResources(),R.drawable.police);
police[1] = BitmapFactory.decodeResource(getResources(),R.drawable.police);
background = BitmapFactory.decodeResource(getResources(),R.drawable.gamebackground);
scorePaint.setColor(Color.BLACK);
scorePaint.setTextSize(70);
scorePaint.setTypeface(Typeface.DEFAULT_BOLD);
scorePaint.setAntiAlias(true);
life[0] = BitmapFactory.decodeResource(getResources(),R.drawable.heart);
life[1] = BitmapFactory.decodeResource(getResources(),R.drawable.brokenheart);
policeY = 550;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvasWidth = canvas.getWidth();
canvasHeight = canvas.getHeight();
//The order of draw matters as objects are drawn in this same order
canvas.drawBitmap(background,-90,null);
int minPoliceY = police[0].getHeight();
int maxPoliceY = canvasHeight - police[0].getHeight() * 3;
policeY = policeY + policeSpeed;
if(policeY < minPoliceY){
policeY = minPoliceY;
}
if(policeY > minPoliceY){
policeY = maxPoliceY;
}
policeSpeed = policeSpeed + 2;
if(touch){
canvas.drawBitmap(police[1],policeX,policeY,null);
touch = false;
}
else{
canvas.drawBitmap(police[0],null);
}
canvas.drawText("score:",40,80,scorePaint);
//Three lives for the police
canvas.drawBitmap(life[0],100,null);
canvas.drawBitmap(life[0],160,280,null);
@Override
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN){
touch = true;
policeSpeed = -22;
}
return true;
}}
这是我主要活动的代码:
public class GameScreen extends AppCompatActivity {
private PoliceView gameView;
private Handler handler = new Handler();
private final static long interval = 30;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameView = new PoliceView(this);
setContentView(gameView);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
gameView.invalidate();
}
});
}
},interval);
}}
我基本上需要帮助,让警察在每次用户点击屏幕时向上移动,如果他不点击类似于飞扬的小鸟,就会向下坠落。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。