设为首页 加入收藏

TOP

iFIERO - (二)宇宙大战 Space Battle -- SpriteKit 无限循环背景Endless、SpriteKit物理碰撞、CoreMotion加速计(二)
2019-09-03 02:41:13 】 浏览:531
Tags:iFIERO 宇宙 大战 Space Battle SpriteKit 无限 循环 背景 Endless 物理 碰撞 CoreMotion 加速
oringAcceleration() /// 开启手机加速计感应

对于停止加速计,合适的地方是一个类型的deinit方法:

stopMonitoringAcceleration()

获取加速计:

func updateAccleration(){
        
        motionManager.accelerometerUpdateInterval = 0.2 /// 感应时间 motionManager.startAccelerometerUpdates(to: OperationQueue.current!) { (data, error) in ///1. 取得data数据; guard let accelerometerData = data else { return } ///2. 取得加速度 let acceleration = accelerometerData.acceleration ///3. 更新XAcceleration的值 let filterFactor:CGFloat = 0.75 //fiter的加入是很有必要的,这样处理一下得到的数据更加平滑 self.xAcceleration = CGFloat(acceleration.x) * filterFactor + self.xAcceleration * (1 - filterFactor) self.yAcceleration = CGFloat(acceleration.y) * filterFactor + self.yAcceleration * (1 - filterFactor) } } 
 
SpriteKit框架渲染每一帧的周期Loop流程原理图

接着,我们在SpriteKit框架渲染每一帧的周期Loop中的didSimulatePhysics调用物理特性让飞船改变位置,代码如下:

//MARK: - 手机加速度计感应,在SpriteKit框架渲染每一帧的周期Loop中的didSimulatePhysics调用物理特性让飞船改变位置 override func didSimulatePhysics() { /// 取得xAcceleration的加速度 /// 速度乘以时间得到应该移动的距离,更新现在飞船应该在的位置 self.playerNode.position.x += self.xAcceleration * 50 /// * 50表示时间 self.playerNode.position.y += self.yAcceleration * 50 // 让player => SpaceShip在屏幕之间滑动 x // X-Axis X轴水平方向 最小值 // 如果player的x-axis最小值 < player飞船的size.with 1/2 设飞船的最小值为 size.with/2 if self.playerNode.position.x < -self.frame.size.width / 2 + self.playerNode.size.width { self.playerNode.position.x = -self.frame.size.width / 2 + self.playerNode.size.width } // 最大值 if self.playerNode.position.x > self.frame.size.width / 2 - self.playerNode.size.width { self.playerNode.position.x = self.frame.size.width / 2 - self.playerNode.size.width } // Y-Axis Y轴方向 if self.playerNode.position.y > -self.playerNode.size.height { self.playerNode.position.y = -self.playerNode.size.height } if self.playerNode.position.y < -self.frame.size.height / 2 + self.playerNode.size.height { self.playerNode.position.y = -self.frame.size.height / 2 + self.playerNode.size.height } } 

最终,didSimulatePhysics()将会被调用来更新飞船的位置。

用真机跑一下你的程序吧。你现在已经可以通过倾斜设备来调用加速计来让飞船运动啦!

二、如何创建无限循环Endless的星空背景

 
ENDLESS无限循环背景

 

红色框中的节点bgNode1,SpriteNode的名称Name BG1 位置为Position(0,0)

bgNode1 = childNode(withName: "BG1") as! SKSpriteNode

黄色框为的节点bgNode2, SpriteNode的名称Name BG2 位置为Position(0,2048)

bgNode2 = childNode(withName: "BG2") as! SKSpriteNode

二个SpriteNode同时向下移动

func updateBackground(deltaTime:TimeInterval){ // 下移 bgNode1.position.y -= CGFloat(deltaTime * 300) bgNode2.position.y -= CGFloat(deltaTime * 300) } override func update(_ currentTime: TimeInterval) { // 每Frame的时间差 if lastUpdateTimeInterval == 0 { lastUpdateTimeInterval = currentTime } deltaTime = currentTime - lastUpdateTimeInterval lastUpdateTimeInterval = currentTime // endless 无限循环星空背景 updateBackground(deltaTime: deltaTime) } 
 
二个SpriteNode同时向下移动

当红色框BG1的位置bgNode1.position.y < bgNode1.size.height 的高度(即屏幕的height),把bgNode1移到之间黄色框的位置

/// 第一个背景node
if bgNode1.position.y < -bgNode1.size.height {
bgNode1.position.y = bgNode2.position.y + bgNode2.size.height
}

 
红色框bgNode2.position.y = 2048,黄色框bgNode2.position.y = 0

 

此时黄色框bgNode2.position.y = 0 位于屏幕的正中央
红色框bgNode1.position.y = 2048 取代之间花黄色框的位置,同理,黄色框再次向下移动时,当黄色框BG2的位置bgNode2.position.y < bgNode2.size.height 的高度(即屏

首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇swift为什么不是do while? 下一篇Swift 在UIWindow 上添加并移除 v..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目