Wrong class type in objective C

Posted by Max Hui on Stack Overflow See other posts from Stack Overflow or by Max Hui
Published on 2012-10-30T04:51:16Z Indexed on 2012/10/30 5:00 UTC
Read the original article Hit count: 117

Filed under:
|

I have a parent class and a child class. GameObjectBase (parent) GameObjectPlayer(child). When I override a method in Child class and call it using [myPlayerClass showNextFrame] It is calling the parent class one. It turns out in the debugger, I see the myPlayerClass was indeed class type GameObjectBase (which is the parent class) How come?

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

When I call examine in debugger what type "temp" is in this function inside GameLayer class, it's giving parent class GameObjectBase instead of subclass GameObjectPlayer

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

    [temp showNextFrame];

}

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocos2d-iphone