I'm just trying to find the best approach to run scolling background on an Android device. The method that I have so far ... its pretty laggy. I use streams, which, in my opinion, are not the best choice for Android platforms.
@Override public void run() { // Game Loop while(runningThread){ //Scroll background down bgY += 1; try { this.postInvalidate(); t.sleep(10); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
where postinvalidate in onDraw function just pushes the background image down
canvas.drawBitmap(backgroundImage, bgX, bgY, null);
Thank you in advance
UPDATE
I have identified the problem. And it's a fact that my player updates at the same speed as background scrolls (which makes it look volatile). top down. This is due to the fact that both are obtained in the same function. I'm not quite sure how to handle this and will be grateful for any help. so the playerβs movement is handled separately from the map scroll
Also, how can I control the speed at which onDraw (canvas) is called?
Thanks in advance.
However, I fixed another loop cycle for those who have the same problem. This is partly from the google jetboy example.
Below is my inner class in my surface view
class MapThread extends Thread{ private Map map; private SurfaceHolder holder; private boolean run = false; public MapThread(Map map, SurfaceHolder holder){ this.holder = holder; this.map = map; setRunning(true); } public void setRunning(boolean run){ this.run = run; } @Override public void run(){ while(run){ try{ Canvas c = null; try { c = holder.lockCanvas(null); synchronized (holder) { map.onDraw(c); } } finally { if (c != null) { holder.unlockCanvasAndPost(c); } } } } }
Decision
https://gamedev.stackexchange.com/questions/8127/android-game-scrolling-background
source share