绘制精灵时,科科斯2D大型FPS掉落

时间:2013-03-23 21:06:18

标签: objective-c xcode cocos2d-iphone lag

我正在为滚动视图绘制52个标签,精灵和按钮。然而,当我去滚动它时,我使用滚动视图是非常滞后的。我使用类似的设置,在x和轴上滚动,没有滞后。我正在iphone 5上测试,所以我认为它能够很容易地处理它。正确计算物体需要移动的距离,并且正确地绘制物体,它实际上是滞后的。画代码:

    int cnt = 40;      
  for (NSString *i in [Trucks GetSetTruckList].TruckList){
    NSMutableArray *Truck = [[NSMutableArray alloc] initWithArray:[TruckDict objectForKey:i]];
     CGSize s = [[CCDirector sharedDirector] winSize];


    CCMenuItemImage *BuyButton = [CCMenuItemImage itemWithNormalImage:@"Buy.jpg" selectedImage:@"Buy.jpg"block:^(id sender) {[self BuyTruck:Truck]; }];
    BuyButton.position = ccp((s.width/2) - 20 , (s.height/2) - cnt  + ShopPointX);
    BuyButton.scale = .5;

    CCLabelTTF *Name = [CCLabelTTF labelWithString:[Truck objectAtIndex:0] fontName:@"Marker Felt" fontSize:19];
    Name.position = ccp(100, (s.height) - cnt + ShopPointX);

    CCLabelTTF *NumPeople = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Ppl: %@" , [Truck objectAtIndex:2]] fontName:@"Marker Felt" fontSize:13];
    NumPeople.position = ccp(200, (s.height) - cnt + ShopPointX);

    CCLabelTTF *NumCrate = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Crgo: %@" , [Truck objectAtIndex:1]] fontName:@"Marker Felt" fontSize:13];
    NumCrate.position = ccp(270, (s.height) - cnt + ShopPointX);

    CCSprite *Pic = [CCSprite spriteWithFile:[Truck objectAtIndex:5]];
    Pic.position = ccp(340, (s.height) - cnt + ShopPointX);
    Pic.scale = .3;

    CCMenu *Menu = [CCMenu menuWithItems:BuyButton, nil];
    cnt = cnt + 40;

    [self addChild:Pic];
    [self addChild:Menu];
    [self addChild:Name];
    [self addChild:NumCrate];
    [self addChild:NumPeople];
    StartShop = 1;
}

1 个答案:

答案 0 :(得分:0)

你可能有滞后因为OpenGL正在为每个精灵进行单独的绘制调用。你是否将精灵放入'CCSpriteBatchNode'?看起来所有的孩子都被添加到“自我”中。如果使用批处理节点,则无论批处理节点中包含多少个子节点,OpenGL都可以对每个CCSpriteBatchNode进行一次“绘制”调用。唯一的问题是所有作为CCSpriteBatchNode子代的CCSprit都必须共享相同的图像。

一种方法是将CCSpriteBatchNodes按图像名称放入NSDictionary中,并将每个CCSpriteBatchNode添加到“self”。

    // This line assumes usage of a CCSpriteFrameCache to init the sprite, but you can create it some other way.
    CCSprite *spr = [CCSprite spriteWithSpriteFrameName:spriteName];

    CCSpriteBatchNode *batchNode;
    if([self.batchNodeDictionary objectForKey: spriteName]) {
        // Acquire the batchnode by image name
        batchNode = (CCSpriteBatchNode *) [self.batchNodeDictionary objectForKey: spriteName];
    } else {
        // BatchNode for this image doesn't exist yet; therefore, populate a new batch node for the image name

        batchNode = [CCSpriteBatchNode batchNodeWithFile:spriteNamePNG];
        [self addChild:batchNode];
        [batchNodesDictionary setObject:batchNode forKey:spriteName];


    }
    [batchNode addChild:spr];