ObjC:无法从外部访问自定义类的ivar

时间:2012-05-14 09:56:00

标签: objective-c xcode cocos2d-iphone instance-variables undeclared-identifier

我正在尝试用cocos2d做一个基本的游戏,我已经到了我试图根据英雄精灵的位置滚动背景的地步。英雄与名为GameplayLayer的类中的控件一起创建,所有这些都适用于非滚动背景。

我正在关注的这本书给了我一些示例代码,以便根据我的角色通过中间点来实现滚动工作,这看起来很完美,但它没有执行,我相信这是因为它正在创建另一个实例我的Hero课程,并且该实例与屏幕上显示的实例之间没有任何关联。

我假设我可以通过在背景滚动类(称为YrrasCoastBackgroundLayer)中访问我的英雄的工作实例来解决这个问题,但是我有一个脑块而无法得到它看见。我已经尝试了@property,但Xcode只是不会在其他类中看到它。

这是来自GameplayLayer类的.h文件,其中英雄被创建/连接到控件等:

#import "CCLayer.h"
#import "cocos2d.h"
#import "CommonProtocols.h"
#import "SneakyJoystick.h"
#import "SneakyButton.h"
#import "SneakyJoystickSkinnedBase.h"
#import "SneakyButtonSkinnedBase.h"
#import "HeroMale.h"
#import "GameCharacter.h"

@interface GameplayLayer : CCLayer <GameplayLayerDelegate> {
    CCSprite *playerSprite;
    SneakyJoystick *playerJoystick;
    SneakyButton *jumpButton;  
    CCSpriteBatchNode *sceneSpriteBatchNode;
    HeroMale *heroMale;
}

@property (nonatomic, readonly) SneakyJoystick *playerJoystick;
@property (nonatomic, readonly) SneakyButton *jumpButton;
@property (nonatomic, assign) HeroMale *heroMale;

@end

YrrasCoastBackgroundLayer.h文件肯定会导入GameplayLayer.h,这里是YrrasCoastBackgroundLayer.m文件中的方法内容,我希望能够访问* heroMale ivar:

- (void) adjustLayer {
    float heroXPosition = heroMale.position.x;
    CGSize screenSize = [[CCDirector sharedDirector] winSize];
    float halfOfTheScreen = screenSize.width / 2.0f;
    CGSize levelSize = [[GameManager sharedGameManager] getDimensionsOfCurrentScene];

    if ((heroXPosition > halfOfTheScreen) && (heroXPosition < (levelSize.width - halfOfTheScreen))) {
        float newXPosition = halfOfTheScreen - heroXPosition;
        [self setPosition:ccp(newXPosition, self.position.y)];
    }
}

我在float heroXPosition = heroMale.position.x行上收到Xcode错误,指出heroMale是未声明的标识符。我怎样才能在这里使用它,甚至可以解决问题呢?

更新:

这是YrrasCoastBackgroundLayer的.h文件:

#import "CCLayer.h"
#import "cocos2d.h"
#import "HeroMale.h"
#import "GameplayLayer.h"

@interface YrrasCoastBackgroundLayer : CCLayer {
    // Web Tutorial
    CCParallaxNode *backgroundNode;
    CCSprite *backgroundImage;

    // Book Tutorial
    CCSpriteBatchNode *sceneSpriteBatchNode;
    CCParallaxNode *parallaxNode;
}

@property (nonatomic, assign) GameplayLayer *gameplayLayer;

@end

这是YrrasCoastBackgroundLayer.m:

#import "YrrasCoastBackgroundLayer.h"

@implementation YrrasCoastBackgroundLayer

@synthesize gameplayLayer;

- (id) init {
    self = [super init];

if (self != nil) {

    // Web Tutorial
    backgroundNode = [CCParallaxNode node];
    [self addChild:backgroundNode z: -1];

    backgroundImage = [CCSprite spriteWithFile:@"yrras-coast-ipad-hd.png"];

    CGPoint dustSpeed = ccp(0.1, 0.1);

    [backgroundNode addChild:backgroundImage z:0 parallaxRatio:dustSpeed positionOffset:ccp(1024, [[CCDirector sharedDirector]winSize].height / 2)];

    self.gameplayLayer = gameplayLayer;

    [self scheduleUpdate];

    }
    return self;
}

- (void)update:(ccTime)deltaTime {

    // Web Tutorial
    // CGPoint backgroundScrollVel = ccp(-1000, 0);
    // backgroundNode.position = ccpAdd(backgroundNode.position, ccpMult(backgroundScrollVel, deltaTime));

    CCArray *listOfGameObjects = [sceneSpriteBatchNode children];
    for (GameCharacter *tempChar in listOfGameObjects) {
        [tempChar updateStateWithDeltaTime:deltaTime andListOfGameObjects:listOfGameObjects];
    }
    [self adjustLayer];

}

// Book Tutorial
- (void) adjustLayer {
    float heroXPosition = gameplayLayer.heroMale.position.x;
    CCLOG(@"heroXPosition is %f", gameplayLayer.heroMale.position.x);
    CGSize screenSize = [[CCDirector sharedDirector] winSize];
    float halfOfTheScreen = screenSize.width / 2.0f;
    CGSize levelSize = [[GameManager sharedGameManager] getDimensionsOfCurrentScene];
    if ((heroXPosition > halfOfTheScreen) && (heroXPosition < (levelSize.width - halfOfTheScreen))) {
        float newXPosition = halfOfTheScreen - heroXPosition;
        [self setPosition:ccp(newXPosition, self.position.y)];
    }
}

@end

2 个答案:

答案 0 :(得分:1)

您需要将GameplayLayer的实例传递到YrrasCoastBackgroundLayer图层。

您可以在YrrasCoastBackgroundLayer中声明一个可分配的属性:

@property (nonatomic, assign) GameplayLayer gamplayLayer;

当您初始化YrrasCoastBackgroundLayer传递游戏播放图层的实例并在您的adjustLayer方法中执行:

float heroXPosition = gameplayLayer.heroMale.position.x;

编辑:

假设这是你创建场景的方式:

  MyScene *scene = [MyScene scene];
  gameplayLayer = [[GameplayLayer alloc] init];
  bgLayer = [[YrrasCoastBackgroundLayer alloc] init];
  bgLayer.gameplayLayer = gameplayLayer; // This is where you assign the gameplay Layer to the background layer;

  [scene addChild:bgLayer];
  [scene addChild:gameplayLayer];

  // Release both layers if you dont need them anymore here (scene addChild retains them when they are added)      
  [gameplayLayer release];
  [bgLayer release];

答案 1 :(得分:0)

我不知道你怎么能访问heroMale,因为它不是YrrasCoastBackgroundLayer的成员,而是GameplayLayer的成员。您必须拥有GameplayLayer类的对象/实例才能访问heroMale。这就是你得到未声明标识符的原因(heroMale不是类YrrasCoastBackgroundLayer的属性)。

亲切的问候,