I am trying to change a different sprite sheet on my cocos2d project.
And i was using spriteSheet.plist and it was working fine. For some reason, I need to add more sprite and change the file to spriteSheet2.plist
And i already include files like spriteSheet2.plist, spriteSheet2.pvr.ccz
But it ends up return a error msg
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'CCSprite is not using the same texture id'
[[CCTextureCache sharedTextureCache] removeUnusedTextures];
[CCSpriteFrameCache purgeSharedSpriteFrameCache];
CCSpriteFrameCache *frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
// [frameCache addSpriteFramesWithFile:#"spriteSheet.plist" ];
[frameCache addSpriteFramesWithFile:#"spriteSheet2.plist" ];
and other issue if i remove my original 'spritesheet.plist' from Xcode. Sprits is still working on the screen.
would anyone tell what should I do to get ride of the error message?
you have a batch node somewhere which was created with the first texture, to which you are trying to add sprites from the second texture. All sprites contained in a batch node must be from the same texture as the texture with which the batch node was created.
Related
I have a problem.
i'm trying to make a screenshot of a scene then make a sprite and pass this sprite to other scene.
i'm doing this:
RenderTexture* texture = RenderTexture::create((int)Director::getInstance()->getWinSize().width, (int)Director::getInstance()->getWinSize().height, Texture2D::PixelFormat::RGBA8888);
texture->begin();
Director::getInstance()->getRunningScene()->visit();
texture->end();
Sprite* bgSprite = Sprite::createWithTexture(texture->getSprite()->getTexture());
bgSprite->setRotationX(180);
/-------------------------------------
LoadScreen* loadLayer = LoadScreen::create(GameScene, this->_carModel, bgSprite/*, this*/);
/-------------------------------------
then in next scene i do:
bgSprite->setPosition(Point(Director::getInstance()->getVisibleSize().width / 2, Director::getInstance()->getVisibleSize().height / 2));
bgSprite->setOpacity(200);
this->addChild(bgSprite, 1, 1);
I debug this. Sprite is not null. The texture in this sprite has size and etc.
I try to save texure in file. The result is empty .png file.
It's an engine problem, the file is alway saved with RGB in the previous version.
Since the rendering of cocos2d-x 3.0 is based on the command queue, we should always wrap the read/write frame buffer operations with commands. Invoking newImage() directly is not guaranteed to work fine. But "RenderTexture::saveToFile" should work fine because it's wrapped with a custom command in code.
I think this issue has already been solved in cocos2d-x v3.3, please refer http://www.cocos2d-x.org/issues/5562 for more information.
And also you could take a look at "cpp-tests" bundled with cocos2d-x to see how to save screenshot of your scene or sprite.
I am getting this exception in logs
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'CCSprite is not using the same texture id'
What I am trying to do is adding a normal "myfile.png" file to a SpriteBatchNode
**declaration of batch node
CCSpriteBatchNode *_backgroundLayer = [CCSpriteBatchNode batchNodeWithFile:#"sprites.png"];
** usage
This line works perfect
CCSprite *sprite1 = [CCSprite spriteWithSpriteFrameName:#"PngFileKeptInSpriteSheet.png"];
[_backgroundLayer addChild:sprite1];
But, when I use a direct *.png file to add to batch node, It crashes
CCSprite *sprite2 = [CCSprite spriteWithFile:#"myfile.png"];
crashes on line
[_backgroundLayer addChild:sprite2];
On further debugging I found that:
The assertion failure is in file CCSpriteBatchNode.m
inside method -(void) addChild:(CCSprite*)child z:(NSInteger)z tag:(NSInteger) aTag
at line NSAssert( child.texture.name == textureAtlas_.texture.name, #"CCSprite is not using the same texture id");
P.S. : by "normal" I mean not taken from *.plist file
First and foremost, I'd update cocos2D. However, that isn't your problem and probably isn't "fixed" in the latest version anyway. This isn't really a bug
Batch nodes require that all the sprites that you intend to batch are using the same texture. When you load a sprite sheet, it uses one, large texture. When you call spriteWithFile, cocos2d creates a texture from that image.
It's rare that you'll want to create a batch node from a sprite using spriteWithFile. The only scenario I can think of is when you want to draw the same image many times. (Rather than many images from the same texture).
In short, what you are trying to do is unsupported and doesn't make much sense anyway as the two sprites wouldn't be batchable.
I've a problem using the method addchild adding sprites
previously I've never had this problem and I could add png files without problems,now sometimes if I try to add some sprites to the scene using the addchild method I receive these errors:
[2126:15503] cocos2d: CCTexture2D. Can't create Texture. cgImage is nil
[2126:15503] cocos2d: Couldn't add image:image_1.png in CCTextureCache
[2126:15503] * Assertion failure in -[HelloWorldLayer addChild:]
I think that the problem isn't in the code because if I try to add sprites using the images included in the default Cocos2d project it works... I receive this error only from some images...also if I've added it by the same way to the project and to the scene, what could be the cause?
I receive this error also using the default helloWorldLayer class, without changing anything, only adding
CCSprite * sprite = [CCSprite SpriteWithFile: #"image_1.png"];
[self addChild:sprite];
the rest of the code is exactly the same of the default HelloWorldLayer class
there could be something that might cause this error in some png files?
Add image_1.png to your Xcode project. Make sure you use the exact same filename, for example Image_1.PNG won't load on devices due to uppercasing.
I am looking for a way to mask out a layer and reveal the contents of the layer through a rectangle (actually I want multiple rectangles to reveal the underlying layer's content).
I came across this similar question:
Cocos2d iPhone - Sprite cliping/mask/frame
Which had a solution:
http://www.learn-cocos2d.com/2011/01/cocos2d-gem-clippingnode/
However, when trying to use this class, I get the warning: "implicit declaration of function 'glPushMatrix' is invalid in C99"... I also get the error: "Property 'deviceOrientation' not found on object of type 'CCDirector *'"
What do I need to do to get this to work with the latest version of cocos2d?
... In any event, I commented out the deviceOrientation stuff, just to test if it will even work, and it doesn't seem to be.
I've got a CCBatchNode:
sheet = [CCSpriteBatchNode batchNodeWithFile:#"bg.png" capacity:500];
Then I add many sprites to that
[sheet addChild:sprite1];
[sheet addChild:sprite2];
[sheet addChild:sprite3];
Then I make the clipping node layer
ClippingNode *clipNode = [ClippingNode node];
clipNode.clippingRegion = CGRectMake(50, 50, 200, 200);
Then I add the sprite sheet and the clipNode:
[layer addChild:sheet];
[layer addChild:clipNode];
Then I add that to the CCSprite object
[self addChild:layer];
...
The result is, I see my many sprites from the sheet, but there is no clipping mask.. And my console shows a million: "OpenGL error 0x0502 in -[CCTextureAtlas drawNumberOfQuads:fromIndex:] 556"
so..... I am not sure what I am doing wrong-- or if this all has to do with the openGL warnings and device orientation errors... ?
UPDATE: I added #include <OpenGLES/ES1/gl.h> to the ClippingNode.m, and it got rid of the glpush/pop warnings.. But still results in the same OpenGL error once I add the clipNode child to the layer...
You can do it with shaders in cocos2d 2.0. Have a look at this tutorial.
http://www.raywenderlich.com/4428/how-to-mask-a-sprite-with-cocos2d-2-0
my problem is:
I'm making a game for iOS using cocos2d and this game has lots of levels, so I'll have to create a loading scene to load my sprites for each level. ( like new backgrounds, monsters and other stuff )
But I have no idea about this, I'm adding all the Texture Packer Files (.plist and .pvr.ccz)
on the sharedSpriteFrameCache in the GameData.m.
Does anyone knows a good tutorial for this or can help me solve this?
Thanks!
So basically you want to know how to load and unload images as you see fit. How about
#implementation Level1
- (void) loadLevel
{
CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
CCTextureCache* textureCache = [CCTextureCache sharedTextureCache];
// Add the sprite frames. This will load the texture as well
[frameCache addSpriteFramesWithFile:#"monkey.plist"];
[frameCache addSpriteFramesWithFile:#"player.plist"];
[frameCache addSpriteFramesWithFile:#"enemy.plist"];
// Load other textures that are going to be used
_myBackgroundTexture = [textureCache addImage:#"background.png"];
}
- (void) unloadLevel
{
CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
CCTextureCache* textureCache = [CCTextureCache sharedTextureCache];
// Remove textures
[textureCache removeTexture:_myBackgroundTexture];
// Remove sprite frames. This will load the texture as well
[frameCache removeSpriteFramesFromFile:#"monkey.plist"];
[frameCache removeSpriteFramesFromFile:#"player.plist"];
[frameCache removeSpriteFramesFromFile:#"enemy.plist"];
// Though normally, id use frameCache removeUnusedSpriteFrames and
// textureCache removeUnusedTextures
}
...
#end
You can use the sprite by CCMenuItem and also by the Menu as you would require to click and move to that particular level.. The below is the code for adding the menu item image
CCMenuItem *m4 =[CCMenuItemImage itemFromNormalSprite:[CCSprite spriteWithSpriteFrameName:<#(NSString *)spriteFrameName#>]
selectedSprite:[CCSprite spriteWithSpriteFrameName:<#(NSString *)spriteFrameName#>]
disabledSprite:[CCSprite spriteWithSpriteFrameName:<#(NSString *)spriteFrameName#>]
target:self selector:#selector(MoveLeft)];
The above code gives the information you can use to display the sprite for the particular state of the menu. And the "MoveLeft" is the method selector which I have used to call the particular method.
At last you can add the m4 object to the CCMenu and get the desired output...
Hope it works for you.