It is a bit simple problem but i could not figure it out.
1-> Application Starts (with MainMenu scene)
2-> Start (Sub levels scene is replaced)
3-> Select First Level (Level1Scene is replaced. Game started.)
4-> Get back to main menu. (MainMenu Scene is replaced)
5-> Start (Sub levels scene is replaced)
6-> Select First Level
7-> Crashes.
box(32842,0x3f5c9d98) malloc: * error for object 0x4bed44: incorrect checksum for freed object - object was probably modified after being freed.
* set a breakpoint in malloc_error_break to debug
When i replace scenes a to b, is a released?
As far as i know, only pushScene keeps scene at memory.
+(CCScene *) scene {
CCScene *scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
return scene;
}
Scene and layer are autorelease objects. Must be released when replaceScene is called. (If previous scene is released while replacing scenes.)
I have solved this problem. Its weird but works.
I had a class that subclass of CCSprite called Collectable. It has cousing crash when second time replacing the scene that has Collectable object. I converted it to CCNode and its works now.
Maybe it helps someone.
Related
Since I'm having performance issues with using a UIView parallaxing images in the background of a transparent Cocos2D scene (it was fine in iOS 6), I think I may use the notification node to run the parallax since I need the parallax running between scene transitions. I've tested setting the notifications node, but I haven't figured out how to make it display in a different z-order. I want it to display under everything in the scene.
Right now for my test I have
CCLayer *layer = [CCLayer node];
CCLabelTTF *labelTest = [CCLabelBMFont labelWithString:#"test" fntFile:#"readySetGo.fnt"];
labelTest.position = CGPointMake(100,100);
[layer addChild:labelTest];
[[CCDirector sharedDirector] setNotificationNode:layer]; // Layer should be placed here
[layer onEnter]; // Schedule for updates
Always the same problem .
I have a scene and i am adding it a CClayer from another class , which is some background with ccmenu on him .
When touching it, touches goes under this layer, and i dont want it .
otherClass *layer=[otherClass alloc]init]; //otherClass returns a cclayer .
[self addChild: layer];
layer is good, and is above my scene , but the touches goes down.
is there a way in cocos2d to enable ONLY touches at the top layer ??
I must change the touches priority now ?
You need to change touch priority and set to swallowsTouches.
To do this, register your layer on touchDispatcher setting these parameters (you can also see CCLayer registerWithTouchDispatcher method as example):
[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
If I have a CCSprite that has previously been added to a parent CCNode or CCLayer, is there any way to change its parent object?
I tried doing:
[self setParent:newParent];
...
and then:
[parent removeChild:self cleanup:YES];
[newParent addChild:self];
Neither works... THe latter actually causes a crash.
I assume that in the second case you added autoreleased object to the parent. In this case sprite will be deleted after removing from parent, so you will not be able to add it to another parent. Try this
[self retain];
[self removeFromParentWithCleanup:YES];
[newParent addChild: self];
[self release];
And in the case of crash with error, post the crash message in future. It can help to figure out problem.
I think if you first remove (NO cleanup), then add to new parent, it should work.
Is it possible to add global layer in Cocos2d, which is not affected by scene transitions?
As I can see, it should be above all scenes hierarchy.
There's an old and short discussion on Cocos2d forum, but there's no answer:
http://www.cocos2d-iphone.org/forum/topic/8071
UPD. 'by scene transitions' I mean 'by animated scene transitions'.
You can use the notificationNode property of CCDirector to place a CCNode (ie.CCLayer, CCLabel, etc.) that will remain above scenes, even during transitions. Something like this:
CCLayer *layer = [CCLayer node];
CCLabelTTF *label = [CCLabelTTF labelWithString:#"Test" fontName:#"Marker Felt" fontSize:32];
[layer addChild:label];
[[CCDirector sharedDirector] setNotificationNode:layer]; // Layer should be placed here
[layer onEnter]; // Schedule for updates (ie. so that CCActions will work)
It's meant for notification purposes (ads, etc.) so I wouldn't suggest trying to do anything too fancy from this node.
My gut says no, my brain says maybe.
The documentation says "It is a good practice to use and CCScene as the parent of all your nodes."
I can't test this right now, but looking at the inheritence diagram of CCNode, it looks like the logic of CCNode and CCScene differs only by anchor point. So, you might be able to create a CCLayer to use as your root layer, and add two children to it - A root CCScene, and a CCLayer for your GUI (with a higher Z order).
However, scene transitions may still be tricky, as you generally call CCDirector replaceScene, which works on the root scene you give it. If you give it the CCScene child of your root CCLayer, it may not draw the CCLayer and its GUI child. If you give it the root CCLayer, you are in the same situation as before.
I would still give it a try.
You can create a CCLayer subclass and turn it into a singleton. You add it to the scene like any other child node.
Whenever you transition from one scene to another, you can then remove the layer from the old scene and add it to the new scene as a child. This will only work if you don't use scene transition animations.
The alternative is to not use the CCDirector replaceScene method, and instead design your app to run as a single scene that never changes. To "fake" the changing of a scene you will use two layers, one global layer and another layer that contains your current scene nodes. If you want to transition you can animate the layer with CCActions to, for example, slide out of the screen while sliding in a new layer with a different node hierarchy. All you really lose is the convenience of the CCSceneTransition classes for animating scene changes.
I have a UIButton that moves around randomly on the screen. On clicking on the button, a new scene is loaded that, for now, contains a CCSprite. Here is the code:
//in init
CCSprite *a = [CCSprite spriteWithFile:#"a.png"];
[a setPosition:ccp(0,0)];
[self addChild:a];
Pretty straightforward, and it stumps me why after 'replaceScene' the UIButton of theHelloWorldScene.m is still visible, right on top of the Sprite. Where am I going wrong?
I'm assuming since you're adding a UIButton into a cocos2d Scene you're using the openGLView
something like:
[[[CCDirector sharedDirector] openGLView] addSubView:button];
If this is the case, then before you replace your HelloWorldScene you'll need call something similar to
[button removeFromSuperview]
Where button is the name of your UIButton (in both instances).
A suggestion though would be to use a CCMenu with a CCMenuItem on your HelloWorldScene as UIKit objects don't really mesh very well with Cocos2d.
You really aren't giving enough information. The simplest answer however is that if you do not want the button to be visible anymore then remove the child (the button). You can set a tag on the button and then use [layer getChildByTag:(NSInteger)].