user2121776
user2121776

Reputation: 121

making my own tile system cause lag

- (void) loadStartingTiles //16 by 24
{
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
     @"clonespritesheet.plist"];

    for(int x = 0; x < 16; x++) //minus 1 for one at the begining
    {
        for(int y = 0; y < 26; y++)
        {
            CCSprite *tempsprite;
            switch (currentscreen[x][y])
            {
                case 0:
                    tempsprite = [CCSprite spriteWithSpriteFrameName:@"block0.png"];
                break;
                case 1:
                    tempsprite = [CCSprite spriteWithSpriteFrameName:@"block1.png"];
                break;
            }
            tempsprite.position = ccp(y*20+10,(16-x)*20-10); //+10 for align for tile size
            [self addChild:tempsprite z:3];
            [tiles addObject:tempsprite];
        }
    }

}

So I make a bunch of sprites from an int array that tells them where they should be then i add them into the nsmutable array tile. then i move everything in the array to the left slowly, and im losing around 20 FPS. what is a more efficient way to make a tile system? my goal is to make randomly generated tiles later on.

- (void) manageTiles:(CGFloat)dt
{
    int tileamount = [tiles count];
    for(int i = 0; i < tileamount; i++)
    {
        CCSprite *tempsprite = [tiles objectAtIndex:i];
        tempsprite.position = ccp(tempsprite.position.x-20*dt,tempsprite.position.y);
    }
}

EDIT: the awnser is

int themap = -20;
- (void) manageTiles:(CGFloat)dt
{
    tiles.position = ccp(tiles.position.x-10*dt,tiles.position.y);

    NSLog(@"%d",themap);
    if(tiles.position.x < themap)
    {
        CCSprite *tempsprite;
        for(int i = 0; i < 16; i++)
        {
            [tiles removeChildAtIndex:0 cleanup:YES];
        }
        for(int i = 0; i < 16; i++)
        {
            switch (tilewall[i])
            {
                case 0:
                    tempsprite = [CCSprite spriteWithSpriteFrameName:@"block1.png"];
                    break;
                case 1:
                    tempsprite = [CCSprite spriteWithSpriteFrameName:@"block1.png"];
                    break;
            }
            tempsprite.position = ccp((themap*-1)+500+10,((16-i)*20-10));
            [tiles addChild:tempsprite];
        }
        themap = themap-20;
    }
}

Upvotes: 2

Views: 82

Answers (2)

dqhendricks
dqhendricks

Reputation: 19251

Where you are going wrong is, you are not using a CCSpriteBatchNode. A CCSpriteBatchNode will draw all of the tiles in one draw operation instead of doing one draw operation per tile. The drawbacks are, each tile in the batch node will have the same zOrder (in a way), and it all must use one source spritesheet per batch node. SO basically if you wanted different layers at different zOrders, or different layers which use different source images for the tiles, you would have to create multiple batch nodes, one for each.

http://www.cocos2d-iphone.org/api-ref/0.99.5/interface_c_c_sprite_batch_node.html

Upvotes: 1

Richard Brown
Richard Brown

Reputation: 11436

Preload the tempsprite variable outside your loop:

CSprite *sprite0 = [CCSprite spriteWithSpriteFrameName:@"block0.png"];
CSprite *sprite1 = [CCSprite spriteWithSpriteFrameName:@"block1.png"];

And then refer them to them in the loop:

        switch (currentscreen[x][y])
        {
            case 0:
                tempsprite = sprite0;
            break;
            case 1:
                tempsprite = sprite1;
            break;
        }

or even better:

tempsprite = currentscreen[x][y] ? sprite1 : sprite0;

Oh, and your inner loop should refer to 24, not 26.

Upvotes: 1

Related Questions