I started my app using the base code found here in the menus tutorial. In this manner, all of my 'screens' (there are only 5 of them) are implemented as extensions of the CCLayer class, and I have a shared + Scene Manager which works by adding my layer classes as children to a new scene and then using the director to run or replace the currently playing scene:
+(void) goMenu{
// \/---------- Issue right here, next line:
CCLayer *layer = [MenuLayer node];
[SceneManager go: layer];
}
+(void) go: (CCLayer *) layer{
CCDirector *director = [CCDirector sharedDirector];
CCScene *newScene = [SceneManager wrap:layer];
if ([director runningScene]) {
[director replaceScene: newScene];
}else {
[director runWithScene:newScene];
}
}
+(CCScene *) wrap: (CCLayer *) layer{
CCScene *newScene = [CCScene node];
[newScene addChild: layer];
return newScene;
}
The problem I am having is as follows. Let's say I have 2 layers -- 'MenuLayer' and 'GameLayer'. I start off with MenuLayer and later on use [SceneManager go:[GameLayer node]] to transition over to GameLayer. From GameLayer, if I goMenu the app terminates with an 'NSInternalInconsistencyException', reason: 'child already added. It can't be added again' where indicated. I assumed that this is happening because I am trying to add some child sprites in the layer's init code and I am re-adding them again.
My first attempt at debugging was to call [self removeAllChildrenWithCleanup:YES]; within the onExit code of all my layers. That didn't solve the issue. I added in some debugging logs and found out the last line that executes before the script blows up is the indicated one [myLayerClass node];. The init code inside the layer class does not get executed at all -- so it can't be one of the sprites or other children being added that is causing the issue. Again -- remember that everything works the first time I try to open any scene. It's moving back to an opened scene that is currently problematic.
So I tried a different approach -- the approach used in Cocos2D Hello world. I modified all my Layer Classes by adding a singleton +(id) scene method defined as follows:
+(id) scene
{
CCScene *scene = [CCScene node];
myLayerClass *layer = [myLayerClass node];
[scene addChild: layer];
return scene;
}
Added in a [super dealloc] in the dealloc, and encapsulated all my init code within:
-(id) init
{
if( (self=[super init] )) {
// All init code here....
}
}
Of course, all of the goLayerName singleton methods in the scene manager had their contents replaced to match this -- for example:
+(void) goMenu {
//CCLayer *layer = [MenuLayer node];
//[SceneManager go: layerMenu];
CCDirector *director = [CCDirector sharedDirector];
if ([director runningScene])
[director replaceScene:[MenuLayer scene]];
else
[director runWithScene:[MenuLayer scene]];
}
I figured this was the way Cocos2D's hello world worked, it must be good. Also, since each of my scenes would be created once and once alone (and thus every layer would be created once and once alone). No effect whatsoever! The first time I visit any scene it runs as expected. Whenever I try to navigate back to any existing scene, I get the error I mentioned above.
I tried poking around SO and found this, but I am unsure how to properly implement it; also I'm not entirely sure that would even solve the issue -- since the symptoms are different (he just gets a pink screen, whereas my app terminates).
Any assistance would be appreciated.
For anyone who was interested in this question, I was able to resolve the issue.
In summary, any child of a child you add, will not be released with releaseAllChildrenWithClenup. That function will only release the immediate children, so you are responsible to release any nested children yourself.
In my game, I had some characters and their eyes were nested in the head sprites (so that I could just move / rotate the heads). The eyes had their own animation loop but I wanted them to transform with the head. All that worked, but since I had a line of code in init which basically [headSprite addChild:eyeSprite];, I had to also add [headSprite removeChild:eyeSprite]; in the onExit code to dissociate it, followed by releaseAllChildrenWithClenup to remove all of the n=1 level children as well.
Once that was handled, everything worked as advertised.
Related
I just started learning Cocos2dx, and use the basic HelloWorld project. I added a SecondScene and and a button in it to change scene. But once the popScene method executed, screen became black and it didn't pop to first scene. I don't have any idea what's wrong.
Here's the code I modified a little in HelloWorld.cpp:
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
CCDirector::sharedDirector()->pushScene(SecondScence::scene());
#endif
Code in SecondScene:
#include "SecondScence.h"
USING_NS_CC;
CCScene* SecondScence::scene(){
CCScene* scene=CCScene::create();
SecondScence* layer = SecondScence::create();
scene->addChild(layer);
return scene;
bool SecondScence::init(){
CCLabelTTF* label = CCLabelTTF::create("hfiejfeiojfoej", "Arial", 30);
label->setPosition(ccp(200,200));
this->addChild(label);
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png","CloseSelected.png",this,menu_selector(SecondScence::popScene));
pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width-20, 20));
CCMenu *pMenu = CCMenu::create(pCloseItem,NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu,1);
return true;
}
void SecondScence::popScene(CCObject* pSender){
CCDirector::sharedDirector()->popScene();
}
By the way, I use cocos2dx 2.2 and xcode5, the the console print a message:Cocos2d: cocos2d: deallocing CCDirector 0x6906f0
Verify that the popScene method isn't run twice, perhaps by the user quickly tapping the menu item (or a bug).
That would pop both the current and the HelloWorld scene, leaving the director with no scene to display. It would also explain the director deallocating.
You can prevent this by first checking whether the director's runningScene is identical to this (as in: the SecondScene instance), and only then call popScene.
I have same problem too, but I solved,
I think you may have removed all children from the scene,
check you onExit or destructor to see if any release/remove options available in these two functions.
if the scene has no children, it would be black.
I don't get why this code is incorrect...
DeadPanelLayer* deadPanelLayer = [DeadPanelLayer node];
[(CCScene*)self.parent addChild:deadPanelLayer z:2];
DeadPanel is a layer that I want to add on the Scene running from another layer
any idea why my layer is not loaded?
Edit: the game is not crashing, but the new layer does't show up. I tried to even move the objects on the scene and layer just to make sure it is not displaying due zindex hierarchic
but still...
I tried to add a method on Scene to add the layer in case it is called from parent:
[(GameScene*)self.parent showDeadPanel];
and even get current scene from Director
[[CCDirector sharedDirector] runningScene]
and it doesn't work either
Evening all,
I'll start this question in the time honoured tradition by saying that I've had a good old search on SO and also in the wider world but I'm not quite getting my head around this...
I've implemented a sneakyJoystick which works wonderfully (It moves my sprite around quite happily) however I've now done myself a mischief in thinking about it's positioning.
What I'd like to do is simply change it's position to a touch location and have it move my sprite around but this seems to be out of my knowledge pool. I might be being an idiot but I cannot work it out.
The touch events are already sorted in the sneakyjoystick classes (available on github https://github.com/0xPr0xy/sneaky-joystick-cocos2d). At the moment if I create the joystick during the init method in a class called controlsLayer then everything works fine; Joystick appears and it allows me to move to sprite around
-(id) init
{
if( (self=[super init]) ) {
myJoystickBase = [[[SneakyJoystickSkinnedBase alloc] init] autorelease];
myJoystickBase.backgroundSprite = [CCSprite spriteWithFile:#"dpad.png"];
myJoystickBase.thumbSprite = [CCSprite spriteWithFile:#"joystick.png"];
myJoystickBase.joystick = [[SneakyJoystick alloc] initWithRect:CGRectMake(0, 0, 128, 128)];
myJoystickBase.position = ccp(64, 64);
myJoystickBase.backgroundSprite.opacity = 100;
myJoystickBase.thumbSprite.opacity = 100;
[self addChild:myJoystickBase];
myJoystick = [myJoystickBase.joystick retain];
[self scheduleUpdate];
}
return self;
}
So to begin with I thought about looking at how I could simply get it to show and hide itself and set it's location. To do this I created a ccTouchesbegan method which contains pretty much the same code as my init method did before. This works fine up to a point (the joystick appears centred wherever I touch) but the issue now is that I cannot interact with it. The joystick appears where i want but it will not recognise my movements (the stick on the joystick does not move which in turn means that my sprite is not being told to move either)
if( (self=[super init]) ) {
self.isTouchEnabled = YES;
[self scheduleUpdate];
}
return self;
}
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
location = [self convertToNodeSpace:location];
myJoystickBase = [[[SneakyJoystickSkinnedBase alloc] init] autorelease];
myJoystickBase.backgroundSprite = [CCSprite spriteWithFile:#"dpad.png"];
myJoystickBase.thumbSprite = [CCSprite spriteWithFile:#"joystick.png"];
myJoystickBase.joystick = [[SneakyJoystick alloc] initWithRect:CGRectMake(0, 0, 128, 128)];
myJoystickBase.position = location;
myJoystickBase.backgroundSprite.opacity = 100;
myJoystickBase.thumbSprite.opacity = 100;
[self addChild:myJoystickBase];
myJoystick = [myJoystickBase.joystick retain];
}
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self removeChild:myJoystickBase cleanup:YES];
}
So to my uninitiated brain this is saying that when I touch the controlsLayer class it happily does what I've asked it to but it will not then pass this touch to other classes.
In a nutshell can a touch event be passed to multiple classes at the same time? The sneaky joystick class uses CCTargetedTouchDelegate which worked fine when it was the only thing looking for a touch. However now that I've added a -(void)ccTouchesBegan: in another class it's not happy.
Can anyone tell me if the problem is with the way I'm handling touches or is it possibly an issue with the way that I allocate the joystick in the touch method? Should I be allocating the joystick in the init method and be doing something else in the touchesBegan method? Trial and error isn't getting me anywhere useful at the moment. Feel like I'm banging my head against a brick wall. I'm happy to post up the full class files if necessary. Does this make sense?
The problem is that you create the joystick during touch began, and remove it on touch ended. This does not allow the joystick to receive a touch began event, since the event is already being processed at the time the joystick is being created.
You're also leaking the joystick due to the unnecessary retain (use ARC, please!).
Try creating the joystick in init, disable & hide it until a touch began event is received. This is also faster than recreating the joystick on every touch.
Also, should you have multiple touches enabled, keep in mind that you can receive up to 5 touch began events (5 fingers) without any touch ended in between. That would create 5 joysticks, but only remove one! Every time!
I don't know if this is possible, but I would like to create one big texture atlas and use it on all classes of the application.
Can one CCSpriteBatchNode be used for multiple classes?
Suppose I create this on the main class
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"atlasGeral.plist"];
self.batchNodeGeneral = [CCSpriteBatchNode batchNodeWithFile:#"atlasGeral.png"];
[self addChild:self.batchNodeGeneral];
and I have another class creating CCLayers on the main class, that is initialized, before using CCSpriteBatchNode, like this:
-(id) init
{
if( (self=[super init])) {
self.bg = [CCSprite spriteWithFile: #"cCircularBG.png"];
[self addChild:self.bg];
self.dr = [CCSprite spriteWithFile: #"cCircularDR.png"];
[self addChild:self.dr];
}
return self; // self is a CCLayer
}
can this be optimized using the self.batchNodeGeneral from the main class? My idea is to replace these two sprites and others with something like [CCSprite spriteWithSpriteFrameName:...
thanks
I'm not entirely sure I follow, but I'm pretty sure the answer is yes.
CCSpriteBatchNode doesn't have anything to do with classes, it has to do with assets. The important restriction on the use of batch nodes is that every sprite in the batch needs to reference the same texture atlas. So it is perfectly fine to have one batch node for your entire application and have every gameplay class add its own sprites to that batch. This can turn into a practical issue if your texture atlas becomes larger than the maximum texture size on your target hardware (see iOS device specs for details), but if you still want to have global batches and lots of assets it's not too hard to create a batch pool indexed by texture ID, create one batch node per atlas, and whenever you create a new sprite add it to the appropriate batch.
Honestly, I feel like the whole batch node thing is a terrible kludge on Cocos2D's part that could be made almost completely transparent to developers while still retaining its efficiency, but maybe this opinion is not entirely fair since I haven't dug around in the rendering code to understand their motivations. I guess it would mess with expectations of how depth sorting works, etc., but still I don't understand why batching objects for render is made the programmer's responsibility, it should be done by the engine.
Edit to add possible solution:
-(id) initWithMainClass:(MainClass*)mc
{
if( (self=[super init])) {
self.bg = [CCSprite spriteWithSpriteFrameName: #"cCircularBG.png"];
[mc.batchNodeGeneral addChild:self.bg];
self.dr = [CCSprite spriteWithSpriteFrameName: #"cCircularDR.png"];
[mc.batchNodeGeneral addChild:self.dr];
}
return self; // self is a CCLayer
}
`
So when you initialize one of the other classes, you pass the main class instance as a parameter so you can access its fields. Or make the main/manager class a singleton or find another architecture suitable to your needs.
I was playing around with a Joystick moving a Sprite around in one layer. Now, according to best practice, the Joystick and the Sprite have to be in different Layers of the same scene. I have managed to separate these, yet I am now completely stuck having absolutely no clue whatsoever how to pass joystick commands from one layer to another? What is the recommended way to do this?
Scene
Game Play Layer
Sprite
Game Control Layer
Joystick
When the joystick is manipulated I need to pass this info to the GamePlayLayer to control the sprite.
Well, I got a great Cocos2d book, written by Rod Strougo and Ray Wenderlich, by the name "Learning Cocos2d". In their book, they implement a game, which has a joystick implemented and all, using your initial setup. The GamePlayLayer contains both the joyStick and the hero sprite. (See book page 40).
I don't believe they would use bad practices in their book, given they are very talented!
...
With that being said, I have possible solutions, if you wish to implement them on separate layers:
GameScene
|
|__>GameLayer
|
|__>ControlLayer
That's your basic setup. But, intuitively, what is the purpose of the control layer? Control the game layer's content! So, I would suggest you hold a (weak) reference to the GameLayer within the ControlLayer. That way, using a simple:
#property (nonatomic, assign) CCSprite* hero;
you now have access to the hero from the ControlLayer!
Extra (if needed):
//GameScene init:
- (id)init {
....
gameLayer = [GameLayer node];
controlLayer = [ControlLayer node];
[controlLayer setGameLayerRef:gameLayer];
...
}
// Control layer:
#property (nonatomic, assign) GameLayer* gameLayerRef;
Even though I just suggested that way, I don't use it in my games :)
What I normally do is:
Make the GameScene class a "Semi-Singleton". (I learned this method from "Learn iPhone and iPad Game Dev" By Itterheim (aka gaming horror, Kobold2d publisher ... etc).
Then, inside the control layer, I would call the GameScene object:
[[GameScene sharedScene] doSomethingToTheGameLayer];
Yeah, the gameScene has simplistic methods that just relies what the control need to update in the game layer.
Edit:
Implementing the Semi-singleton pattern, as described by Itterheim in his book.
But, what is semi-singleton?
It has the singleton pattern's property: you can access the object instance from anywhere using a static call.
[GameScene sharedScene];
However, singleton objects are usually retained, after being created for the first time, till the end of the application's life. In the Semi-singleton pattern, this is not the case.
Once you create the instance, you cannot create another instance before destroying the old one, BUT once you are done with the instance, you destroy it (dealloc). Creating another one when necessary.
Recap:
Semi-Singleton: Create many object from it, but only one at any given time. Only recreate after destroying the old.
Implementation:
Of course, as you do with any singleton class, you first declare a static variable of the same type of the class:
//In GameScene.m
static GameScene* instance = nil;
#implementation
//Here we have the static method to access the instance:
+(GameScene*)sharedScene {
//you must have created one before trying to access it "Globally".
///Typically, created where you transition the GameScene using CCDirector's replaceScene.
NSAssert(instance != nil, #"GameScene not instantiated!!");
return instance;
}
-(id)init {
if((self = [super init])) {
//Make sure you don't have another instance of the class created
//NOTE: Possible race condition that I wouldn't worry about, since it should never happen.
NSAssert(instance == nil, #"Another instance is already created!!");
instance = self;
...
}
return self;
}
//Don't forget!! Dealloc!!
- (void)dealloc {
//the only instance we had is dealloc'd! We are back to zero. Can create a new one later! :D
instance = nil;
[super dealloc];
}
Edit2:
So, the timeline:
CCScene* scene = [GameScene node];
[[CCDirector sharedDirector] replaceScene:scene];
...
[[GameScene sharedScene] doSomething];
...
[[CCDirector sharedDirector] replaceScene:otherScene];
//After the new scene replaces GameScene, dealloc will be called, implicitly. Making instance = nil;
instance = nil;
[super dealloc];
...
//Loop again
CCScene* scene = [GameScene node];
[[CCDirector sharedDirector] replaceScene:scene];
...