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

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

问题描述

我有3个问题。


  1. 如何在cocos2d中保持fps速率常数并且在每秒(5-8)之类的小间隔内移除是可能保持帧速率最小化。

  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??

[self removeChild:sprite cleanup:YES] 就够了,或者我也应该使用

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

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

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

以下部分代码负责我的框架丢弃。如何以更好的方式完成相同的任务 -

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 fadeout = [CCFadeOut actionWithDuration:1.4f];




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

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



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

-(void)RemoveSmashedSprite:(id)sender
{


    CCSprite *sp = (CCSprite *)sender;



    [self removeChild:sp cleanup:YES];  

}

这称为每秒5-8次。

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

推荐答案

我猜这个被捣碎的精灵可能是一个在屏幕上可见的多个sprite,可能共享相同的纹理(或许是sprite sheet的一部分,或者是所有相同的图像等等)。

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?).

如果是这样,我建议分配你的游戏需要在屏幕上的sprite数量,而不是毁灭(删除,重新创建),你应该重复使用。

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".

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

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).

然后,而不是删除精灵作为一个孩子,简单地[sprite setVisible:NO]隐藏它,然后...当你需要在屏幕上显示一个新的,使用下一个可用(隐藏)精灵数组中(跟踪最后一个使用精灵与索引,并简单地增加1,找到下一个可用。一旦索引超过边界,重置它为0 - 在这一点上,sprite应该已经砸)。一旦你有下一个可用精灵,更改它的属性(位置,规模,纹理?可见性),并应用任何适当的行动(不要忘记停止所有行动,当粉碎它 - 这是什么清理:是)。

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).

如果你的游戏不允许你在这样的圈子,因为用户决定什么可能可能不可见通过与sprite以不同的顺序交互,那么你可以存储一个单独的数组中的捣烂的精灵(同时保持它们在原始数组中的引用 - 然后你可以只是从未使用数组,从数组中删除它,然后如上所述改变它)。

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.

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

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天全站免登陆