2012-03-25 67 views
0

我有一個名爲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本身。

initavatarchanged之間,我做的是顯示對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

+0

其中' - (無效)avatarchanged'聲明? – giorashc 2012-03-25 09:07:48

+0

你的意思是「它不工作」和「也沒有迴應」? NSLog是否發生? – yuji 2012-03-25 09:09:06

+0

你可以顯示你的init方法嗎? – 2012-03-25 09:09:17

回答

2

流程模式(self.isRunning)的'self'的頁面的鏈接?如果不是從cocos2d的角度來看沒有什麼會發生。當您將ChoosePlayer實例添加到正在運行的CCNode後代時,可以實現isRunning模式。如果你忘了將它添加到正在運行的節點,它會在被忽略平局,行動等......

+0

很棒的線索,我確定'isRunning'會是假的,但可悲的是它不是.. !!! – saiy2k 2012-03-26 05:41:32

1
+0

thx爲鏈接...我havnt在我的班級實施'onExit'和'onEnter',但現在我實現了與相應的'超級'調用,並嘗試過,仍然沒有工作.. 和正如在說我發表評論出我的'self.isTouchEnabled =是;'和觸摸方法,然後也不工作..! – saiy2k 2012-03-27 05:13:04