我正在尝试在 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)];
}
}
我收到一条 Xcode 错误float heroXPosition = heroMale.position.x
,指出 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