Skip navigation
faKastner
Currently Being Moderated

Performance slowdown when displaying many small images

Aug 11, 2012 2:30 AM

Tags: #flex #images #flex-4.6 #air3 #actionscript_3.0

Hey!

 

I'm working on a small Flex Mobile game. It uses a 2D game grid and I am trying to display tiles in the cells. There is 3 different images being used: One for Walls, one for floor and one for the character.

 

I display these images in the following way:

I have a group with a id ( displayGroup ), I then create a array of new Image() with the position they should be placed at and the image they should use.

I then loop over the array and add the images individually as a element into the group.

 

In code, it looks like this:

 

var temparray:Array = disp.drawMapGfx(map, hero)
for each (var img:Image in temparray) 
{
    displayGroup.addElement(img);
}

 

Now, the problem appears when I move.

What I currently do is the following:

 

var numImages:int = displayGroup.numElements-1;

var temparray:Array = disp.drawMapGfx(map, hero)
for each (var img:Image in temparray) 
{
    displayGroup.addElement(img);
}


for(var i:int = numImages; i >= 1; i--)
{
    displayGroup.removeElementAt(i);
}

 

I first grab the total number of images in the group. Then I add the updated images that take the new position into account, and then I remove all the outdated images from the group. I go to 1 instead of 0 because element 1 is a rect that creates the background color.

 

For testing, I've been using "Run as iPhone 3 GS". Every image is 50x30, which, taking the GUI into consideration equates to 11 x 17 images + 1 for the character. So it is displaying up to a maximum of 188 images.

 

Picture for reference:

mobile_imageproblem.png

The problem is that the images flicker as the old images disappear and the new images appear. What can I do to have it redraw the screen faster, to prevent any flickering ?

Would it make sense to, instead of redrawing every image, to simply shift the whole scene and just load / remove images where necessary, rather than reloading all images ?

 

And a small second question: Is it possible to share a mobile app in a single desktop-ready AIR file, so that I can have friends who do not have smartphones help test on their computer ?

 
Replies
  • Currently Being Moderated
    Aug 13, 2012 12:33 AM   in reply to faKastner

    How big is the world, in total?

    Theoretically, you could load the whole world on startup, and use a mask to display only the relevant part of the world. With each move of the character, you move the mask to the next section.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 13, 2012 3:00 AM   in reply to faKastner

    Embedding the images will probably improve the load time (and improve the memory usage as well).

     

    By masking, you 'mask' a part of the image. In other words, you only display a specific rectange of the image. You can think of it as a viewport; only the part that is masked will be visible.

     

    Example code:

     

    <fx:Script>

    public function move():void {

         moveCanvas.xTo = 100;

         moveCanvas.play();

    }

    </fx:Script>

     

    <fx:Declarations>

            <s:Move id="moveCanvas" target="{panel1}" duration="500" />

        </fx:Declarations>

     

        <!-- Surrounding group necessary, since panel1 shouldn't have an HorizontalLayout as parent -->

        <s:Group id="surroundingGroup" width="100" right="0">

     

            <s:Group id="mask" top="0" right="0" bottom="0" left="0">

                <s:Rect width="100" height="100">

                    <s:fill>

                        <s:SolidColor color="0xFF0000" />

                    </s:fill>

                </s:Rect>

            </s:Group>

     

            <s:Group id="panel1" top="5" width="100" mask="{mask}" maskType="clip">

                <CellContainingImage width="100" />

                <CellContainingImage width="100" left="100" />

            </s:Group>

    </s:Group>

     

    Panel1 is the total world view, containing all cells. By calling the move-method, you move this world to the left. The viewport (mask) stays in place, but the world behind this viewport moves to the left. This way, you would see the cell to the right of the current cell.

    I hope this makes sense.. Let me know if there are any issues.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 14, 2012 2:46 PM   in reply to faKastner
     
    |
    Mark as:
  • Currently Being Moderated
    Aug 17, 2012 12:54 AM   in reply to faKastner

    Glad I could help.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 10, 2012 2:42 AM   in reply to faKastner

    Apologies for the late response, I was on holiday.

     

    Is the problem already solved, or are you still stuck? I don't have a problem at hand, but I can give it some thought if it's still relevant.

     

    As for the second question, I encounter the same warnings, but have never acted on it. I don't think it is related to the first question, or is reponsible for any error at all.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points