ios8升级ios12教程_iOS SpriteKit教程

ios8升级ios12教程

In this tutorial, we’ll discuss SpriteKit and build a Hello World iOS Application out of it using Swift.

在本教程中,我们将讨论SpriteKit,并使用Swift从中构建一个Hello World iOS应用程序。

入门 (Getting Started)

SpriteKit is a framework that’s used to create high-performance 2D games while rendering graphics and animation textures.
SpriteKit has an optimized animation system and advances physics effects that are ideal to create engaging 2D games.

SpriteKit是一个框架,用于在渲染图形和动画纹理的同时创建高性能2D游戏。
SpriteKit具有优化的动画系统,并增强了物理效果,非常适合制作引人入胜的2D游戏。

To get started with the SpriteKit Game Project launch your XCode and choose the Game template as shown below:

ios spritekit game template

要开始使用SpriteKit Game Project,请启动XCode并选择Game模板,如下所示:

On the next screen make sure that you choose SpriteKit. Once XCode builds your project, you’ll see many files generated in your Project panel. These include GameScene.swift, .sks files and the GameViewController.
The code can look intimidating at first. Don’t worry we’ll start from the SpriteKit basics before reaching till there.
Build and run the application on your simulator or device.
Following is the output:

在下一个屏幕上,确保选择SpriteKit。 XCode构建项目后,您将在“项目”面板中看到许多生成的文件。 其中包括GameScene.swift,.sks文件和GameViewController。
该代码乍一看可能令人生畏。 别担心,在到达那里之前,我们将从SpriteKit基础开始。
在模拟器或设备上生成并运行该应用程序。
以下是输出:

That’s the sample Hello World SpriteKit application created. But that was all XCode magic. Let’s start from ground zero and cover the important concepts and terms and build our version of SpriteKit Hello World Application.

那就是创建的示例Hello World SpriteKit应用程序。 但这就是XCode的全部魔力。 让我们从零开始,介绍重要的概念和术语,并构建我们的SpriteKit Hello World应用程序版本。

SpriteKit的基础 (Basics of SpriteKit)

Sprites are nothing but animated textured images.
SpriteKit consists of the following important classes:

精灵不过是动画纹理图像。
SpriteKit包含以下重要类:

  • SKView: This is a subclass of the UIView. SKView is the view in which all of the SpriteKit contents are added and displayed on the screen.

    SKView :这是UIView的子类。 SKView是其中添加了所有SpriteKit内容并在屏幕上显示的视图。
  • SKNode: This is the building block of SpriteKit. It is just an empty node. No visual elements. On a SKNode we can add child nodes such as SKSpriteNode. These nodes can be visual elements or a scene.

    SKNode :这是SpriteKit的构建块。 它只是一个空节点。 没有视觉元素。 在SKNode上,我们可以添加子节点,例如SKSpriteNode。 这些节点可以是视觉元素或场景。
  • SKScene: An SKScene is a subclass of the SKNode. It represents a single scene from the game. SKScene acts as a root node of the SKNode. SKScene provides the content to the SKView. You just need to call the function presentScene over the SKView and pass the scene instance.

    SKScene :SKScene是SKNode的子类。 它代表了游戏中的一个场景。 SKScene充当SKNode的根节点。 SKScene将内容提供给SKView。 您只需要在presentScene上调用函数presentScene并传递场景实例。
  • SKAction: An SKAction is used to set actions such as animations/movements on SKNodes.

    SKAction :SKAction用于设置动作,例如SKNode上的动画/动作。
  • SKPhysicsBody: This class adds the physics components to the SKNodes. Components such as gravity, collision are commonly used in games.

    SKPhysicsBody :此类将物理组件添加到SKNodes。 游戏中通常使用重力,碰撞等组件。
SKScene origin lies at the bottom left corner.
SKScene的原点位于左下角。

Now create a new XCode Project and choose single view application. We’ll build the SpriteKit project from the scratch.

现在创建一个新的XCode项目并选择单视图应用程序。 我们将从头开始构建SpriteKit项目。

Goto your MainStoryboard and set the custom class type as SKView.

转到MainStoryboard并将自定义类类型设置为SKView。

Right click on the project folder and select new file of the type SpriteKitScene as shown below:

ios spritekit scene file

We’ve named the file HelloScene.sks.
This is the layout editor of it:

右键单击项目文件夹,然后选择SpriteKitScene类型的新文件,如下所示:

ios spritekit场景文件

我们已将文件命名为HelloScene.sks。
这是它的布局编辑器:

Now we need to create a HelloScene.swift file to connect with the above SK scene file.
Create a new file of the type Cocoa Touch class and choose the following class type as the parent:

现在,我们需要创建一个HelloScene.swift文件,以与上述SK场景文件连接。
创建类型为Cocoa Touch类的新文件,然后选择以下类类型作为父类:

The HelloScene.swift file looks like this:

ios spritekit skscene swift code

HelloScene.swift文件如下所示:

Ensure that you’ve imported SpriteKit module in it.

确保已在其中导入SpriteKit模块。

Now its time to connect this scene file in the scene layout editor:

现在是时候在场景布局编辑器中连接该场景文件了:

添加精灵 (Adding a Sprite)

To add a sprite from the layout editor we do:

ios skscene add sprite

要从布局编辑器添加一个精灵,我们要做:

We can add set any file path to images present in the Assets folder on this Color Sprite.
For now, let’s keep it blank.

我们可以将任何文件路径添加到此Color Sprite上Assets文件夹中的图像。
现在,让我们保持空白。

Adding a Sprite programmatically.
To do so in the HelloScene.swift file, we need to add the child node inside a didMove() function that’s overridden.

以编程方式添加Sprite。
为此,在HelloScene.swift文件中,我们需要在被覆盖的didMove()函数内添加子节点。

import SpriteKit

class HelloScene: SKScene {

    override func didMove(to view: SKView) {
        let label = SKLabelNode()
        label.text = "Hello World!"
        label.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
        label.fontSize = 30
        label.fontColor = SKColor.red
        self.addChild(label)
        
    }
}

We’ve adding a SKLabelNode which displays a Sprite with a text.
The SKLabelNode must be assigned a position else it’ll be placed at the bottom left of the screen.
We’ve centered it here and set the fonts and sizes.
Finally we add the SKLabelNode to the SKScene using addChild()

我们添加了一个SKLabelNode,它显示带有文本的Sprite。
必须为SKLabelNode分配一个位置,否则将其放置在屏幕的左下方。
我们将其定位在此处并设置字体和大小。
最后,我们使用addChild()将SKLabelNode添加到SKScene

从我们的ViewController运行SKScene (Running the SKScene from our ViewController)

We need to now add the SKScene to the ViewController.swift file. In it, we’ll present the scene to the SKView:

现在,我们需要将SKScene添加到ViewController.swift文件中。 在其中,我们将场景呈现给SKView:

import UIKit
import SpriteKit
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        if let view = self.view as? SKView
        {
            if let scene = SKScene(fileNamed: "HelloScene")
            {
                scene.scaleMode = .aspectFill
                view.presentScene(scene)
            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

We’ve set the scale mode to fill the screen.

我们已经设置了缩放模式以填充屏幕。

The output of the above application is:

ios spritekit hello world output

上面的应用程序的输出是:

This brings an end to this basic SpriteKit introductory tutorial.
You can download the project from the link below:

这结束了此基本的SpriteKit入门教程。
您可以从下面的链接下载项目:

翻译自: https://www.journaldev.com/21623/ios-spritekit-tutorial

ios8升级ios12教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值