add cclayer on top of another - cocos2d-iphone

I want to add one cclayer on top of another.
I have tried this by using following code
+(id) scene
{
CCScene *scene = [CCScene node];
GameScreen *layer = [GameScreen node];
[scene addChild: layer];
GameScreen *newLayer=[GameScreen node];
[scene addChild:newLayer];
return scene;
}
but may be there are some mistakes, cuz when i tried to add something on newLayer ,it says using undeclared variable even when ideclared that in .h file also.
Can you please help me with detail code?

Instead of doing this in the "scene" class method, add the "new" CCLayer in the -(id)init{} method:
-(id) init {
self = [super init];
if (self) {
GameScreen *newLayer=[GameScreen node];
[self addChild:newLayer];
//Other code
} return self;
}

Related

Refresh game scene in cocos2d

Hey folks I've being working around as follows. I get 2 layers, one's for control and the other's for game stuff. The problem is that the game's stucked ( the game's scene is refreshed and showed but it's out of control) when I trigerred a function for restart from my control layer. Have no idea where the problem is.
//restart function from input layer
......
-(id) init
{
if ((self = [super init]))
{......}
return self;
}
......
-(void)restart
{
GameScene* game = [GameScene sharedGameScene];
[game restartScene];
}
//gameLayer
......
static GameScene* instanceOfGameScene;
#implementation GameScene
+(GameScene*) sharedGameScene
{
if(instanceOfGameScene == nil)
instanceOfGameScene = [[self alloc]init];
return instanceOfGameScene;
}
+(id) scene
{
CCScene* scene = [CCScene node];
GameScene* layer = [GameScene node];
[scene addChild:layer z:0];
InputLayer* inputLayer = [InputLayer node];
[scene addChild:inputLayer z:1];
return scene;
}
-(void) restartScene
{
CCScene * newScene = [GameScene scene];
[[CCDirector sharedDirector] replaceScene:\
[CCTransitionFade transitionWithDuration:0.7f scene:newScene]];
}
......
[scene:newScene] does not seem to be valid ObjC syntax, does this even compile?
Anyway, you'll want to use newScene there and nothing else:
-(void) restartScene
{
CCScene * newScene = [GameScene scene];
[[CCDirector sharedDirector] replaceScene:newScene];
}

cocos2d add layers on scene

