Android 2D terrain scrolling
- by Nikola Ninkovic
I want to make infinite 2D terrain based on my algorithm.Then I want to move it along Y axis (to the left)
This is how I did it :
public class Terrain {
Queue<Integer> _bottom;
Paint _paint;
Bitmap _texture;
Point _screen;
int _numberOfColumns = 100;
int _columnWidth = 20;
public Terrain(int screenWidth, int screenHeight, Bitmap texture)
{
_bottom = new LinkedList<Integer>();
_screen = new Point(screenWidth, screenHeight);
_numberOfColumns = screenWidth / 6;
_columnWidth = screenWidth / _numberOfColumns;
for(int i=0;i<=_numberOfColumns;i++)
{
// Generate terrain point and put it into _bottom queue
}
_paint = new Paint();
_paint.setStyle(Paint.Style.FILL);
_paint.setShader(new BitmapShader(texture, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT));
}
public void update()
{
_bottom.remove();
// Algorithm calculates next point
_bottom.add(nextPoint);
}
public void draw(Canvas canvas)
{
Iterator<Integer> i = _bottom.iterator();
int counter = 0;
Path path = new Path();
path.moveTo(0, _screen.y);
while (i.hasNext())
{
path.lineTo(counter, _screen.y-i.next());
counter += _columnWidth;
}
path.lineTo(_screen.x, _screen.y);
path.lineTo(0, _screen.y);
canvas.drawPath(path2, _paint);
}
}
The problem is that the game is too 'fast', so I tried with pausing thread
with
Thread.sleep(50);
in run() method of my game thread but then it looks too torn.
Well, is there any way to slow down drawing of my terrain ?