Refresh game scene in cocos2d - cocos2d-iphone

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];
}

Related

add cclayer on top of another

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;
}

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.

Code Refactoring issue

I have game ready and now I am trying to refactor code. I have derived Spider class from CCNode and used targeted delegate method CCTargetedTouchDelegate.
#interface Spider : CCNode<CCTargetedTouchDelegate> {
CCSprite* spiderSprite;
NSString * spiderKilled;
int killed;
AppDelegate *del;
}
+(id) spiderWithParentNode:(CCNode*)parentNode;
-(id) initWithParentNode:(CCNode*)parentNode;
#end
On Touch spider should be killed and here goes the code:
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint tch = [touch locationInView: [touch view]];
CGPoint touchLocation = [[CCDirector sharedDirector] convertToGL:tch];
// Check if this touch is on the Spider's sprite.
BOOL isTouchHandled = CGRectContainsPoint([spiderSprite boundingBox], touchLocation);
if (isTouchHandled)
{
j = j + 1;
killed ++;
[del setKilledScore:j];
[self removeChild:spiderSprite cleanup:YES];
}
return isTouchHandled;
}
I am adding 10 spiders in GameScene layer using: -
for(int i=0; i <10 ;i++){
[Spider spiderWithParentNode:self];
}
But, unfortunately I am not able to remove spiders and giving me EXC_BAD_ACCESS error on this line: [self removeChild:spiderSprite cleanup:YES];
Please help me overcome this error.
Thanks
Update --
Spider Init code
// Static autorelease initializer, mimics cocos2d's memory allocation scheme.
+(id) spiderWithParentNode:(CCNode*)parentNode
{
return [[[self alloc] initWithParentNode:parentNode] autorelease];
}
-(id) initWithParentNode:(CCNode*)parentNode
{
if ((self = [super init]))
{
[parentNode addChild:self];
del = [[UIApplication sharedApplication] delegate];
CGSize screenSize = [[CCDirector sharedDirector] winSize];
spiderSprite = [CCSprite spriteWithFile:#"spider.png"];
spiderSprite.position = CGPointMake(CCRANDOM_0_1() * screenSize.width, CCRANDOM_0_1() * screenSize.height);
[self addChild:spiderSprite];
// Manually add this class as receiver of targeted touch events.
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-1 swallowsTouches:YES];
}
return self;
}
If you manually add the class to the CCTouchDispatcher list, you should remove it from there after you're done using it.

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).