I know that my question might be stupid but I searched and I can't find why it's not working.
I create a CCLayer class BackgroundLayer with implementation below:
#import "BackgroundLayer.h"
#implementation BackgroundLayer
- (id)init {
if (self != nil) {
CCSprite *background = [CCSprite spriteWithFile:#"menu.png"];
background.anchorPoint = ccp(0, 0);
[self addChild:background z:-1];
NSLog(#"test");
}
return self;
}
#end
and I want to add it on main menu scene and I have:
#import "MainMenuScene.h"
#import "BackgroundLayer.h"
#implementation MainMenuScene
+ (id)scene {
CCScene *scene = [CCScene node];
BackgroundLayer *backgroundLayer = [BackgroundLayer node];
[scene addChild:backgroundLayer];
return scene;
}
- (id)init {
self = [super init];
if (self != nil) {
}
return self;
}
#end
My problem is that NSLog test appears but the background doesn't load. If I add the background on the init method of MainMenuScene it works... Shouldn't I suppose that the layer works this way?
Not sure if related but you forgot self = [super init]; in BackgroundLayer.
Try commenting out the anchor point line to see if the image shows up then.

Calling CCAction (CCTintTo) with method of another class to alter CCSprite of the other class

I have a class ButtonLayer with a method in it called redClick.
redClick's implementation looks like this..
-(void) redClick {
[red runAction: [CCTintTo actionWithDuration:0.1 red:200 green:200 blue:200]];
}
The variable red is a CCSprite in the ButtonLayer class.
I have another class called MainLayer that inherits from CCLayer. The scene method in this class looks like this..
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
overlay = [ButtonLayer node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
[scene addChild: overlay];
return scene;
}
In my ccTouchesBegan method in MainLayer, I call [overlay redClick], but when I call it, nothing changes. The CCSprite remains unchanged.
Add overlay layer to your HelloWorldLayer 'layer'.
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
overlay = [ButtonLayer node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
[layer addChild: overlay];
return scene;
}

not able to change background using CCLayerColor and initWithColor:cc4(255,255,255,255)

i am working on very first tutorial on cocos2d understanding basic concept.I am trying to change background color from default(black) to white.here is my code:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#interface GameScene : CCLayerColor {
CCSprite *player;
}
+(id) scene;
#end
and implementation goes here:
#import "GameScene.h"
#implementation GameScene
+(id) scene
{
CCScene *scene = [CCScene node];
CCLayer *layer = [CCLayer node];
[scene addChild:layer];
return scene;
}
-(id) init
{
if ((self=[super initWithColor:ccc4(255, 255, 255, 255)])) {
self.isAccelerometerEnabled=YES;
player= [CCSprite spriteWithFile:#"Icon-72.png"];
CGSize screenSize=[[CCDirector sharedDirector] winSize];
float imageHeight=[player texture].contentSize.height;
player.position=CGPointMake(screenSize.width/2, imageHeight/2);
[self addChild:player z:0 tag:123];
}
return self;
}
-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
CGPoint pos=player.position;
pos.x+=acceleration.x*10;
player.position=pos;
}
- (void)dealloc {
[super dealloc];
}
#end
any suggestion? thanks
CCDirectory only takes CCScenes. So most likely the black screen you experience is not a faulty CCColorLayer, but simply the blank stage.
Subclass CCScene as GameScene, then add a CCLayerColor to that with your desired color, as well as your player. Then call [[CCDirector sharedDirector] runWithScene:gameScene].

Cocos2d Displaying a Layer Issue

I'm trying to display a pause game layer from the applicationDidEnterBackground: method and for some reason it does call the method but nothing happens.
Delegate
- (void)applicationDidEnterBackground:(UIApplication*)application {
ship = [[Ship alloc] init];
[ship pause];
Pause Method
- (void)pause
{
BOOL isPaused = [[CCDirector sharedDirector] isPaused];
if(!isPaused)
{
//Pause the game
ccColor4B c = {100,100,0,100};
PauseLayer *pauseLayer = [[[PauseLayer alloc] initWithColor:c] autorelease];
[self.leftMenuItem setIsEnabled:NO];
[self.rightMenuItem setIsEnabled:NO];
[self.fireMenuItem setIsEnabled:NO];
[self addChild:pauseLayer z:10 tag:100];
[[CCDirector sharedDirector] pause];
}
}
PauseLayer
+ (id)scene
{
CCScene *scene = [CCScene node];
PauseLayer *layer = [PauseLayer node];
[scene addChild:layer];
return scene;
}
- (id)initWithColor:(ccColor4B)color
{
if((self = [super initWithColor:color]))
{
self.isTouchEnabled = YES;
[CCMenuItemFont setFontName:#"Marker Felt"];
[CCMenuItemFont setFontSize:40];
CCMenuItemFont *resumeGameItem = [CCMenuItemFont itemFromString:#"Resume" target:self selector:#selector(resumeGame)];
CCMenuItemFont *menuGameItem = [CCMenuItemFont itemFromString:#"Menu" target:self selector:#selector(goToGameMenu)];
CCMenu *menu = [CCMenu menuWithItems:resumeGameItem,menuGameItem,nil];
[menu alignItemsVerticallyWithPadding:40.00];
[self addChild:menu];
}
return self;
}
Thanks!
If you init the ship in the delegate, it isn't added to any cocos layer that I can see. you would have to get a reference to the current scene and add the ship to it (assuming ship is a sub-class of Cocos node).