2012-10-10 69 views
1

我做了一個簡單的物理sprite,它會自動觸發更新選擇器,並相應地將sprite移動到b2body成員集。如何在cocos2d中正確地覆蓋超類更新方法

但是,當我做了物理sprite的子類,我想擴展該更新方法來做更多的動作。當我重寫這個更新方法時,我需要調用超類更新方法,以便在我在子類中完成業務之前完成業務。

下面是一些代碼:

超類:

/** 
* SELECTOR 
* This selector updates the sprites position 
*/ 
-(void) update:(ccTime) dt { 
    CCLOG(@"PSprite update"); 
    if (self.body) { 

     //Get the body's position in pixels 
     CGPoint newLocationForSprite = ccp(self.body->GetPosition().x * PTM_RATIO, self.body->GetPosition().y * PTM_RATIO); 
     //positioning self 
     [self setPosition:newLocationForSprite]; 
    } 
} 

子類:

-(void) update:(ccTime) dt { 
//I want to call my superclass's update method 
CCLOG(@"Subclass update"); 
} 

回答

4

子類代碼

- (void) update:(ccTime)dt 
{ 
    [super update: dt]; 

    // do anything you want 
} 
+0

看來,這不是一個公共方法,這它應該是,但對我的情況我需要它。太糟糕了,它們在Objective C中不受保護,我會接受這一點 – chrs

相關問題