how to create transparent ccscene in cocos2d? - iphone

I need to show ccscene on game pause. But it should be transparent. Can anyone please help me with this??
Thank you,
Anks

I suggest you should not create a separate scene for pause layer. What you can do is, create a layer similar to HUD Layer.
Second option would be going for something like this.Just dont set the background color of the layer.
Hope that helps.

I know you already have an answer, but wanted to share my solution because I hit this link when researching the same issue. I also posted this in the cocos2d forums.
- (void)pauseSchedulerAndActionsRecursive:(CCNode *)node {
[node pauseSchedulerAndActions];
for (CCNode *child in [node children]) {
[self pauseSchedulerAndActionsRecursive:child];
}
}
- (void)resumeSchedulerAndActionsRecursive:(CCNode *)node {
[node resumeSchedulerAndActions];
for (CCNode *child in [node children]) {
[self resumeSchedulerAndActionsRecursive:child];
}
}
I have one scene and two layers, a stage/gameplay layer and a menu layer. My menus animate in and out, and I wanted my pause menu to be able to animate in and out as well. So I just added the above functions to my stage/gameplay layer. Then when the user hits pause, I call pauseSchedulerAndActionsRecursive: on my stage/gameplay layer and add my menu layer to the scene on top. My menu swallows all the touches, so touching is also disabled on the stage/gameplay layer while the menu layer is up. Then just call resumeSchedulerAndActionsRecursive: method to resume. Hope this helps someone out.

Related

Cocos2d access scene properties and methods

I'm trying to include a pause feature for my game. So far running pause on the [CCDirector sharedDirector] has been good enough when run through my pauseGame method (which incorporates a BOOL to tell if game is paused, etc.)
However, I noticed that if I go back to the homescreen on my device, my application delegate will automatically run pause and resume on the sharedDirector.
Ideally, I would like access my active scene/layer's so I can run my own pause method.
How can I: 1) check if the current scene is my game scene
2) access the game scene's pause property, and run the pauseGame method on it?
Any help appreciated. Thanks
The running scene is [CCDirector sharedDirector].runningScene
If you call [scene pauseSchedulerAndActions]; on the current scene this is will not be resumed when going back and forth on the home screen.
If you need to pause ALL nodes in the hierarchy, here is a method to extend CCNode
-(void)recursivePauseSchedulerAndActions {
[self pauseSchedulerAndActions];
CCNode *child;
CCARRAY_FOREACH(children_, child) {
[child recursivePauseSchedulerAndActions];
}
}
-(void)recursiveResumeSchedulerAndActions {
[self resumeSchedulerAndActions];
CCNode *child;
CCARRAY_FOREACH(children_, child) {
[child recursiveResumeSchedulerAndActions];
}
}
I use a different approach for pausing a game.
I usually have a game scene with game layers. To pause the game I add a new layer (covering the entire screen) and when I pause the game I display this layer. This way you can stop touching events for the game layer and restore them when resuming the game (and removing the pause layer from the scene)

How to draw a view over a GLkit view on iOS5/Ipad

I´m creating a GLK view on iOS5.
First, i create the context.
GLKView *view = (GLKView *)self.view;
view.context = self.context;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
[self setupGL];
Second, i draw a triangle moving.
This works.
Now, i want to create a view over the GLKview. So, i used the Storyboard, to draw a View (HUDView) with background red, and i put it over the main view. When i run the app, only openGL appear, i assume that red background view is drawing behind GLKView. Why?
I have tried to use on Hudview -> viewDidLoad
[self bringSubviewToFront:self];
But the problem nothing happen.
Any idea how could i solve this problem????
Thanks in advance.
I have found a sample that solve the problem. Glfun is a sample where a openGL layer is drawn, and you can add a hud view over it. Hopes help people :D

How to add transition effect in Current running scene in cocos2d iphone

