加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

SpriteKit工程简读

发布时间:2020-12-14 01:30:02 所属栏目:百科 来源:网络整理
导读:1,默认创建的工程目录结构如下: AppDelegate.swift:应用入口文件; GameScene.sks : 游戏情景可视化文件; GameScene.swift : 游戏场景控制类; GameViewController.swift : 游戏视图控制类; Main.storyboard : 应用程序故事板; Assets.xcassets : 游戏

1,默认创建的工程目录结构如下:


AppDelegate.swift:应用入口文件;

GameScene.sks : 游戏情景可视化文件;

GameScene.swift : 游戏场景控制类;

GameViewController.swift : 游戏视图控制类;

Main.storyboard : 应用程序故事板;

Assets.xcassets : 游戏资源目录

LaunchScreen.storyboard : 闪屏页;

info.plist : 应用信息文件;

2,代码简读

打开GameViewController.swift文件,在viewDidLoad函数中:

        if let scene = GameScene(fileNamed:"GameScene") {
             Configure the view.
            let skView = self.view as! SKView
            skView.showsFPS = true
            skView.showsNodeCount = true
            
            /* Sprite Kit applies additional optimizations to improve rendering performance */
            skView.ignoresSiblingOrder = true
            
            /* Set the scale mode to scale to fit the window */
            scene.scaleMode = .AspectFill
            
            skView.presentScene(scene)
        }
此处是使用的GameScene.sks创建的游戏场景,在此,暂且不用这种方法。

将上述代码去掉,添加如下代码:

        let skView = self.view as! SKView
        skView.showsNodeCount = true
        skView.showsFPS = true
        let scene = GameScene(size:skView.bounds.size)
        skView.presentScene(scene)
在运行程序,经过闪屏后进入我们的游戏场景:GameScene,打开文件GameScene.swift,查看下为什么点击一下屏幕就会出现一个飞机对象呢?

OK,有没有看到处理触摸的函数:touchesBegan:

override func touchesBegan(touches: Set<UITouch>,withEvent event: UIEvent?) {
       /* Called when a touch begins */
        
        for touch in touches {
            let location = touch.locationInNode(self)
            
            let sprite = SKSpriteNode(imageNamed:"Spaceship")
            sprite.xScale = 0.5
            sprite.yScale = 0.5
            sprite.position = location
            
            let action = SKAction.rotateByAngle(CGFloat(M_PI),duration:1)
            
            sprite.runAction(SKAction.repeatActionForever(action))
            
            allPlane.append(sprite)
            
            self.addChild(sprite)
        }
    }

这个函数的处理逻辑很简单:遍历每一个触摸开始事件,获取其点击位置,创建一个飞机精灵,放置到触摸点。

点、点、点...... 一屏幕的飞机了,怎么删除掉呢??

嗯,我们设计为手指在屏幕上移动上时把飞机删除掉,那么,怎么处理手指移动的事件呢??

touchesBegan函数声明在UIResponder类中,查看此类代码,支持的触摸操作:

    public func touchesBegan(touches: Set<UITouch>,withEvent event: UIEvent?)
    public func touchesMoved(touches: Set<UITouch>,withEvent event: UIEvent?)
    public func touchesEnded(touches: Set<UITouch>,withEvent event: UIEvent?)
    public func touchesCancelled(touches: Set<UITouch>?,withEvent event: UIEvent?)

那么,在GameScene.swift中重写touchesMoved函数,在响应此函数中做一个移除相关精灵的操作:
override func touchesMoved(touches: Set<UITouch>,withEvent event: UIEvent?) {
        self.removeAllChildren()
    }

Ok,这样在屏幕上移动时就会删除屏幕上所有的精灵,包括我们经典的"Hello world"。


3,扩展

那怎么才能做到只删除我们触摸移动到的精灵上呢?

我们要把添加到屏幕上的飞机精灵存储在一个集合中,再在处理触摸拖动时获取拖动到的位置,遍历飞机精灵集合监测此位置是否有飞机精灵,有则删之。

1)声明一个存储飞机集合的变量,用一个数组,在didMoveToview函数前添加:

     var allPlane:[SKSpriteNode] = []

2)在touchesBegan函数中,self.addChild(sprite)语句后添加一句:

     allPlane.append(sprite)

3)重写touchesMoved函数
override func touchesMoved(touches: Set<UITouch>,withEvent event: UIEvent?) {
        for touch in touches {
            let location = touch.locationInNode(self)
            //寻找在location的飞机精灵
            var index = 0
            for plane in allPlane {
                if (plane.containsPoint(location)){
                    print("Find sprite in location")
                    allPlane.removeAtIndex(index)
                    plane.removeFromParent()
                    index--
                }
                index++
            }
        }
    }

4,小结

此段代码中,主要涉及类SKSpriteNode、SKScene类,简单看下这两个类的继承关系,如下:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读