1

我有一个父类和一个子类。GameObjectBase(父) GameObjectPlayer(子)。当我覆盖子类中的一个方法并使用它调用它时

[myPlayerClass showNextFrame]

它正在调用父类。原来在调试器中,我看到myPlayerClass确实是类类型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

游戏对象播放器.h

#import <Foundation/Foundation.h>
#import "GameObjectBase.h"
@interface GameObjectPlayer : GameObjectBase
{
    int direction;
}

@property (nonatomic) int direction;
@end

游戏层.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 类中此函数中的“temp”类型时,它给出了父类 GameObjectBase 而不是子类 GameObjectPlayer

- (void) update:(ccTime) dt
{
    GameObjectPlayer *temp = _player;

    [temp showNextFrame];

}
4

1 回答 1

0

您只是在分配值。请分配它然后分配它。

希望它能解决你的问题。

于 2012-10-30T04:58:33.360 回答