How to add transition effect in Current running scene in cocos2d iphone. Means I am making a game and after each goal I want to give a fade effect or any type of effect on the current running scene.
If I write this, It replace current scene to its new scene. But I don't want to replace scene.
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0f scene:[GamePage scene]]];
Is there some way to show effect on the current page like this. I know that it is wrong but I want something like this :
[self transitionEffect:[CCTransitionFade actionWithDuration:0.5]];
For Scene,Layer (Subclass of CCNode) action related to Opacity will not work. !
You can Either Use transition, or Must have to apply CCFadeTo to all of your sprite.
But If you are choosing to CCFadeTo to all sprites, this will require allocation of lots of actions suddenly ! FPS slow down !!
Another best approach:
Tell to your designer, to make 1 x 1 pixel Square black dot image.
Add this code at last in the init method.
CCSprite *temp=[CCSprite spriteWithFile:#"squaredotBlack.png"];
temp.position=ccp(s.w/2,s.h/2);
[self addChild:temp z:50000]; //set as most top layer
temp.scaleX=s.w;
temp.scaleY=s.h;
temp.opacity=0;
Then Apply, For "Fade out" process of whole screen, Increase the opacity.
temp.opacity=0;
[temp runAction:[CCFadeTo actionWithDuration:1 opacity:255]]; //0 to 255
Then Apply, For "Fade In" process of whole screen, decrease the opacity.
temp.opacity=255; // this will cover whole screen with black color
[temp runAction:[CCFadeTo actionWithDuration:1 opacity:0]]; //255 to 0
you can run an action on the entire CCLayer
[self runAction:[CCFadeOut actionWithDuration:0.5f]];
or you can use CCFadeTo to fade to a desired opacity.

hot to disable touch handling in CCLayer in cocos2d

I've got a CCLayer subclass i'm using to display some sprites and to show some animations. Also it has a CCMenu with some items. When user selects some of the menu item i want to run an animation and then to show another scene. But i want user not to be able to touch anything on the screen while animation is running.
Of course, i can just disable handling touches in my callbacks, but maybe there is more simple way - just to disable all touch handling for a while ?
Disable touch dispatcher before animation running and enable touch dispatcher after animation stopped. Here is the code snippet:
[[CCDirector sharedDirector] touchDispatcher].dispatchEvents = NO;
CCAnimation* animation = [CCAnimation animationWithFrame:#"numberexplode" frameCount:5 delay:0.2];
CCAnimate* animate = [CCAnimate actionWithAnimation:animation];
CCCallBlock* completion = [CCCallBlock actionWithBlock:^{
[[CCDirector sharedDirector] touchDispatcher].dispatchEvents = YES;
}];
CCSequence* sequence = [CCSequence actions:animate, completion, nil];
[self runAction:sequence];
You want to look at the CCTouchDispatcher singleton class. If you add a targeted touch handler that swallows touches (and does nothing) then you won't get any touches handled. As far as I can tell there's no way to totally disable touches.
Alternatively you can make a new CCLayer that's on top of everything else (I think z order really high will do this), and make it clear, and have it do nothing with touches.
hope that helps.

Cocos2d adding a background image to a layer?

I just finished reading some of the cocos2d documentation, and as far as I understand to add a background image to a layer you have to do something like:
Sprite * bg = [Sprite spriteWithFile:#"Background.png"];
[layer addChild:bg z:0];
Allthough as far as my testing goes you can just directly add the sprite to the scene like this:
#implementation IntroScene
- (id) init {
self = [super init];
if (self != nil) {
Sprite * bg = [Sprite spriteWithFile:#"Background.png"];
[bg setPosition:ccp(240, 160)];
[self addChild:bg z:0];
[self addChild:[MenuLayer node] z:1];
}
return self;
}
What is the difference between this 2 options, and what is the better way to set a background for a layer?. Any help will be greatly appreciated.
You do not need to have a layer, but they are handy. Think of the layers as a way of grouping sprites (both visually and organizationally). You may want your background images grouped as a layer and all the enemies in a game as another layer. This allows you to deal with each set more cleanly. e.g. You can remove a specific layer instead of finding all of the enemies and removing them.
If it helps, play with layers in Photoshop or a graphics editing program. Layers in cocos2d work similarly.
For a simple scene, adding the background to a layer or to a scene won't make much difference. But it doesn't take long before the benefits of layering kicks in.
Also layers control drawing order (over and above any inter-layer z ordering).
In geoSpark, for example, I have a background layer, a spark layer, a UI layer, etc. (Simplified). But this allows me to make sure my UI stuff is always above any game stuff (the spark layer) and all that is above the background.
If your game does only additive rendering, draw order won't matter much. :)
-- David
scene is a blackboard, layer is a page... news is wrote on page, but, if you want you can write on the board. If you want erase all new.
In cocos2d we can add any node on any node. But it depends on level of hierarchy.
You can add Layers on Scenes. If you want same property or look for all layers that you add on a CCScene you can add those in CCScene (as in your second case). but if you want to have different feature on each layer add those to CCLayer.
CCLayer is meant for grouping a set of nodes/sprites for some purpose. For example you can add a layer which includes options after pausing a game, or HUD layer for displaying som kind always on screen along with some scrolling layer (liken in some scrolling platform games).
Finally you can even CCNode on a CCScene, a layer on another layer, and even a CCNode on a CCSprite.
But the order of drawing in graphics context in cocos2d differs.
Hope you got the difference.
Good luck!