问题
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.
回答1:
It doesn't seem like you have looked into Apple's documentation. They have an excellent guide for SpriteKit. Also, the tutorials at RayWenderlich.com 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) Calling the initial scene:
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;
some useful debugging properties on SKView
:
spriteKitView.showsFPS = YES;
spriteKitView.showsNodeCount = YES;
spriteKitView.showsPhysics = YES;
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) 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) 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];
}
}
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 and SKPhysicsBody have other relevant properties you may need
回答2:
I solve this problem in swift.
func goToGameScene(){
let gameScene:GameScene = GameScene(size: self.size) // create your new scene
let transition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 0) // create type of transition (you can check in documentation for more transtions)
gameScene.scaleMode = SKSceneScaleMode.AspectFill
self.view!.presentScene(gameScene, transition: transition)
}
回答3:
Make a file called: TestScene.Swift
Declare it in your GameScene.Swift file and implement the touches function like so:
var sceneTest: TestScene!
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
// Configure the view.
let gameView = self.view as SKView!
/* Sprite Kit applies additional optimizations to improve rendering performance */
gameView.ignoresSiblingOrder = true
sceneTest = TestScene(size: gameView.bounds.size)
gameView.presentScene(sceneTest)
sceneTest.scaleMode = .AspectFill
}
This will present a new scene called TestScene
回答4:
Create a new swift file called MainMenu.swift
The following code placed in your current scene will allow a transition to the scene described in MainMenu.swift:
var scene = MainMenu(size: self.size)
let skView = self.view! as SKView
skView.ignoresSiblingOrder = true
scene.scaleMode = .ResizeFill
scene.size = skView.bounds.size
skView.presentScene(scene)
In the new scene, you can use the following code to get back to the scene described in GameScene.swift:
var scene = GameScene(size: self.size)
let skView = self.view! as SKView
skView.ignoresSiblingOrder = true
scene.scaleMode = .ResizeFill
scene.size = skView.bounds.size
skView.presentScene(scene)
来源:https://stackoverflow.com/questions/24230245/spritekit-how-to-create-and-transition-to-different-scenes-swift-language