سؤال

I have an android game where a ball moves based on the accelerometer. The ball moves at a variable speed. The game has levels much larger than a smartphone screen, and so I translate the canvas as the ball nears the edge of the screen to accomodate the large levels. The scrolling works, the trouble is smoothly scrolling based on the ball.

Here's an attempt that shifts based on the change of position of the ball between updates:


// RIGHT SHIFT
shiftFactor = Math.abs(xCoor - prevXCoor);
if (ball.x + translateX &gt rightX * 0.8
 && rightX &lt levelWidth + padding) {
    translateX -= shiftFactor;
    leftX += shiftFactor;
    rightX += shiftFactor;
} 
It looks jittery.

Here's an attempt that shifts based on some constant.


int shiftFactor=2;

//SHIFT SCREEN RIGHT
if (ball.x + translateX > RightPerimeter * 0.8 && rightPerimeter < levelWidth) {
  translateX -= shiftFactor;
  viewPortLeft += shiftFactor;
  viewPortRight += shiftFactor;
}

For lower shiftFactor values, it smoothly scrolls, but the ball can easily 'outrun' the scrolling. For larger shiftFactors, the scrolling gets incrementally choppier.

Any suggestions? Thanks.

هل كانت مفيدة؟

المحلول

Your problem is that your frame rate is too low. In my experience Canvas isn't well-equipped to do real-time games at a good frame rate. You'll see a big performance gain if you switch to rendering with OpenGL.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top