iphone - 制作我自己的瓷砖系统导致滞后

标签 iphone ios objective-c cocos2d-iphone

- (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];
        }
    }

}

所以我从一个 int 数组中创建了一堆 Sprite ,告诉它们它们应该在哪里,然后我将它们添加到 nsmutable 数组图 block 中。然后我将阵列中的所有内容缓慢地向左移动,并且我损失了大约 20 FPS。制作瓷砖系统的更有效方法是什么?我的目标是稍后制作随机生成的图 block 。

- (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);
    }
}

编辑:遮阳篷是

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;
    }
}

最佳答案

你出错的地方是,你没有使用CCSpriteBatchNode。 CCSpriteBatchNode 将在一次绘制操作中绘制所有图 block ,而不是对每个图 block 执行一次绘制操作。缺点是,批处理节点中的每个图 block 将具有相同的 zOrder(在某种程度上),并且每个批处理节点都必须使用一个源 spritesheet。因此,基本上,如果您想要不同 zOrder 的不同图层,或者不同的图层对图 block 使用不同的源图像,则必须创建多个批处理节点,每个批处理节点一个。

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

关于iphone - 制作我自己的瓷砖系统导致滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15627132/

相关文章:

ios - 是否可以用 SpriteKit 动画画一条线?

iphone - 如何在 iPhone 模拟器中工作的 UIAutomation 中获取 captureScreenWithName?

iphone - iPad 中的宏问题 (#define) "showing Expected identifier before numeric constant"错误

ios - 如何在 Swift3 中将事件添加到我的 CustomCell

objective-c - 从 ObjectiveC 类引用 UIViewController

ios - 当应用程序在没有 Xcode 的情况下运行时,IB 中的 GLKView 会导致崩溃

iphone - 从 xib 移动到 Storyboard时的封装?

iphone - 需要创建日历的指导

ios - 我如何从 UICollectionView 传递我的 id

objective-c - 如何在 Cocoa 中更改 NSview 的背景颜色