CCActionCallBlock *call=[CCActionCallBlock actionWithBlock:^
{
NSLog(#"***********done");
}];
[self runAction:call];
Is not working if i put it in another node, and add this node to my main scene .
It only works in my main scene, and not in any other layer added to that scene .
why ?
I just tried it and it works for me, have you called [super onEnter] on your scene?
If that does not work please a bit more code please.
Related
I have a CCLayer class imported onto a game layer
strangely all the the sprites have "artefacts" that seem to appear from nowhere since I have checked and re-exported all of the files
Is some setting or something else that could cause this to happen?
I'm new at this
but I have checked so far:
set to PixelFormat_RGBA8888
PVRImagesHavePremultipliedAlpha:YES
png's are clear from artefact (28bit with transparency)
Textures are made with texture packer with "pre-multiplied"
The Background is a CCLayer
The Mine is a CCLayer
both are added to the game layer (cclayer also) as "addChild"
backgroundManager = [[BackGround alloc] init];
[self addChild:backgroundManager z:0];
myShip = [[Ship alloc]init];
[self addChild:myShip z:5];
Yes it was the settings in texture packer
after a few changes in the settings they now seem to load fine.
with no artifacts
use pre multiple
trim not crop
and give a little more inner padding
hope it helps someone else (since it was driving me a little crazy)
I am trying to take a particle effect in my Cocos2d project. Particle effect shows good. But I am confused when I put the particle showing function into a thread as follows, it only shows tiny dark squares instead of the right texture. thanx in advance.
// thread caller
[self performSelectorInBackground:#selector(showParticleThrd:) withObject:nil];
// it works good when i call it directly
-(void) showParticleThrd{
CCParticleSystem * system = [[ParticleEffectSelfMade alloc] initWithTotalParticles:1];
[aLayer removeChildByTag:R_TAG cleanup:YES];
system.position = ccp(self.position.x,self.position.y);
[self.parent addChild:system z:R_ORDER tag:R_TAG];
[system release];
}
You can not modify anything cocos2d related in a background thread. Cocos2d requires you to make changes to nodes in the main thread, where the OpenGL context was created.
I have several apps created with Cocos2D.
I am upgrading some of them to the new release 2.0 of Cocos2D. The method I am using is:
I create a new blank project from the standard template
I move all files to the new project
I generally keep the layer Helloworld as my initial Layer. I have noticed that Cocos2D creates an introLayer as the initial one and I have removed that and reverted the behavior to load Helloworld as before. Said that, I have noticed a few things:
The behavior inside Helloworld's init method is now different. I have noticed that the orientation is now reported incorrectly inside the init method. It will always report it as portrait even if the app is just landscape. What I have noticed also is that it is like the director is not fully initialized yet. This was confirmed if I try to use the director's view. This view will always come as not valid, if I try to use it inside Helloworld's init method.
To solve that, I have moved all code to Helloworld's onEnter method. Now, Helloworld and the director are full initialized and everything works.
This method worked for the first apps that I upgraded for Cocos2D 1.0 to 2.0, but something is wrong for the application I am converting now from Cocos2D 2.0beta to 2.0 final.
The problem I have now is that Helloworld is not triggering any touch and yes, I have this in place:
-(void) registerWithTouchDispatcher
{
[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
return YES;
}
What can be preventing Helloworld from detecting touches in 2.0? How can I make the init method of Helloworld to behave like in 2.0 beta or 1.0?
NOTE: ccTouchBegan and registerWithTouchDispatcher are never called when Helloworld loads.
thanks.
The rotation issue is a known bug. The very first scene will be initialized in portrait mode, even if the app is set to be in landscape only. That's because the init happens before the orientation has been updated by the app delegate. Putting the code in onEnter fixes that, or simply loading an "intro" scene that then loads the actual first scene fixes that as well.
The touch issue is probably not a problem of cocos2d 2.0. Maybe it's as simple as not setting self.isTouchEnabled = YES. If the registerWithTouchDispatcher method isn't called, then isTouchEnabled is not set to YES or you're not doing this in a CCLayer class but maybe CCScene.
The solution for this question is to put [super onEnter] on the onEnter method.
-(void) onEnter {
[super onEnter];
self.isTouchEnabled = YES;
}
the lack of this line was creating all kind of problems on the application, like scheduler not working, weird behavior of ccMove, ccTransition not triggering onEnterTransitionDidFinish and all kind of problems.
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.
i am doing something wrong, maybe someone could help me .
when app is start i add a sprite as a child,from DB like this :
b_pic=[CCSprite spriteWithFile:basic_pic];
b_pic.position=ccp(160,175);
[self addChild:b_pic];
then i do things,and run animation, so before animation starts, i remove the sprite with :
[b_pic.parent removeChild:b_pic cleanup:YES];
and then i am trying to add it back, BUT its crashes. i add it with :
b_pic=[CCSprite spriteWithFile:#"regular.png"];
b_pic.position=ccp(160,175);
[self addChild:b_pic];
what am i doing wrong here ?
i cant understand this child and parent thing.
i have also tried to remove the sprite with :
[self removeChild:b_pic cleanup:YES];
thanks a lot .
The sprite is an autorelease object in cocos2d. So when you remove the sprite the CleanUp should be NO like so...
[self removeChild:b_pic cleanup:NO];