How to achieve invariable background during scene change in cocos2d? - cocos2d-iphone

Scene A and scene B have same background. If transition from A to B has animations (say, move in from left), the background will also take part in it. I do not want this. In other words, the background layer belongs neither to scene A or scene B, but something parallel to scenes and laid behind. Can I achieve this?

Although I don't think you can do this with scenes directly,
A plausible work around would be to do the following:
Do a scene replace with no transition
Set all sprite's positions to offscreen, then CCMove them into their proper places.
It's messy, but it could work.
Adam

Related

cocos2d-x 3 batch drawing explained

I have read this page to understand batch drawing details, but I still have questions. I know that in order to reduce draw call number we need to use batch drawing. I use it like this:
auto spritebatch = SpriteBatchNode::create("ingame.png");
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ingame.plist");
And now I need to create a Sprite I have to do this:
auto backgroundSprite = Sprite::createWithSpriteFrameName("back_gradient.png");
spritebatch->addChild(backgroundSprite);
But I don't understand the following things:
What if my game has several spritesheets. For example I have HUD spritesheet and ingame spritesheet. Now if I want to show ingame screen with HUD then I need to create 2 SpriteBatchNode? and add them into ingame layer?
What if the same spritesheet should be used in different Scenes. Should I do the following again?
auto spritebatch = SpriteBatchNode::create("ingame.png");
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ingame.plist");
What if I use sprites with Button, TextEdit, Label and other UI elements.
First of all can I initialize Button state images from spritesheet?
As I know I cannot add UI element as a child to SpriteBatchNode. In this case how to combile UI elements and sprite in the same view/scene?
Sorry for lots of questions. But the fact is that I could not find any resource that contains the explanations to this questions. While they are all related. If you don't know answers to these questions, you don't know how to use SpriteBatchNode.
Don't use CCSpriteBatchNode in cocos2d-x v3. Batching is automatic and best left to the renderer to optimize draw calls through batch drawing. It says so right in the article you've linked:
The Render graph was decoupled from the Scene graph.That means that auto-batching is supported, finally :-) And the new renderer is so fast, that we no longer encourage the use of SpriteBatchNode.
I don't agree, depending not how fast new render is we want to get most of it as we can.
Narek, you are correct.
During rendering the geometry will be sorted to reduce the quantity of GL calls. But don't expect it will sort children of different parents in one line. Example: you have
Node A with children ab and ac
Node B with children bd and be.
if b and d uses textures of same atlas it is not guaranted you will get any perfomance boost of using atlases at all.
But I can confirm, currently it is really fast, and at my case the GL calls are not the bottleneck at all :)

Cocos2d CCLabelBMFont how to add a background to string

I am wondering how can I add a border & background to labels generated via CCLabelBMFont class in cocos2d.
I don't want to use sprites because my labels are generated on the fly and will keep changing and the size of labels are also different.
Also, I want user to touch and move these labels across the screen. When user picks a label it wiggles a bit like in free air. In that case, I wish to keep low complexity and preserve memory and cpu calculations.
Anyone knows best ways to achieve this?
IOS app LetterPress has similar effects.
Create your own class, that will incapsulate creation of complex node.
It will have several layers, for example, the first layer can be simple CCLayerColor of given rect with zOrder -2, the next layer will be your CCLabelBMFont with zOrder -1 and then you can overload draw method to draw border over your control. All that you draw in this method will be drawn with zOrder 0.
Then you can encapsulate any effects in this class. For example, you can rotate it a bit with method pick, etc. Whatever you want.

Overlay items in qtgraphicsscene

I would like to draw some dynamical overlay elements in a scene.
Usually I would go for the paintEvent in the view or foreground painting for doing overlays, but this time I need to interact with these elements: they are like every other item in the scene... Mostly, this choice comes from the fact that these items are somewhat complex, and they share the rendering with other QGraphicsItem(s).
The problem is that, being overlayed, they shall be treated a bit differently than any other element in the scene: rendered in view coord space, not in scene coord space, and ignore changes in the view rect, dragging, dropping, rubber band selection, and such.
The question is somewhat general: how could I use the scene for a thing like this? Is there a better option for doing overlays e.g. using multiple scenes and rendering them all, in some magic way, in a single view widget?

Cocos2d: Slowly reveal a sprite( think WoW spell cooldown )

I'm wanting to create a WoW cooldown effect where a player does some action and is not able to do the action again until the sprite is fully shown again. I have a grayed out version of the same sprite and am wanting to slowly reveal the sprite until it is fully available again. So, there will be a slow blend vertically of the gray and colored sprite.
Is there a way to do this with built in functionality with Cocos2d and CCSprite?
I'm using v2 of Cocos2d so I could write a shader which I think would be pretty easy, but before I went this route I wanted to see if there is an easier way.
Take a look to the CCProgressTimer class. If I understand right, it will make what you want
You can use CCFadeIn to animate the colored sprite over the grayed sprite :
[coloredSprite runAction:[CCFadeIn actionWithDuration:1.0f];

Draw shape shifting objects on (desktop)screen

I'm currently developing a program to show and control animated sprites on the desktopscreen. My problem is now to actually draw them onto the screen. The user should still be able to access other applications, as long as the sprite does not obstruct it.
My attempts are below and I hope, someone can point me in the right direction. I don't really care which library I need to use, as long as the performance is good enough for something around 20-30 animated sprites.
My attempts so far:
My first attempt was with Qt. I used a QWidget with a QLabel in it to show the pixmap of an object. The pixmap itself had an alpha channel and I used the "setMask(pixmap.mask()" method of QWidget to remove anything I don't want to show. But this method can't be used for rapidly shifting shapes, like moving creatures. If setMask is called all 50-100ms to change the mask to the next movementphase, then the cpu load gets to high with a lot of creatures moving at the same time.
My second attempt was to use one QWidget for all creatures. This way setMask ist called only one time and not once for every creature. It's possible to move more creatures this way, but the screen is flickering like hell when moving the mouse pointer over the creatures.
My third attempt were the XShape functions from Xlib to change the shape of each creature, but the performance is not much better then setMask.
I tried the transparency in Qt but if I use a QWidget over the whole screen the cpu load of X gets really high while moving the mouse. I don't know, if I can do something better here.
Create a QGLWidget and learn to use the OpenGL API to draw sprites within it, even if only using glDrawPixels rather than texture objects.
You certainly won't have any problems drawing a few tens of sprites, and the time spent learning OpenGL will be a good investment if you aspire to do more complex graphical things in future.
Not sure if this is your language but the ESheep is on GitHub, could get you started: https://github.com/Adrianotiger/desktopPet