我在一个故事板项目中集成了一个Cocos2d游戏。以下是故事板。当游戏结束时(onExit
方法),我会向CocosViewController
发送通知并从那里执行一个segue,这会打开ScoreViewController
。这工作正常。此外,当用户按下ScoreViewController
中的"再次播放" 按钮时,我想再次加载游戏。
我尝试使用展开的segue并打开CocosViewController
但它没有加载游戏,它会立即返回ScoreViewController
。如何实现Play Again功能?或许有一个segue?
CocosViewController
-(void)setupCocos2d {
CCDirector *director = [CCDirector sharedDirector];
//[[[self navigationController] navigationBar] setHidden:YES];
if([director isViewLoaded] == NO)
{
// Create the OpenGL view that Cocos2D will render to.
CCGLView *glView = [CCGLView viewWithFrame:self.view.bounds
pixelFormat:kEAGLColorFormatRGB565
depthFormat:0
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];
// Assign the view to the director.
director.view = glView;
// Initialize other director settings.
[director setAnimationInterval:1.0f/60.0f];
[director enableRetinaDisplay:YES];
}
// Set the view controller as the director's delegate, so we can respond to certain events.
director.delegate = self;
// Add the director as a child view controller of this view controller.
[self addChildViewController:director];
// Add the director's OpenGL view as a subview so we can see it.
[self.view addSubview:director.view];
[self.view sendSubviewToBack:director.view];
// Finish up our view controller containment responsibilities.
[director didMoveToParentViewController:self];
// Run whatever scene we'd like to run here.
NSArray *parameters = [[NSArray alloc] initWithObjects:@"3", @"sound", @"countdown", nil];
if(director.runningScene)
[director replaceScene:[IntroLayer sceneWithParameters:parameters]];
else
[director pushScene:[IntroLayer sceneWithParameters:parameters]];
}
-(void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self setupCocos2d];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotificationFromGame:) name:@"GameEndedNotification" object:nil];
}
-(void)receiveNotificationFromGame:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"GameEndedNotification"])
{
NSLog (@"Successfully received the test notification! %@", [self class]);
NSLog(@"score: %d", [notification.userInfo[@"score"] intValue]);
//get variables
score = [notification.userInfo[@"score"] intValue];
gameTracking = notification.userInfo[@"gameTracking"];
//show the next view
[self performSegueWithIdentifier:@"sg_showScore" sender:self];
}
}
- (IBAction)unwindToCocosViewController:(UIStoryboardSegue *)sender {
//nothing goes there
}
这是我初始化cocos2d游戏的方式:
-(id) initWithParameters:(NSArray *) parameters
{
NSLog(@"init TheButton with parameters");
// Apple recommend assigning self with supers return value
self = [super init];
if (!self) return(nil);
gameParameters = parameters;
// Enable touch handling on scene node
[self setIsTouchEnabled:YES];
// Create a colored background (Dark Grey)
//CCNodeColor *background = [CCNodeColor nodeWithColor:[CCColor colorWithRed:0.6f green:0.6f blue:0.6f alpha:1.0f]];
//[self addChild:background];
CCMenuItem *starMenuItem = [CCMenuItemImage
itemFromNormalImage:@"ButtonStar.png" selectedImage:@"ButtonStarSel.png"
target:self selector:@selector(increasePoints:)];
CGSize size = [[CCDirector sharedDirector] winSize];
starMenuItem.anchorPoint = ccp(0.5f,0.5f);
starMenuItem.position = ccp(size.width/2, size.height/2);
CCMenu *starMenu = [CCMenu menuWithItems:starMenuItem, nil];
starMenu.position = CGPointZero;
[self addChild:starMenu];
timeLabel = [CCLabelTTF labelWithString:@"0.00" fontName:@"Arial" fontSize:18.0f];
//timeLabel.positionType = CCPositionTypeNormalized;
timeLabel.position = ccp(30,size.height-10);
// Add the time label
[self addChild:timeLabel];
NSLog(@"time: %@", [gameParameters objectAtIndex:0]);
clicksLabel = [CCLabelTTF labelWithString:@"0" fontName:@"Arial" fontSize:18.0f];
//clicksLabel.positionType = CCPositionTypeNormalized;
clicksLabel.position = ccp(size.width - 20,size.height-10);
// Add the time label
[self addChild:clicksLabel];
[self startGame];
return self;
}
-(void) startGame
{
clicks = 0;
myTime = [[gameParameters objectAtIndex:0] intValue];
[self schedule:@selector(update:)];
}
答案 0 :(得分:0)
您不需要在-setupCocos2d
中重新添加glView,因为它已经添加了。我不确定这是否是行为错误的唯一原因,但是尝试将此方法从viewDidAppear
调用到-viewDidLoad
,并且从ScoreViewController
返回时,只需重置您的Cocos2d引擎(例如清除所有标签或移除敌人)而不重新创建视图。
但是,你的方法不是很好,我强烈建议你用Cocos2d制作所有场景,而不是UIViewControllers。场景之间也有很好的过渡(至少在Cocos2d 3.0中)。