Scrolling in tower defence game causes delay

Problems with Canvas, OpenGL, etc...

Scrolling in tower defence game causes delay

Postby teano » Mon Jan 03, 2011 4:34 pm

i started trying to develop a tower defence game for android. Unfortunately i don't have so much experience with java. I already got a map you can scroll and cells that can contain towers (drawn as circles). But when scrolling over the map with some towers there is a delay between towers an background and the towers seem to move. To scroll over the background i show a part of a bitmap that is resized depending on the Scrolloffset.
Code: Select all
    public void onDraw(Canvas canvas) {   
       Bitmap resizedBackground = Bitmap.createBitmap(background,XOffset, YOffset, getWidth(), getHeight());
       canvas.drawBitmap(resizedBackground, 0, 0, null);
       //draw grid
      int actViewRow = 0;
      int actViewCell;
      int X;
      int Y;      
      for(int i = startRow; i < maxRow; i++){
         actViewCell = 0;
         Row = MapCells.get(i);
         //calculate Coordinates on current View
         Y = actViewRow * CellSize - YScrollOffset;
            for(int j = startCell; j<maxCell ;j++){
               X = actViewCell * CellSize - XScrollOffset;
               Row.get(j).draw(canvas, Paint, X, Y, showGrid);

My Questions are:
1. is there a way to draw the background and the towers at the same time to avoid the delay
or 2. is there a delay because of resizing the image all the time, how could i do better
3. would the app perform better if i just draw an bitmap for each cell of the map and change the bitmap if a tower is set on the cell

Thanks for your help.

PS. I hope you can understand me. I did not write any line of javacode for the last 2 years and any line of english sentences for 4 years :oops: Now its time to practise both.
Once Poster
Once Poster
Posts: 1
Joined: Mon Jan 03, 2011 3:37 pm


Re: Scrolling in tower defence game causes delay

Postby cloudjubei » Wed Feb 23, 2011 6:29 pm

I also started developing a TD for android.
Now I've been doing it with the Canvas for a month, and when I finally got all the towers and bullets and monsters running and so on it started to become slow. By slow I mean max of 16 fps. This is an unacceptable speed for a game, so now I'm trying to swap to OpenGL which I would recommend for you to do, as I've heavily optimized my game to make it run as quick as possible (as little time for game logic, so there's a lot for graphics drawing), but Canvas simply isn't enough if you plan to have more than a 100 sprites (which could hypothetically happen having a lot of bullets flying around, with all the monsters on the grid as well as the towers).
So I'd suggest to swap to OpenGL.
I'm not answering your question, which I will now, in case you just want to make this work:

So, first of all try to have as little sprites as possible. Don't make a grid unless it's strictly necessary, have one bitmap for the background (composed of all the tiles) and store it somewhere so you can call draw on it every frame.
There is a delay because of resizing, that's why it'd be best if you had a place where you store the static images for all towers/monsters/bullets which are already resized, and that all those guys reuse, so you don't have to resize any more.
I think, that if you have one huge bitmap for the background and then a cell for a tower, then it won't matter much. If you plan to have a grid, then yes it will perform better but negligibly, as it's already slow.

Hope I helped, and I know it's hard making all this stuff get to work as I've spend the last 2 months trying to make mine work, so full encouragement on my side :)

PS. Your english is very good, keep at it! :)
Posts: 8
Joined: Wed Feb 23, 2011 3:50 pm


Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 3 guests