Tile-based problem!

I’m having a big problem with my tile-based game. I create tiles based on a 2 dimensional array. Each tile is a Movie Clip, created using attachMovie. The problem is that when there are many of these tiles, the game lags, because so many properties must be stored for each MC. Is there a better method for creating “tiles” than attachMovie, or a way to free the memory used for each one?

PS. First post!

[quote=Ph03nix;2332769]I’m having a big problem with my tile-based game. I create tiles based on a 2 dimensional array. Each tile is a Movie Clip, created using attachMovie. The problem is that when there are many of these tiles, the game lags, because so many properties must be stored for each MC. Is there a better method for creating “tiles” than attachMovie, or a way to free the memory used for each one?

PS. First post![/quote]
Yes its called tile scrolling, as in only loading the tiles that are going to be visible on one screen and everything beyond what isn’t visible to the use isn’t loaded.
That way even with a large map the game wont lag like map when you scroll it because only the portion that is visible to the user is loaded.

[quote=Gathan;2332772]Yes its called tile scrolling, as in only loading the tiles that are going to be visible on one screen and everything beyond what isn’t visible to the use isn’t loaded.
That way even with a large map the game wont lag like map when you scroll it because only the portion that is visible to the user is loaded.[/quote]

I currently do something like that. The off-screen tiles’ “_visible” property is set to false. Is this the wrong way to do it?

Yes visible or not the movieclips are still there and will take up the same ram,cpu usage no matter what the visibility is set to.

You’re right. Can you explain the method you mentioned, tile scrolling? I’m not familiar with it.

First you make a grid of movieclips on the screen, the amount is going to depend on your screen size and space them apart from each other with the width and height of the tiles so they don’t overlap.
Then as you move to the right for example and it starts to scroll you see if the the top right or top left tiles for the grid is greater or less than a specific value depending on which one you use and if if it is you move the row of tiles that is to left that is going to be out of view at this point to the right most side of the tiles which is going to be the tiles that are going to come into view as you move and load the tiles into those movieclips for them.
So basically you recycle all of the movieclips that you loaded to begin with, moving them around as needed and replacing the the contains of whats inside with the new tiles as you scroll.

[quote=Gathan;2332792]First you make a grid of movieclips on the screen, the amount is going to depend on your screen size and space them apart from each other with the width and height of the tiles so they don’t overlap.
Then as you move to the right for example and it starts to scroll you see if the the top right or top left tiles for the grid is greater or less than a specific value depending on which one you use and if if it is you move the row of tiles that is to left that is going to be out of view at this point to the right most side of the tiles which is going to be the tiles that are going to come into view as you move and load the tiles into those movieclips for them.
So basically you recycle all of the movieclips that you loaded to begin with, moving them around as needed and replacing the the contains of whats inside with the new tiles as you scroll.[/quote]

I understand this vaguely, but how should I modify my current tile creation code?

code:

size=16;
num=0;
for (i=0; i<size; i++) {
    for (j=0; j<100; j++) {
        if (_root.map*[j] != 0) {
            if (_root.map*[j]>0 && _root.map*[j]<10) {
                _root.attachMovie("tile", "tile_"+num, DepthManager.kBottom+num);
                _root["tile_"+num]._x = j*size;
                _root["tile_"+num]._y = i*size;
                _root["tile_"+num].gotoAndStop(_root.map*[j]);
                _root["tile_"+num].stop();
                num++;
            }
        }
    }
}

