愚蠢的问题。Cocos2d 是围绕父子层次结构构建的。我想知道是否可以有一个父类(例如GameScene)并使用指向父类成员(例如CCSpriteBatchNode *)的指针初始化一个子类(例如SpriteHandler)。
我正在尝试这样做以优化 CCSpriteBatchNodes 的数量。这是我的主类(GameScene 风格)的代码片段。
#import <Foundation/Foundation.h>
#import "cocos2d.h"
enum ShooterSceneLayerTags {
HudLayerTag = 0,
};
@interface ShooterScene : CCLayer {
CCSpriteBatchNode* sharedSpriteBatchNode;
}
-(id) initWithSharedBatchNodeReference:( CCSpriteBatchNode*) sharedSpriteBatchNode;
+ (id) sceneWithId:(int)sceneId;
@end
#import "ShooterScene.h"
#import "MainMenuScene.h"
//Layers
#import "LevelSpritesLayer.h"
#import "HudLayer.h"
@interface ShooterScene (PrivateMethods)
-(void) addLayers:(int)sceneId;
-(void) loadGameArtFile;
-(BOOL) verifyAndHandlePause;
@end
@implementation ShooterScene
+ (id) sceneWithId:(int)sceneId
{
CCScene *scene = [CCScene node];
ShooterScene * shooterLayer = [[self alloc] initWithId:sceneId];
[scene addChild:shooterLayer];
return scene;
}
-(id) initWithId:(int)sceneId
{
if ((self = [super init]))
{
//Load game art before adding layers - This will initialize the batch node
[self loadGameArtFile:sceneId];
//Will add sprites to shared batch node for performance
[self addLayers:sceneId];
[self addChild:sharedSpriteBatchNode];
//Do other stuff..
[self scheduleUpdate];
}
return self;
}
-(void) addLayers:(int)sceneId
{
LevelSpritesLayer * levelData = [LevelSpritesLayer node];
[levelData initWithSharedBatchNodeReference:sharedSpriteBatchNode];
[self addChild:levelData];
switch (sceneId) {
case 1:
[levelData loadLevelOneSprites];
break;
case 2:
[levelData loadLevelTwoSprites];
break;
default:
break;
}
HudLayer * hud = [HudLayer node];
[hud setUpPauseMenu];
[self addChild:hud z:1 tag:HudLayerTag];
}
-(BOOL) verifyAndHandlePause
{
HudLayer * hud = [self getChildByTag:HudLayerTag];
if(hud.pauseRequested){
[[CCDirector sharedDirector] replaceScene:[MainMenuScene scene]];
return true;
}
else {
return false;
}
}
-(void) update:(ccTime)delta
{
if([self verifyAndHandlePause]==false)
{
//Continue with animation etc..
}
}
/**
This is tricky. Could have loaded this in LevelData but as I am expecting to use the same SpriteSheet for HudLayer as well then
I prefer to have the control here of this. Also, the same sheet could be used for more level, hence specific function is not bad
**/
-(void) loadGameArtFile:(int) sceneId
{
CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
[frameCache addSpriteFramesWithFile:@"game-art-hd.plist"];
sharedSpriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:@"game-art-hd.png"];
}
//As dealloc is deprecated, I prefer to remove unused sprites and texture on cleanup
-(void) cleanup
{
[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
}
这里是 loadLevelData 方法之一,它使用 sharedSpriteBatchNodeReference
-(void) loadLevelOneSprites
{
//Just a proof of concept example
testSprite = [CCSprite spriteWithSpriteFrameName:@"File0.png"];
testSprite.anchorPoint = CGPointMake(0.5f, 0.5f);
testSprite.position = CGPointMake(160.0f, 240.0f);
[sharedSpriteBatchNodeReference addChild:testSprite];
}