[英]Sprite Kit: How to add scene on top of running scene
So I'm working on porting my cocos2d game to Sprite Kit. 因此,我正在努力将cocos2d游戏移植到Sprite Kit。
In cocos2d I had 2 situations where I would overlay a menu over the entire game scene. 在cocos2d中,我有2种情况需要在整个游戏场景上覆盖一个菜单。 The first case is pause, and the second case is game over. 第一种情况是暂停,第二种情况是游戏结束。
In cocos2d for the game over I did 在游戏的cocos2d中,我做到了
CCScene *runningScene = [[CCDirector sharedDirector] runningScene];
WaveEndLayer *waveEndedLayer = [[WaveEndLayer alloc] initWithWon:didWin];
[waveEndedLayer setOpacity:0];
[runningScene addChild:waveEndedLayer z:kZHUD];
CCFadeTo *fadeIn = [CCFadeTo actionWithDuration:0.5 opacity:255];
[waveEndedLayer runAction:fadeIn];
The WaveEndLayer class had some CCMenuItems on it, and cocos2d handled passing the touch events to their handlers. WaveEndLayer类上有一些CCMenuItems,而cocos2d处理了将触摸事件传递给其处理程序的过程。
What is the proper way of doing something like this in sprite kit? 在Sprite Kit中做这样的事情的正确方法是什么?
What should my game over class inherit from ( SKNode
, SKView
, SKScene
)? 我的课堂游戏应该继承什么( SKNode
, SKView
, SKScene
)?
I tried using an SKScene
, but you can't have 2 running scenes (or am I wrong?), so I was adding the scene as a child to the main game scene. 我尝试使用SKScene
,但是您不能有2个正在运行的场景(或者我错了吗?),因此我将场景作为儿童添加到了主游戏场景中。 But the touch events were only getting called on the main game scene, not on the game over scene. 但是触摸事件只是在主游戏场景中被调用,而不是在游戏上方场景中被调用。 So I tried something like 所以我尝试了类似
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
if (_gameOver)
{
[_gameOverScene touchAtLocation:location];
}
}
But for simplicity purposes I'm not resetting the state of everything when I restart a level, I'm just pushing the game scene again, which it didn't like since it was technically still running. 但是为了简单起见,我不会在重新启动关卡时重置所有状态,只是在重新推动游戏场景,这在技术上仍在运行,因此不希望如此。
Anyways, whats the proper way of overlaying a menu that needs touch events? 无论如何,叠加需要触摸事件的菜单的正确方法是什么?
So I ended up using an SKNode for my game over. 因此,我最终在游戏中使用了SKNode。
I add the layer as a child to my game scene in its init
method. 我通过其init
方法将图层作为孩子添加到游戏场景中。
_gameOverLayer = [[GameOverLayer alloc] initWithSize:size];
_gameOverLayer.alpha = 0;
[self addChild:_gameOverLayer];
My game over method looks like this: 我的游戏结束方法如下所示:
- (void)gameOver
{
_gameOver = YES;
SKAction *fade = [SKAction fadeInWithDuration:0.5];
[_gameOverLayer runAction:fade];
}
And then I changed my touch method on my game scene to have this: 然后,我在游戏场景中更改了触摸方式,使其具有以下功能:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
if (_gameOver)
{
[_gameOverLayer touchesEnded:touches withEvent:event];
return;
}
...
}
Everything works perfectly, the game over appears on top of the running game, and can get input. 一切运行正常,游戏结束出现在正在运行的游戏的顶部,并且可以获取输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.