Reputation: 1862
I have a cocos2d layer named ChoosePlayer
and in init
method I am adding a few sprite using [self addChild:]
. Its plain and works right. But when I try to do the same in another method as given below, its not working:
-(void) avatarchanged {
[self addChild:[CCSprite spriteWithFile:@"av1.png"]];
[self runAction:[CCMoveBy actionWithDuration:1.0 position:ccp(100, 100)]];
NSLog(@"added new avatar");
}
The [self runAction:]
is also not responding. So I guess its not the problem with sprite, but with the self
itself.
In between the init
and avatarchanged
, what I am doing is showing a UIView on top of openGL View, perform some actions there and returning back as follows:
-(void) selectAvatar {
CGSize winSize = [CCDirector sharedDirector].winSize;
flowCoverView = [[[FlowCoverView alloc] initWithFrame: CGRectMake(0, 0, 480, 320)] autorelease];
flowCoverView.center = ccp(-80 + winSize.width / 2, 80 + winSize.height / 2);
flowCoverView.delegate = self;
flowCoverView.transform = CGAffineTransformMakeRotation(90*(3.14/180));
[[CCDirector sharedDirector].openGLView.window addSubview:flowCoverView];
}
When the necessary actions are performed, flowCoverView is removed as follows:
- (void)flowCover:(FlowCoverView *)view didSelect:(int)cover {
selectedavat = cover;
[flowCoverView removeFromSuperview];
[[NSNotificationCenter defaultCenter] postNotificationName:@"avatarchanged" object:nil];
}
The notification posted above invoked my avatarchanged
method, where the self
is not responding.
Edit: here is my init
method:
-(id) init {
if( (self=[super init])) {
self.isTouchEnabled = YES;
BG = [CCSprite spriteWithFile:@"opponent.jpg"];
BG.scale *= CC_CONTENT_SCALE_FACTOR() * 1;
BG.position = ccp(240,160);
[self addChild:BG];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(avatarchanged) name:@"avatarchanged" object:nil];
}
return self;
}
Note: In my project there is a bunch of global variables declared using extern
, they might something to do with my problem, but am not sure.
Could someone please help me with this?
Edit 2:
changed avatarchanged
as follows:
-(void) avatarchanged {
if (self == nil) {
NSLog(@"self is nil!!!!!!!!");
} else {
NSLog(@"pheww.. its not nil");
}
if (self.isRunning) {
NSLog(@"running");
} else {
NSLog(@"not running");
}
[BG runAction:[CCRotateBy actionWithDuration:1.0 angle:100.0]];
[self addChild:[CCSprite spriteWithFile:@"av1.png"]];
NSLog(@"added new avatar");
[self runAction:[CCMoveBy actionWithDuration:1.0 position:ccp(100, 100)]];
}
log shows as
2012-03-26 11:16:21.213 Funsip[1550:207] pheww.. its not nil
2012-03-26 11:16:21.214 Funsip[1550:207] running
2012-03-26 11:16:21.224 Funsip[1550:207] added new avatar
the BG's runAction is also not getting applied, but doing the same in init
method works perfectly right.
Edit 3: The FlowCoverView that I add is implemented with OpenGL calls internally. May be it could be causing conflicts with OpenGL states setup in cocos2d. But I dont know OpenGL to look for these sort of issues. Here is the link to the page where I took the flowcoverview from http://www.chaosinmotion.com/flowcover.html
Upvotes: 0
Views: 206
Reputation: 1138
This might help: http://www.cocos2d-iphone.org/forum/topic/28056
Upvotes: 1
Reputation: 9079
Is 'self' in running mode (self.isRunning) ? if not nothing much will happen from a cocos2d point of view. The isRunning mode is achieved when you add the ChoosePlayer instance to a running CCNode descendant. If you forgot to add it to a running node, it will be ignored in draws, actions, etc ...
Upvotes: 2