1
我有一個父類和一個子類。 GameObjectBase(父)GameObjectPlayer(子)。當我重寫Child類中的方法並調用它時使用錯誤的類目標C
[myPlayerClass showNextFrame]
它調用父類。原來在調試器中,我看到myPlayerClass確實是類類型的GameObjectBase(這是父類)怎麼回事?
GameObjectBase.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@class GameLayer;
@interface GameObjectBase : NSObject
{
/* CCSprite *gameObjectSprite; // Sprite representing this game object
GameLayer *parentGameLayer; */ // Reference of the game layer this object
// belongs to
}
@property (nonatomic, assign) CCSprite *gameObjectSprite;
@property (nonatomic, assign) GameLayer *parentGameLayer;
// Class method. Autorelease
+ (id) initWithGameLayer:(GameLayer *) gamelayer
imageFileName:(NSString *) fileName;
// "Virtual methods" that the derived class should implement.
// If not implemented, this method will be called and Assert game
- (void) update: (ccTime) dt;
- (void) showNextFrame;
@end
GameObjectPlayer.h
#import <Foundation/Foundation.h>
#import "GameObjectBase.h"
@interface GameObjectPlayer : GameObjectBase
{
int direction;
}
@property (nonatomic) int direction;
@end
GameLayer.h
#import "cocos2d.h"
#import "GameObjectPlayer.h"
@interface GameLayer : CCLayer
{
}
// returns a CCScene that contains the GameLayer as the only child
+(CCScene *) scene;
@property (nonatomic, strong) GameObjectPlayer *player;
@end
當我打電話的調試器是什麼類型的 「臨時」 檢查是在這個函數內GameLayer類,它給父類GameObjectBase而不是子類GameObjectPlayer
- (void) update:(ccTime) dt
{
GameObjectPlayer *temp = _player;
[temp showNextFrame];
}
你如何爲玩家設定價值?在上面的例子中無法看到代碼。由於temp =等同於玩家,我認爲問題出在您創建玩家的代碼上。 – iDev
你如何創建玩家對象? – LearnCocos2D