I encountered an unexplainable problem. This might contitute a flaw in the android studio.
Mainly: I am developing a game, where the background (grass or trees) moves in the opposite direction to the player, pretty simple. Both of them (grass and player) is a bitmap in a GameView class which extends View class. Player is stationary, while grass is being moved to the left.
The problem begins when I want the grass to move at a VARYING speed. VARYING is the keyword here. What happens is that when the grass bitmap reaches the left screen edge, its velocity reduces for no reason ! It does not happen when the grass speed is constant.
I have no idea how to overcome this problem. Basically my goal is to enable: player goes faster = grass moves faster. I would like you to solve the above issue or propose alternative solutions.
I am not showing any code governing the player, to make it more clear where the problem is. The problem is entirely represented by the below code chunk:
public class GameView extends View
{
Bitmap trees;
int treeY, treeX, U; //position and velocity
int treeWidth, screenWidth, screenHeight;
Point point;
public GameView(Context context, AttributeSet atr)
{
super(context, atr);
trees = new Bitmap();
trees = BitmapFactory.decodeResource(getResources(), R.drawable.t1);
treeWidth = trees.getWidth();
U = 0;
point = new Point();
screenHeight = point.y;
screenWidth = point.x;
treeY = 8*screeHeight/10
treeX = screenWidth;
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
U += 1/8000;
treeX -= U/2;
if(treeX < -treeW)
{
treeX = screenWidth;
}
// just looping the trees Bitmap
canvas.drawBitmap(trees, treeX, treeY, null);
}
}