Changes

Jump to: navigation, search

An iOS 7 Sprite Kit Game Tutorial

2,950 bytes removed, 21:40, 12 December 2013
Transitioning to the Archery Scene
Compile and run the application. Once running, touch the screen and note that the label node fades away and the transition to the ArcheryScene takes effect. Once again, we are presented with a black scene that now needs to be implemented.
== Transitioning to the Archery Scene ==
Clearly having instructed the user to tap the screen in order to play the game, some code now needs to be written to make this happen. This behavior will be added by implementing the touchesBegan method in the WelcomeScene class. Rather than move directly to ArcheryScene, however, some effects will be added in the form of an action and a transition.
 
When implemented, the SKAction will cause the node to fade away from view whilst an SKTransition will be used to animate the transition from the current scene to the archery scene using a “doorway” style of animation. Implement these requirements by adding the following method to the WelcomeScene.m file:
 
<pre>
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SKNode *welcomeNode = [self childNodeWithName:@"welcomeNode"];
 
if (welcomeNode != nil)
{
SKAction *fadeAway = [SKAction fadeOutWithDuration:1.0];
 
[welcomeNode runAction:fadeAway completion:^{
SKScene *archeryScene =
[[ArcheryScene alloc]initWithSize:self.size];
 
SKTransition *doors =
[SKTransition doorwayWithDuration:1.0];
 
[self.view presentScene:archeryScene transition:doors];
}
];
}
}
</pre>
 
Before moving on to the next steps, we will take some time to provide more detail on the above code.
 
From within the context of the touchesBegan method we have no direct reference to the welcomeNode instance. We do know, however, that when it was created in the createWelcomeNode method that it was assigned the name “welcomeNode”. Using the childNodeWithName method of the scene instance, therefore, a reference to the node is being obtained within the touchesBegan method as follows:
 
<pre>
SKNode *welcomeNode = [self childNodeWithName:@"welcomeNode"];
</pre>
 
The code then checks that the node was found before creating a new SKAction instance configured to cause the node to fade from view over a one second duration:
 
<pre>
SKAction *fadeAway = [SKAction fadeOutWithDuration:1.0];
</pre>
 
The action is then executed on the welcomeNode. A completion block is also specified to be executed when the action completes. This block creates an instance of the ArcheryScene class and an appropriately configured SKTransition object. The transition to the new scene is then initiated:
 
<pre>
SKAction *fadeAway = [SKAction fadeOutWithDuration:1.0];
 
[welcomeNode runAction:fadeAway completion:^{
SKScene *archeryScene =
[[ArcheryScene alloc]initWithSize:self.size];
 
SKTransition *doors =
[SKTransition doorwayWithDuration:1.0];
 
[self.view presentScene:archeryScene transition:doors];
</pre>
 
Compile and run the application. Once running, touch the screen and note that the label node fades away and the transition to the ArcheryScene takes effect. Once again, we are presented with a black scene that now needs to be implemented.
== Preparing the Archery Scene ==

Navigation menu