0

EDIT (shorter version after additional testing): I only added:

        CGSize winSize = [[CCDirector sharedDirector] winSize];
        CCRenderTexture *rt = [CCRenderTexture renderTextureWithWidth:winSize.width height:winSize.height];

to my BulletCache in my game project (that has a GameScene where an instance of BulletCache is added) and now the app does crash whilst trying to load the gamescene instance (before this I had no problems with this crash).

NOTE: I added a breakpoint in the init method of CCRenderTexture and it does seem to run through smoothly, it must be something after the init.

Conversely I tried the same in the init method of the GameScene and it does not crash the App. Likewise if I add it to an empty cocos2d-ios helloworld template project.

Hence in the cocos2d-ios helloworld template I did an additional testing thinking to see if there was a conflict in having a batchnode and a CCRenderTexture instance and added the following at the end of the helloworldlayer.m init method:

    CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
    [frameCache addSpriteFramesWithFile:@"art1-hd.plist"];
    CCSpriteBatchNode*  spriteBatch = [CCSpriteBatchNode batchNodeWithFile:@"art1-hd.png"];
    [self addChild:spriteBatch];

    CGSize winSize = [[CCDirector sharedDirector] winSize];        
    CCRenderTexture *rt = [CCRenderTexture renderTextureWithWidth:winSize.width height:winSize.height];
    [self addChild:rt];

There was no crash.

I am left with not many glues on how to solve this.

EDIT: Removed original question where I mentioned a pixelperfect collision project where I had found CCRenderTexture

mm24
  • 9,280
  • 12
  • 75
  • 170

1 Answers1

0

Maybe the problem is simply that the original code assumes each sprite to use their own textures. Now if you have a texture atlas, each sprite renders only from a smaller area (rect) of that texture. You'll have to initialize the collision test accordingly with the sprite frame's part of the texture.

All in all, it's possible to do pixel perfect collision detection with sprite frames and texture atlas, but more complicated depending on the actual implementation of the collision test.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • Would you have some sample code for doing pixel perfect collision of two sprites belonging to the same texture atlas (CCSpriteBatchNode from the same plist file)? I don't understand why I can't simply add an instance of CCRenderTexture in a class of my project. This one I think is the first step to understand this better. I will keep digging into this :) – mm24 Oct 21 '12 at 00:57
  • I edited the question because it was not the pixel perfect code crashing but just adding CCRenderTexture and hence I thought that was misleading. For additional reference to the EDIT I added CCRenderTexture in the BulletCache.m class (which is added to the GameScene.m) class. – mm24 Oct 21 '12 at 11:02