Fastest way to resize (zoom) a group of sprites in cocos2d

519 views Asked by At

I wanna to zoom a group of about 10 sprites at the same time. The sprites are different sprite layers with transparent background.

I'm trying to preattach all the sprite at first to the layer and store the reference in an array. After that as I click the button I do this: Sorry this is Javascript but in Objective-C it's almost the same.

    attr.zoomAllVisibleSprites = function() {
    for (var i = 0; i < this.SpriteArray.length; i++) {
        if (this.SpriteArray[i].isVisible()) {
            this.SpriteArray[i].setAnchorPoint(cc.PointMake(0.5,0.5));
            this.SpriteArray[i].setScale(2, 2);
        }
    }
}

The execution of this little snippet requires on my Android phone about 2-3 seconds which is too much for my game. Is there a way to do it faster, optimize this code. Maybe group in a different way the sprites could help ?

2

There are 2 answers

0
Oscar Apeland On

You could replace your array of sprites by using a for-loop for all CCSprites, like this:

for(CCSprite *sprite in self.view.subviews){ //or scenes or whatever
        if([sprite isMemberOfClass:[CCSprite class]]){
           //Do your thing }} 

Might not help alot, but you can atleast ditch the array ;D

0
Ganesh Somani On

It might be the case that device runs out of memory, try switching to 16-bit textures, it can be done by adding this line on start:

CCTexture2D::setDefaultAlphaPixelFormat(kCCTexture2DPixelFormat_RGBA4444);

Also you can unload unneeded textures before load new scene, like this:

CCSpriteFrameCache::sharedSpriteFrameCache()->removeUnusedSpriteFrames();
CCTextureCache::sharedTextureCache()->removeUnusedTextures();
CCDirector::sharedDirector()->purgeCachedData();

If that doesn't work then it might be a issue with Open-GL, in that case you should have a look at this. It worked like a magic trick for me.

All the Best.. :)