我通過'學習的cocos2d的書的工作,和我卡上的一些基本的東西。調用子類方法 - cocos2d的
所以,那裏有一個父類:GameplayLayer。其中有一個'init'方法,在這裏創建一個主角的實例 - 'Viking'。 Viking是'GameCharacter'的一個子類,它是'GameObject'的子類。現在
#pragma mark INIT PLAYER
Viking *viking = [[Viking alloc]
initWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName:@"viking.png"]];
[viking setJoystick:leftJoystick];
[viking setFireButton:fireButton];
[viking setSecondaryButton:secondaryButton];
[viking setCollisionLayer:collidableLayer]; // send collision layer to player
[viking setPosition:ccp(screenSize.width * 0.35f, screenSize.height * 0.14f)];
[viking setHealth:100];
[sceneSpriteBatchNode addChild:viking z:1000 tag:kPlayerSpriteTagValue];
,海盜具有被稱爲由GameplayLayer每幀更新方法。它是父類,GameObject也有這種更新方法,如果它被意外調用會產生錯誤信息 - 「GameObject更新應該被覆蓋」。
所以在我的代碼中,我調用'Viking 「用下面的方法:
#pragma mark UPDATE_METHOD
-(void) update:(ccTime)deltaTime
{
CCArray *listOfGameObjects =
[sceneSpriteBatchNode children];
for (GameObject *tempChar in listOfGameObjects) {
CCLOG(@"engine found %@",tempChar);
[tempChar updateStateWithDeltaTime:deltaTime
andListOfGameObjects:listOfGameObjects];
}
}
所以,這應該叫‘’。在海盜的方法,但不知何故,它調用遊戲對象的父類的方法它說‘updateStateWithDeltaTime updatestate應該重寫’我如何重寫父類的方法?
非常感謝,
卡爾
嘿感謝@Dustin!我討厭說,但在我的情況下,這個錯誤竟然是一個全面的小學生錯誤。 Viking的更新方法在實例var中存在拼寫錯誤!衛生署!但是你給我提供了一些有用的語法,我將使用'isKindOfClass [Viking class]'!我的提示 - 調試時,不要忘記檢查明顯的! – CarlosTheJackal 2012-08-15 15:00:26
這是完全錯誤的。 「覆蓋」的要點是當在對象上調用方法時,調用最具體子類的方法。這與你所稱的表達式的靜態類型無關。 – newacct 2012-08-15 18:23:36
@newacct當我看到這個問題我思考的問題是,他調用'GameObject'時,他以爲他是在調用'Viking'。 OP說父方法被調用,如果他正確地調用了一個不會發生的子類的方法。事實證明,他的問題並非如此。隨意發佈您自己的答案或編輯我的問題,但問題已解決。 – Dustin 2012-08-15 18:27:35