Switch the scene of the IOS Sprite Kit tutorial

Source: Internet
Author: User

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 points represent 4 scenes, respectively, 2.20 is shown.

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, 2.21 is shown.

Figure 2.21 first-off scene

when the player chooses the second pass, the scene of the selection level jumps to the scene in the second pass, 2.22 is shown.

Figure 2.22 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 when switching (_ Scene: skscene?) method.

" example 2-7 "The following will be the project 2-1 gamescene switch to newgamescene

( 1 ) Open Gameviewcontroller.swift files, in viewdidload () method, the first scene to display after running a program 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 a scene

  • Scene.scalemode=skscenescalemode.fill

  • skview.presentscene (Scene) Show Scene

  • }

  • }

( 2 ) Open Gamescene.swift files, deleting Touchesbegan (Touches:nsset, withevent event:uievent) code in which you write code to implement a touch screen display Newgamescene the function of the scene, the code is as follows:

  • Override Func Touchesbegan (Touches:nsset, withevent event:uievent) {

  • & nbsp    var nextscene=newgamescene (size:self.size)                                                                 // create scene

  • Self.view?. Presentscene (nextscene) // Show Scene

  • }

When you run the program, you see 2.23 The effect shown.

Figure 2.23 Run 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 ?)

    • Func Presentscene (_ Scene: skscene?,? transition Transition: sktransition?)

among them, _ Scene represents a new scene, transition represents the transition animation effect that is used.

" example 2-8 "The following will be in the example 2-7 Based on the implementation of 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 following are the steps:

open gamescene.swift file, delete touchesbegan (Touches:nsset, withevent event:uievent) newgamescene The function of the scene, the code is as follows:

    • Override func Touchesbegan (Touches:nsset, withevent event:uievent) {

    •    & Nbsp;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 2.24 The effect shown.

Figure 2.24 Run effect

Transition animation can be used in addition to the figure 2.24 fliphorizontalwithduration

    • 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 2.25 The effect shown.

Figure 2.25 Run 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.