Switch the scene of the IOS Sprite Kit tutorialin the Sprite kitSwitching scenes
Each scene does not exist alone. Players can switch from one scene to another. In this section, let's explain the scene switching. In each game will be used to the scene and the scene of the switch function, for example, in the arcade primitive game, select the level of the scene, you can see that there are 4 levels, the four represents 4 scenes, respectively, 2.20 shows.
Figure 2.20 Arcade Original Level selection
When the player chooses the first pass, the scene from the selection level jumps to the first off scene, as shown in 2.21.
Figure 2.21 Scene of the first pass
When the player chooses the second pass, the scene from the selection level jumps to the scene in the second pass, which is shown in 2.22.
Figure 2.22 The scene of the second pass
At this point, they use the scene and scene switching function, that is, when the player taps the corresponding scene, it will enter into the selected scene.
Sprite Kit.Simple scene Switching
Simple scene switching means that there is no transition effect when the scene and scene are switched. Need to use to Presentscene (_ Scene:skscene?) when switching Method.
"Example 2-7" below will be based on project 2-1, the realization after tapping the screen, the scene gamescene switch to Newgamescene function, with the following steps:
(1 Open the Gameviewcontroller.swift file, write the code in the Viewdidload () method, implement after running the program, the first scene to display is Gamescene, the code is as follows:
- Override Func Viewdidload () {
- Super.viewdidload ()
- Let Skview = Self.view as Skview
- if (Skview.scene = = nil) {
- Skview.showsfps=true
- Skview.showsnodecount=true
- Let scene=gamescene (size:skView.bounds.size) //Create Scene
- Scene.scalemode=skscenescalemode.fill
- Skview.presentscene (Scene)//show Scene
- }
- }
(2 Open the Gamescene.swift file, delete the code in Touchesbegan (Touches:nsset, withevent event:uievent), write code in it, Implement the touch screen display newgamescene scene function, the code is as follows:
- Override Func Touchesbegan (Touches:nsset, withevent event:uievent) {
- var nextscene=newgamescene (size:self.size) //Create Scene
- Self.view?. Presentscene (Nextscene) //Show Scene
- }
When you run the program, you see the effect shown in 2.23.
Figure 2.23 Running effect
the Sprite kitScenes with transition animations
Using a simple scene switch can make the game look very stiff. So the game developers in order to solve this shortcoming, during the switching process for the game to add a transition effect, make scene and scene switch more comfortable. These transition effects include fading, horizontal closing, and so on, as shown in table 2-3.
Table 2-3 Transition Animations
If you want to use these transitions for a scene when you switch to another scene, you need to use the Presentscene (_ Scene:skscene?,? transition transition:sktransition?) method, which has the following grammatical form:
- Func Presentscene (_ scene:skscene?,? transition transition:sktransition?)
Where _ scene represents a new scene, transition represents the transition animation effect used.
"Example 2-8" The following will be implemented on the basis of example 2-7 in the scene and scene switching, adding an old scene from the middle to the sides open, the new scene from the rear to the screen near the transition animation. The specific steps are as follows:
Open the Gamescene.swift file, delete the code in Touchesbegan (Touches:nsset, withevent event:uievent), write code in it, implement the function of the touch screen display newgamescene scene , the code is as follows:
- Override Func Touchesbegan (Touches:nsset, withevent event:uievent) {
- var nextscene=newgamescene (size:self.size)
- var doors=sktransition.doorwaywithduration (0.5)
- Self.view?. Presentscene (Nextscene, transition:doors) //transition
- }
When you run the program, you see the effect shown in 2.24.
Figure 2.24 Running effect
In addition to the transition animation can be seen in Figure 2.24, you can also use other effects, such as the following code to use the Fliphorizontalwithduration transition animation, that is, the horizontal axis vertically flipped transition animation, the code is as follows:
- Override Func Touchesbegan (Touches:nsset, withevent event:uievent) {
- var nextscene=newgamescene (size:self.size)
- var doors=sktransition.fliphorizontalwithduration (2)
- Self.view?. Presentscene (Nextscene, transition:doors) //transition
- }
When you run the program, you see the effect shown in 2.25.
Figure 2.25 Running Effect
This article is selected from:iOS Game Frame Sprite Kit Basic Tutorial--swift version of the University PA Internal information, reproduced please indicate the source, respect the technology respect the IT person!
Switch the scene of the IOS Sprite Kit tutorial