我在故事板項目中集成了Cocos2d遊戲。以下是故事板。當遊戲結束時(onExit
方法),我發送通知到CocosViewController
並從那裏執行一個繼續,這會打開ScoreViewController
。這工作正常。此外,當用戶按下中的「再次播放」按鈕時,我想再次加載遊戲。Cocos2D +故事板,實現Play Again功能並重新加載遊戲場景
我嘗試了一個unwind segue,它打開了CocosViewController
,但它沒有加載遊戲,它立即返回到ScoreViewController
。如何實現Play Again功能?也許是賽格?
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:)];
}
我把該方法放在viewDidLoad中,但現在場景是肖像而不是風景。另外,我如何重置Cocos2d引擎?它加載場景,但倒計時爲0,完成時得分相同。 – nabrugir
@nabrugir我的意思是「重置引擎」 - 將分數設置爲0,從一開始就開始倒計時以及在遊戲開始之前完成的所有事情。不知道爲什麼方向改變了。 – medvedNick
從哪裏可以做到這一點?從cocosviewcontroller或從遊戲?你能舉個例子嗎? – nabrugir