Getting black screen when using CCLayerPanZoom instead of CCLayer - cocos2d-iphone

To be able to pan and zoom some of the content in screen I decided to use CCLayerPanZoom extension. When I look at the source code I can see that it derives from the CCLayer class. So I change the parent class of the node that's pushed to the navigation stack from CCLayer to CCLayerPanZoom. But what I get when the app launches is just a black screen. To make it cleaner, I created a new class, derived it from CCLayerPanZoom, added a test sprite onto it in the init method and pushed it to the navigation stack in the AppDelegate.m. Still I got nothing, just a black screen. Here're the two methods that I've implemented in my class:
#interface TestPanZoom : CCLayerPanZoom {
}
+(CCScene *) scene;
#end
#implementation TestPanZoom
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
TestPanZoom *layer = [TestPanZoom node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
-(id)init{
if(self=[super init])
{
CCSprite *sprite=[CCSprite spriteWithFile:#"Default.png"];
sprite.scale=0.5;
[self addChild:sprite];
}
return self;
}
#end

Related

arc enabled cocos2d - old scene lives after replaceScene

I'm trying to implement a game with cocos2d. I enabled arc according to the instructions on this tutorial.
http://www.learn-cocos2d.com/2012/04/enabling-arc-cocos2d-project-howto-stepbystep-tutorialguide/
I realized a weird behavior after replacing game scene with main menu scene and I can't figure out the problem. After replacement, the new scene appears and works as I expected, but the game scene (old scene) still reacts touches. I thought that when I replace the scene, old scene should be removed completely, but it continue to live under the new scene.
Some of the relevant code is as follows:
Singleton:
+(void) go: (CCLayer *) layer{
CCDirector *director = [CCDirector sharedDirector];
CCScene *newScene = [Singelton wrap:layer];
if ([director runningScene]) {
[director replaceScene: [CCTransitionFlipX transitionWithDuration:0.5 scene:newScene]];
} else {
[director pushScene:newScene];
}
}
+(CCScene *) wrap: (CCLayer *) layer{
CCScene *newScene = [CCScene node];
[newScene addChild: layer];
return newScene;
}
+(void) mainMenu
{
CCLayer *layer = [MainMenu node];
[Singleton go:layer];
}
When I need to go to main menu scene I call singleton class as follows
[Singleton mainMenu]
How can I kill the game scene after menu scene appears?
Thanks for your help.
What does Singleton do? I suspect that it might be the cause, holding on to references of the Scene/Layer or any other nodes while/after replacing a scene.

Cocos2d CCScene & CCLayer setup

Sorry for the newbie question but I was wondering what the difference is between these two different setups for the scene & the layer? I have tried both ways and each one works but I just don't know what the difference is or which one I should use.
#implementation Game
+(id) scene {
CCScene *scene = [CCScene node];
[scene addChild:[Game node]];
return scene; }
Or this way.
#implementation Game
+(id) scene {
CCScene *scene = [CCScene node];
Game *layer = [Game node];
[scene addChild:layer];
return scene; }
I don't see any difference between your 2 sample of code. Your 2 methods are identically the same.
[Game node] returns a layer so in the first case you add it directly into your scene and in the 2 example you just put it into a variable then add it into your scene.
For the compiler this is the same thing here.

ccscene arc and autorelease

I am trying to construct a number of scenes in my GameManager singleton init.
The scene is created via
- (id)init
{
self = [super init];
if (self) { // 'mainScene' is an autorelease object.
mainScene = [CCScene node];
...
}
GameManger holds a strong reference to mainScene:
#interface GameManager : NSObject
{
CCScene* mainScene;
}
But if I try to push the scene later with
[[CCDirector sharedDirector] pushScene:mainScene];
I get EXC_BAD_ACCESS
If I create and immediately push then everything works. Shouldn't the default __strong reference keep the object allocated?
Thanks in advance for any help....
Figured it out ... [CCScene node] is a convenience factory method that does:
[[[self alloc] init] autorelease];
but since I am using arc...I dont want that - I want
mainScene = [[CCScene alloc]init];
instead of
mainScene = [CCScene node];

Cannot remove sprite from new CCNode class

I am creating a small game where objective is to tap and destroy mouse. I created a separate mouse class for it.
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "HelloWorldLayer.h"
#interface Mouse : CCNode <CCTargetedTouchDelegate> {
CCSprite *sprite;
HelloWorldLayer *HelloLayer;
}
-(id) initWithGame:(HelloWorldLayer *)aGame;
-(void) runFloatAction;
#property(nonatomic, retain) CCSprite *sprite;
#property(nonatomic, retain) HelloWorldLayer *HelloLayer;
#end
I am initializing like this in .m file:
-(id) initWithGame:(HelloWorldLayer *)aGame{
if ((self = [super init])) {
self.sprite = [CCSprite spriteWithFile:#"mouse.png"];
self.sprite.scale = 0.3f + CCRANDOM_0_1() * 0.5f;
self.sprite.position = ccp(CCRANDOM_0_1() * 480, CCRANDOM_0_1() * 320);
self.HelloLayer = aGame;
[aGame addChild:sprite];
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self
priority:1
swallowsTouches:YES];
//[self runFloatAction];
}
return (self);
}
I want to remove sprite on tap. For which I am using this code in .m file: -
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
if ([self containsTouchLocation:touch]) {
[self.sprite removeFromParentAndCleanup:YES];
return YES;
}else{
return NO;
}
}
Unfortunately I am not able to remove sprite. Logically, we have to remove sprite from parent. But, its not working in actual.
The way I structure my classes is to have the Mouse class as a subclass of CCSprite. If you handle the touch events in the main game class than you just remove the mouse. You also need to enable touch events if you haven't done that.
I recommend you do the above and put this in your game class.
[self setIsTouchEnabled:YES];
Mouse *myMouse = [Mouse spriteWithImage:#"Mouse.png"];
[myMouse setPosition:CGPointMake(160, 240)];
[self addChild:myMouse];
Then just handle the touch events in your game class.

Switching scenes with inputs

Here's the code I know to change scenes with cocos2d:
[[CCDirector sharedDirector] replaceScene:[HelloWorld scene]];
But I wonder if it's possible I can switch the scenes with some parameters.
I tried this method:
HelloWorld *scene = [HelloWorld scene];
[scene initWithInput:0];
[[CCDirector sharedDirector] replaceScene:scene];
-(void)initWithInput:(int)input is what I wrote for test in HelloWorld class.
And it does't work, does any one know how to do it?
Try overriding the scene method. Something like
+(id) sceneWithInput:(int) i
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorld *layer = [HelloWorld nodeWithInput:i];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
Then you would call
[[CCDirector sharedDirector] replaceScene:[HelloWorld sceneWithInput:0]];