如何在 cocos2d 中保持 fps 速率恒定 [英] How to keep fps rate constant in cocos2d

查看:16
本文介绍了如何在 cocos2d 中保持 fps 速率恒定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个问题.

  1. 如何在 cocos2d 中保持 fps 速率恒定(几乎).当在每秒(5-8)个小间隔内创建和删除大量 CCSprite 时,是否可以保持帧速率 lmost 恒定??p>

  2. [self removeChild:sprite cleanup:YES] 够用还是我也应该用

    CCTexture2D *texture = [精灵纹理];[[CCTextureCache sharedTextureCache] removeTexture: 纹理];

  3. 以下部分代码负责我的丢帧.如何以更好的方式完成相同的任务-

    id淡出 = [CCFadeOut actionWithDuration:1.4f];

<块引用>

id 调用 = [CCCallFunc行动目标:自我选择器:@selector(RemoveSmashedSprite:)];

CCSequence* sequence= [CCSequence actions:fadeout, call, nil];[粉碎 runAction:sequence];

-(void)RemoveSmashedSprite:(id)sender{CCSprite *sp = (CCSprite *)sender;[self removeChild:sp cleanup:YES];}

这被称为每秒5-8次.所以帧速率下降了.任何人都可以帮助我.

解决方案

我猜有问题的粉碎精灵"可能是屏幕上可见的许多精灵之一,很可能共享相同的纹理(部分也许是一张精灵表,还是所有相同的图像,等等?).

如果是这种情况,我建议分配您的游戏需要在屏幕上显示的精灵数量,而不是销毁"(删除它们,重新创建它们),而应该重新使用它们".

在 NSMutableArray 中存储对所有 CCSprite 的引用,用您认为合适的精灵数量填充此数组(每秒删除多少精灵?每秒添加多少精灵?屏幕上有多少精灵开始? - 使用这些问题的答案来确定最合适的数组大小).

然后,与其将精灵作为一个孩子移除,不如简单地 [sprite setVisible: NO] 将其隐藏,然后……当您需要在屏幕上显示一个新精灵时,使用下一个可用"(隐藏)精灵在数组中(使用索引跟踪最后一个使用的"精灵,并将其简单地增加 1 以找到下一个可用".一旦索引超出范围,将其重置为 0 - 此时,该精灵应该已被粉碎").一旦你有了下一个可用"的精灵,改变它的属性(位置,比例,纹理?,可见性)并应用任何适当的动作(不要忘记在粉碎它"时停止所有动作" - 这个是清理:是"所做的).

如果您的游戏不允许您像这样在圆圈"中四处走动,因为用户通过以不同顺序与精灵交互来决定哪些内容可能可见或可能不可见,那么您可以存储对粉碎的精灵"在一个单独的数组中(同时将它们保留在原始数组中 - 然后您可以从未使用"数组中选择任何对象,将其从该数组中删除,然后如上所述进行更改).

这应该会显着降低帧速率下降,因为您不会一直创建和销毁精灵.

<小时>

可能发生的另一件事是您正在使用不同的图像,并且每个图像都单独存储(image1.png、image2.png、image3.png),并且这些图像的创建/销毁导致纹理被删除并重新添加...为了解决这个问题,创建一个精灵表并将精灵表添加到纹理缓存中,然后使用'spriteFromSpriteCacheName:@"image1.png"'创建你的图像

I have 3 question.

  1. How to keep fps rate constant(almost) in cocos2d.When lots of CCSprite created and removed within a small interval like (5-8) per second is it possible to keep frame rate lmost constant??

  2. Is [self removeChild:sprite cleanup:YES] is enough or i should also use

    CCTexture2D *texture = [sprite texture]; [[CCTextureCache sharedTextureCache] removeTexture: texture];

  3. The following Part of code is responsible for my frame drop.How to accomplish same task in better way-

    id fadeout = [CCFadeOut actionWithDuration:1.4f];

id call = [CCCallFunc actionWithTarget:self selector:@selector(RemoveSmashedSprite:)];

CCSequence* sequence= [CCSequence actions:fadeout, call, nil];
[smash runAction:sequence];

and

-(void)RemoveSmashedSprite:(id)sender
{


    CCSprite *sp = (CCSprite *)sender;



    [self removeChild:sp cleanup:YES];  

}

This is called 5-8 times per second.So the frame rate goes down.Can any one help me.

解决方案

I am guessing that the "smashed sprite" in question is probably one of a number of sprites visible on the screen, more than likely sharing the same texture (part of a sprite sheet perhaps, or all the same image, etc?).

If this is the case, I would recommend allocating the number of sprites your games requires to have on screen and rather than "destroy" (remove them, re-create them) you should "re-use them".

Store a reference to all the CCSprite's in an NSMutableArray, populate this array with the number of sprites you think is appropriate (how many sprites are removed per second? how many sprites are added per second? how many sprites are on the screen to start? - use the answers to these questions to determine the most appropriate size of your array).

Then, rather than removing the sprite as a child, simply [sprite setVisible: NO] to hide it, then ... when you need to display a new one on screen, use the next "available" (hidden) sprite in the array (keep track of the last "used" sprite with an index, and simply increment it by 1 to find the next "available". Once the index exceeds the bounds, reset it to 0 - at this point, that sprite should have been "smashed"). Once you have the next "available" sprite, change it's properties (position, scale, texture?, visibility) and apply any actions that are appropriate (don't forget to "stop all actions" on it when "smashing it" - this is what "cleanup: YES" does).

If your game does not allow you to go around in a "circle" like this, because the user decides what may or may not be visible by interacting with the sprites in a varied order then you can store a reference to the "smashed sprites" in a separate array (while keeping them in the original array as well - then you can just pick anyObject from the "unused" array, remove it from that array and then alter it as mentioned above).

This should reduce your frame rate drops dramatically, as you won't be creating and destroying sprites all the time.


Another thing that may be occurring is that you are using different images, and each image is stored separately (image1.png, image2.png, image3.png) and the creation/destruction of these is causing the textures to be removed and re-added ... to resolve this, create a sprite sheet and add the sprite sheet to the Texture Cache, then create your images with 'spriteFromSpriteCacheName:@"image1.png"'

这篇关于如何在 cocos2d 中保持 fps 速率恒定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