*note: _root.map is the 2 dimensional array
**note: i<size and j<100 are temporary values (i’ll change them based on the # of rows and columns in my array)

For the initial tile creation your not going to be using the two dimensional array or anything at all just create the empty tiles that are going to be used.
Now for the loop its going to be roughly
[LEFT][COLOR=#0000ff]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000]var[/COLOR] x = [COLOR=#000080]0[/COLOR]; x<Math.roundCOLOR=#000000+1[/COLOR]; x++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000ff]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000]var[/COLOR] y = [COLOR=#000080]0[/COLOR]; y<Math.roundCOLOR=#000000[/COLOR]+1; y++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000ff]attachMovie[/COLOR][COLOR=#000000]([/COLOR][COLOR=#ff0000]“tile”[/COLOR]+x+[COLOR=#ff0000]""[/COLOR]+y, DepthManager.[COLOR=#000080]kBottom[/COLOR]+num[COLOR=#000000])[/COLOR];
[COLOR=#0000ff]this[/COLOR][COLOR=#000000][[/COLOR][COLOR=#ff0000]“tile”[/COLOR]+x+[COLOR=#ff0000]"
"[/COLOR]+y[COLOR=#000000]][/COLOR].[COLOR=#0000ff]x[/COLOR] = x*tilewidth;
[COLOR=#0000ff]this[/COLOR][COLOR=#000000][[/COLOR][COLOR=#ff0000]“tile”[/COLOR]+x+[COLOR=#ff0000]"
"[/COLOR]+y[COLOR=#000000]][/COLOR].[COLOR=#0000ff]_y[/COLOR] = y*tileheight;
[COLOR=#000000]}[/COLOR]
[COLOR=#000000]}[/COLOR]
[/LEFT]

[quote=Gathan;2332799]For the initial tile creation your not going to be using the two dimensional array or anything at all just create the empty tiles that are going to be used.
Now for the loop its going to be roughly
[LEFT][COLOR=#0000ff]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000]var[/COLOR] x = [COLOR=#000080]0[/COLOR]; x<Math.roundCOLOR=#000000+1[/COLOR]; x++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000ff]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000]var[/COLOR] y = [COLOR=#000080]0[/COLOR]; y<Math.roundCOLOR=#000000[/COLOR]+1; y++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000ff]attachMovie[/COLOR][COLOR=#000000]([/COLOR][COLOR=#ff0000]“tile”[/COLOR]+x+[COLOR=#ff0000]""[/COLOR]+y, DepthManager.[COLOR=#000080]kBottom[/COLOR]+num[COLOR=#000000])[/COLOR];
[COLOR=#0000ff]this[/COLOR][COLOR=#000000][[/COLOR][COLOR=#ff0000]“tile”[/COLOR]+x+[COLOR=#ff0000]"
"[/COLOR]+y[COLOR=#000000]][/COLOR].[COLOR=#0000ff]x[/COLOR] = x*tilewidth;
[COLOR=#0000ff]this[/COLOR][COLOR=#000000][[/COLOR][COLOR=#ff0000]“tile”[/COLOR]+x+[COLOR=#ff0000]"
"[/COLOR]+y[COLOR=#000000]][/COLOR].[COLOR=#0000ff]_y[/COLOR] = y*tileheight;
[COLOR=#000000]}[/COLOR]
[COLOR=#000000]}[/COLOR][/LEFT]
[/quote]

I really hate to ask this, but I haven’t every done anything like this before, so how should change the frame of each tile and their positions when they get off screen?

*note: I multiplied the _root’s _xscale and _yscale by 2.

oh, and thank you so much for the help so far!

[quote=Ph03nix;2333225]I really hate to ask this, but I haven’t every done anything like this before, so how should change the frame of each tile and their positions when they get off screen?

*note: I multiplied the _root’s _xscale and _yscale by 2.

oh, and thank you so much for the help so far![/quote]

ok never mind I worked it out myself, however, it LAGS! this code is included in an onClipEvent(enterFrame)… am I doing anything wrong?

*note: _root._x and Stage.width are divided by 2 because i initially mulitiply them by 2. and size is 16

for (i=0; i<=((Stage.width/2)/size+1)*((Stage.height/2)/size+1); i++) {
if (root["tile"+i]._x<-_root._x/2-size) {
root["tile"+i]._x += Stage.width/2+size;
}
if (root["tile"+i]._x>-_root._x/2+(Stage.width/2)) {
root["tile"+i]._x -= Stage.width/2+size;
}
tileY = root["tile"+i]._x/size;
tileX = root["tile"+i]._y/size;
if (_root.map[tileX][tileY] != 0) {
root["tile"+i].gotoAndStop(_root.map[tileX][tileY]+1);
} else {
root["tile"+i].gotoAndStop(1);
}
}

sorry for the triple post, but I just wanted to say that I’ve worked out all the problems. The reason it lagged was that the “empty” tiles had a vector graphic (accidentally). Now it’s not lagging and I can make my levels as big as I want! :thumb: