Android Game Player

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

+4
source share
3 answers

Use SurfaceView in the picture. This allows you more control over what to draw and when.

SurfaceView is a special subclass of View that offers a selected drawing surface in the View hierarchy. The goal is to offer this drawing surface to the second thread of the application so that the application does not need to wait until the system view hierarchy is ready to draw.

The main project is to have an idea of ​​the surface, which is continuously drawn in a while loop. Then add an if-statement whose condition must be true if the timer thread tells you the time to draw. Say every 30 ms a bitmap is drawn. This will give you about 33 frames per second.

Now you can also have another timer thread that tells you when to update the values ​​of bgX or bgY. Say every 60 ms, it will set the logical updateFlag = true; Then in your main thread you have if-statement, check this flag, set it to false and update the values ​​of bgX and bgY. By precisely controlling the timer and the bgX / bgY increments, you should be able to create smooth animations.

It would be nice to see the LunarLander source code provided by Google.

+3
source

It should be borne in mind that sleep is very inaccurate. To get around this, you can keep track of how much time has passed during sleep and update how much you move things accordingly.

It is not clear from your code, but you need to make sure that all UI updates happen in the UI thread.

You need to do your time outside the user interface thread, because otherwise the user interface will never be updated. There are other synchronization methods, for example, using Handler , which can be a little cleaner, but I think that the overhead of them may be a little for what you are trying to do. I think a simple thread has the least amount of overhead.

0
source

I use this method in the second level of my SpaceQuestAlpha game. This makes a useless scroll.

I used 2 lines below to set the starting position.

  moony=0; moon2y=-(heighty); 

These lines then enlarge both versions of the background image. One starts at 0 and one starts at a negative screen height. Each time one of the images goes below the bottom of the screen, it moves up twice in height to move it back to the desired position. I use a surface view without delay issues.

  moony+=5; moon2y+=5; if(moon2y>=heighty) {moon2y=moon2y-(heighty*2);} canvas.drawBitmap(lavabackground, 0, moon2y, null); if(moony>=heighty){moony=moony-(heighty*2);} canvas.drawBitmap(lavabackground, 0, moony, null); 
0
source

Source: https://habr.com/ru/post/1338464/


All Articles