我有一個名爲ChoosePlayer
的cocos2d圖層和init
方法,我使用[self addChild:]
添加了一些精靈。它的樸素和正確的作品。但是,當我嘗試下面給出這樣做的另一種方法,它不工作:Objective-C:`self` not responding
-(void) avatarchanged {
[self addChild:[CCSprite spriteWithFile:@"av1.png"]];
[self runAction:[CCMoveBy actionWithDuration:1.0 position:ccp(100, 100)]];
NSLog(@"added new avatar");
}
的[self runAction:]
也沒有響應。所以我猜想它不是精靈的問題,而是self
本身。
在init
和avatarchanged
之間,我做的是顯示對OpenGL視圖,頂一個UIView,執行某些操作那裏和返回回如下:
-(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];
}
當執行必要的行動,flowCoverView
- (void)flowCover:(FlowCoverView *)view didSelect:(int)cover {
selectedavat = cover;
[flowCoverView removeFromSuperview];
[[NSNotificationCenter defaultCenter] postNotificationName:@"avatarchanged" object:nil];
}
通知上面貼調用我avatarchanged
方法,其中所述self
沒有響應:如下被去除。
編輯:這裏是我的init
方法:
-(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;
}
注:在我的項目有一堆全局變量的使用extern
宣佈,他們可能是與我的問題,但我不知道。
有人可以幫助我嗎?
編輯2: 改變avatarchanged
如下:
-(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)]];
}
日誌顯示爲
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
的BG的runAction也沒有得到應用,但在init
方法做同樣的工作完全正確的。
編輯3: 我添加的FlowCoverView是在內部使用OpenGL調用實現的。可能會導致與cocos2d中的OpenGL狀態設置衝突。但我不知道OpenGL尋找這些問題。 這裏是我從http://www.chaosinmotion.com/flowcover.html
其中' - (無效)avatarchanged'聲明? – giorashc 2012-03-25 09:07:48
你的意思是「它不工作」和「也沒有迴應」? NSLog是否發生? – yuji 2012-03-25 09:09:06
你可以顯示你的init方法嗎? – 2012-03-25 09:09:17