问题描述
限时送ChatGPT账号..我用精灵套件创建了一个游戏,但在游戏启动的那一刻,它直接进入游戏.我如何实现不同的场景,如主菜单和游戏结束场景,并通过按下屏幕上的标签或游戏过程中的接触在它们之间进行转换.
I have created a game with sprite kit but at the moment when the game is launched, it goes straight to the game. How can i implement different scenes like a main menu and a game over scene and transition between them either by pressing a label on the screen or by a contact during the game.
推荐答案
您似乎没有查看 Apple 的文档.他们有一个很好的 SpriteKit 指南.此外,RayWenderlich 上 spriteKit 类别中的教程非常出色.我还没有对 Swift 进行太多阅读,但我的理解是 Swift 可以与 Objective-C 一起编写.话虽如此,我会给你一个起点(在 Objective-C 中).
It doesn't seem like you have looked into Apple's documentation. They have an excellent guide for SpriteKit. Also, the tutorials at RayWenderlich within the spriteKit category are phenomenal. I haven't read much into Swift yet, but it is my understanding that Swift can be written alongside Objective-C.That being said, I'll give you a starting point (in Objective-C) .
1) 调用初始场景:
在主视图控制器中,您需要将 VC 的 view
属性转换为 viewDidLoad
内的 SKView
,如下所示:
Within your main view controller, you will want to cast the VC's view
property to an SKView
within viewDidLoad
like so:
SKView *spriteKitView = (SKView *)self.view;
SKView
上的一些有用的调试属性:
some useful debugging properties on SKView
:
spriteKitView.showsFPS = YES;
spriteKitView.showsNodeCount = YES;
spriteKitView.showsPhysics = YES;
您必须分别配置每个场景(子类 SKScene
).完成此操作后,将主场景导入到主 VC 的类实现中.呈现主场景(仍在 viewDidLoad
中):
You will have to configure each scene separately (subclass SKScene
). After you have done this, import the main scene to your main VC's class implementation. To present the main scene (still in viewDidLoad
):
MyScene *mainScene = [MyScene new];
[spriteKitView presentScene: mainScene];
2) 通过标签(或任何其他 SKNode
)在场景之间转换:将相关节点设置为属性并通过以下方式呈现新场景:
2) transition between scene via label (or any other SKNode
) :
Set relevant nodes as properties and present a new scene via:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self];
if ([self.playAgainButton containsPoint:touchLocation]) {
[self presentGamePlayScene];
} else if([self.mainMenuButton containsPoint:touchLocation]) {
[self presentMainMenu];
}
}
- (void)presentGamePlayScene {
gamePlayScene *newGamePlay = [[gamePlayScene alloc]init];
newGamePlay.scaleMode = SKSceneScaleModeAspectFill;
SKTransition *arbitraryTransition = [SKTransition crossFadeWithDuration:2.0]; //look into SKTransition for a plethora of different options.
[self.view presentScene:newGamePlay transition:arbitraryTransition];
}
3) 要通过联系更改场景,请在任何给定的 SKScene
中遵守协议 SKPhysicsContactDelegate
.在该场景的实现中:
3) To change scenes via contact, conform to the protocol SKPhysicsContactDelegate
within any given SKScene
. Within that scene's implementation:
- (void)didBeginContact:(SKPhysicsContact *)contact {
if(contact.bodyA.categoryBitMask == enemyCategory && contact.bodyB.categoryBitMask == arbitraryNodeCategory) {
[self presentGameOverScene];
}
}
一个 SKPhysicsContact
有 2 个碰撞的 SKPhysicsBody
通过属性附加到它上面.这些碰撞节点必须初始化它们的 SKPhysicsBody
.此外,他们必须在他们的 SKPhysicsBody
上设置他们的 contactBitMask
属性,如下所示:
An SKPhysicsContact
has 2 colliding SKPhysicsBody
s attached to it via properties. These colliding nodes must have their SKPhysicsBody
's initialized. In addition, they must have their contactBitMask
property set on their SKPhysicsBody
like so:
@implementation SomeNode
- (instanceType)initCollidingNode {
if(self = [super init]) {
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.someSize];
self.physicsBody.contactBitMask = enemyCategory | powerUpCategory;
self.physicsBody.categoryBitMask = arbitraryNodeCategory;
self.physicsBody.dynamic = YES
}
return self;
}
SKSpriteNode 和 SKPhysicsBody 具有您可能需要的其他相关属性
这篇关于SpriteKit 如何创建和过渡到不同的场景(Swift 语言)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论