SpriteKit工程简读

前端之家收集整理的这篇文章主要介绍了SpriteKit工程简读前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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


AppDelegate.swift:应用入口文件

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

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

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

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

Assets.xcassets : 游戏资源目录

LaunchScreen.storyboard : 闪屏页;

info.plist : 应用信息文件

2,代码简读

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

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

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

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

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

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

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

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

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

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

  1. public func touchesBegan(touches: Set<UITouch>,withEvent event: UIEvent?)
  2. public func touchesMoved(touches: Set<UITouch>,withEvent event: UIEvent?)
  3. public func touchesEnded(touches: Set<UITouch>,withEvent event: UIEvent?)
  4. public func touchesCancelled(touches: Set<UITouch>?,withEvent event: UIEvent?)

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

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


3,扩展

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

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

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

  1. var allPlane:[SKSpriteNode] = []

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

  1. allPlane.append(sprite)

3)重写touchesMoved函数
  1. override func touchesMoved(touches: Set<UITouch>,withEvent event: UIEvent?) {
  2. for touch in touches {
  3. let location = touch.locationInNode(self)
  4. //寻找在location的飞机精灵
  5. var index = 0
  6. for plane in allPlane {
  7. if (plane.containsPoint(location)){
  8. print("Find sprite in location")
  9. allPlane.removeAtIndex(index)
  10. plane.removeFromParent()
  11. index--
  12. }
  13. index++
  14. }
  15. }
  16. }

4,小结

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

猜你在找的Swift相关文章