这是我的第一个问题,我会尽量说清楚。如果之前有人问过类似的问题,请原谅我(我检查过但一无所获)。
我正在尝试在 java 中创建我的第一个 android 应用程序。这是一个小游戏,屏幕左侧有一个角色(横向模式),背景从右到左,给人以运动的印象。背景的速度是恒定的。我使用了一个如下所示的背景类:
public class Background {
int x=0, y=0;
Bitmap background;
Background(int screenX, int screenY, Resources res){
background = BitmapFactory.decodeResource(res, R.drawable.background);
background = Bitmap.createScaledBitmap(background, screenX, screenY, false);
}
}
然后在 GameView 类中创建 2 个背景对象
private Background background1, background2;
在构造函数中:
background1 = new Background(screenX, screenY, getResources());
background2 = new Background(screenX, screenY, getResources());
//both backgrounds are the same picture
background2.x = screenX;
paint= new Paint();
最后:
private void update(){
background1.x -=2 * screenRatioX;
background2.x -=2 * screenRatioX;
//both have the same speed
if (background1.x + background1.background.getWidth()<0){
background1.x = screenX;
// to make it reappear on the right side when it vanishes on the left side
}
if (background2.x + background2.background.getWidth()<0){
background2.x = screenX;
}
}
我希望这很清楚,并且有人能够帮助我。