I need to create an empty sprite as a container and add there in different positions different sprites created by using different images. At the end I need to save the result which in added as child into the container sprite as an image - .png for example.
How I can do that?
You can use RenderTexture and call visit method of Sprite. Here is a sample code:
RenderTexture* renderTexture = RenderTexture::create(width, height, Texture2D::PixelFormat::RGBA8888);
renderTexture->begin();
sprite->visit();
renderTexture->end();
renderTexture->saveToFile("snapshot.png", Image::Format::PNG);
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 made a sprite
CCSprite *sprite=CCSprite::create("1.png");
1.png and mask_circle.png is in my resource folder
CCSprite* mask = CCSprite::create("mask_circle.png");
mask->setPosition(ccp(winSize.width*.20, winSize.height*.80));
sprite->setPosition(ccp(mask->getContentSize().width/2,mask->getContentSize().height/2));
Now I masked this image by using CCMask.CCmask inherits CCSprite
CCMask* masked = CCMask::create(mask , sprite);
masked->setPosition(ccp(winSize.width*.20, winSize.height*.80));
this->addChild(masked,2);
Now this masked pointer of class CCMask is created using the 2 sprites which have 1.png and mask_circle.png in resources folder
Can we create a texture of masked object or not?It doesn't have any resource.It is created by code using other 2 sprites.
I want to create texture of this masked object.
If not possible is there a way to save this masked object sprite to my resource folder by some name like myimage.png?
Any help will be appreciated.
Thanks
I'm not sure if you can get a texture this way directly, but a way around it is to use RenderTexture. Draw the sprites by calling visit on them in a begin/end block and then use the appropriate method for saving the result to a file. After that you can load the image as a texture.
While creating a cocos2d iOS game, there are several options to add spritesheets - CCTextureCache::addImageAsync, CCSpriteFrameCache::addSpriteFramesWithFile, etc. - what is the difference between using these different ways to add a spritesheet?
Similarly, to load a sprite, we can call CCSprite::spriteWithSpriteFrameName or CCSprite::spriteWithFile or CCSpriteBatchNode::batchNodeWithTexture, etc. What is the difference between using these techniques?
Thanks
Anand
Load sprite frames, this also loads the textures:
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"file.plist"];
Use sprite frames:
CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:#"frame.png"];
Add the sprite to a batch node:
CCSpriteBatchNode* batchNode = [CCSpriteBatchNode batchNodeWithTexture:sprite.texture];
[batchNode addChild:sprite];
The batchNodeWithFile works too if you use the same image file of the sprite. If the sprite was initialized with a spriteframe, it'll be the texture atlas image (ie "file.png").
addImageAsync is only needed if you want to load your texture on another thread, usually to animate the loading screen. You'll still have to add the sprite frames afterwards.
CCSprite spriteWithFile creates a sprite from a single image file. Those can be batched too but it's better to use a texture atlas with sprite frames.
I'm trying to add a SpriteSheet that is shown in the layer but not animated yet, I want to animate onClick.
The problem is when adding the sprite to the layer, the sprite is not shown if its not animated on start.
In Cocos2D-iphone I have not this problem.
Thanks in advance for any help provided.
Just have a look on the coocs2d-android master file :
cocos2d-master\cocos2d-android\src\org\cocos2d\tests\ActionTest.java
In this you can find the Animation class and compare your code with the test file...
or
write down your code ...
Alternatively, show a static image (first sprite) until user clicks, then load the sprites and start animation.
In the isometric tiled map z-ordering example for cocos2d-iphone, they use an object that is already in the tilemap as the player sprite, which is rendered as moving through the trees.
Is there a way to use a random CCNode, created though code (not in the tmx file, and not part of the sprite sheet), and have it z-ordered correctly with the tilemap? addChild is not supported on a CCTMXLayer, and the gives an error when using that reads:'addChild: is not supported on CCTMXLayer. Instead use setTileGID:at:/tileAt:'.
There's got to be a way to have a CCNode (let's say a simple CCSprite) z-order correctly with a tilemap, either using cocos2d's API, or some z-buffer technique. Any pointers?
The CCTMXLayer doesn't support adding tiles at runtime because it's implemented with all sprites on a single spritesheet. Because of this implementation detail you also can't call setTexture on an individual tile.
The easiest solution would be to have the sprite you want to use on the spritesheet at the beginning. If you can't do this because it's generated at runtime or something, the next best thing (without touching cocos2d code) would be to modify the spritesheet.
Put a dummy tile in the spritesheet you're using for the TMXLayer and then, once you have the image you want to use, write it to the spritesheet using CCRenderTexture or something and use the newly generated Texture as your TMXLayer's texture.
You could also modify the CCTMXLayer to allow for your functionality but it sounds like you want to avoid that.
If you want to add a tile to say tile (x,y) ( (x,y) in Tiled Map editor coordinates ) then use the following code -
myTileMap is the reference to the CCTMXTiledMap object.
CCTMXLayer *layer=[myTileMap layerNamed:#"yourlayer"];
NSAssert(floorLayer !=nil, #"Ground layer not found!");
CGPoint tileAddPosition = [layer positionAt: CGPointMake(x,y)];
//Create your CCNode or CCSprite or whatever...say object name is **tileToBeAdded**
tileToBeAdded.anchorPoint = CGPointZero;
tileToBeAdded.position = tileAddPosition;
[myTileMap addChild:addedTile z:1];