0

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);

    }
}
  • In the meanwhile I observed that reduction of bitmap's velocity occurs ONLY when it is launched from a position to the right of left screen edge, e.g. in the middle: treeX = screenWidth/2. When the bitmap hits the left wall after some time, its veocity reduces. – Kolorowy Paw Jul 07 '19 at 15:15
  • However when the bitmap is originially launched from left edge or to the left of it, that is: treeX <= 0, then it will obviosuly not hit the left screen edge. Therefore, its velocity will not be reduced. – Kolorowy Paw Jul 07 '19 at 15:22

0 Answers0