微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpriteKit工程简读

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类,简单看下这两个类的继承关系,如下:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