在SpriteKit中使用UIInterpolatingMotionEffect吗? [英] Use UIInterpolatingMotionEffect in SpriteKit?

查看:115
本文介绍了在SpriteKit中使用UIInterpolatingMotionEffect吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIInterpolatingMotionEffect旨在与UIView一起使用,但是有可能在SpriteKit的节点上使用它吗?

UIInterpolatingMotionEffect is intended for use with UIView, but is there a possibility to use it within SpriteKit on a node ?

如果不是,那么有人是否有想法以另一种方式实现这种效果.

If not, does someone has an idea to realize the effect another way.

预先感谢

推荐答案

在Sprite Kit中创建视差效果可以通过创建一组SKNode图层,向这些图层添加sprite,然后将每个图层移动一个来实现.当用户倾斜设备时,其数量会有所不同.您可以选择缩放图层以给出深度的错觉.

Creating a parallax effect in Sprite Kit can be achieved by creating a set of SKNode layers, adding sprites to the layers, and then moving each layer by a different amount when the user tilts the device. You can optionally scale the layers to give the illusion of depth.

这是Swift中的实现.由于它使用加速度计,因此只能在设备(而不是模拟器)上使用.

Here's an implementation in Swift. Since it uses the accelerometer, this will only work on a device (not the Simulator).

import SpriteKit
import CoreMotion

class GameScene: SKScene {

    let squareSize = CGFloat(50.0)
    let manager = CMMotionManager()
    let maxDistance = CGFloat(40.0)

    override func didMoveToView(view: SKView) {

        let colors = [SKColor.redColor(), SKColor.greenColor(), SKColor.blueColor()]

        for i in 0..<colors.count {
            let node = createLayer(colors[i], depth: CGFloat(colors.count-i), screenSize: view.frame.size)
            addChild(node)
        }

        var tiltX:CGFloat = 0.0
        var alpha:CGFloat = 0.25

        // Define block to handle accelerometer updates
        if manager.accelerometerAvailable {
            manager.accelerometerUpdateInterval = 0.1
            manager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()) {
                [weak self] (data: CMAccelerometerData!, error: NSError!) in
                // Low-pass filter to smooth the measurements
                tiltX = tiltX * (1-alpha) + CGFloat(data.acceleration.x) * alpha
                for (index, node) in enumerate(self!.children as! [SKNode]) {
                    // Move each layer by a different amount to produce parallax effect
                    let deltaX = tiltX*CGFloat(index+1)*self!.maxDistance
                    node.position = CGPoint(x:self!.size.width/2.0+deltaX, y:self!.size.height/2.0)
                }
            }
        }
    }

    func createLayer(color:SKColor, depth:CGFloat, screenSize:CGSize) -> SKNode {
        let scale = 1.0/depth
        let node = SKNode()
        // Scale layer to create depth effect
        node.setScale(scale)
        node.zPosition = -depth
        node.position = CGPoint(x:screenSize.width/2.0, y:screenSize.height/2.0)
        let size = CGSize(width: squareSize, height: squareSize)
        // Create squares at random positions
        for i in 1...10 {
            let square = SKSpriteNode(color: color, size: size)
            let x = CGFloat(arc4random_uniform(UInt32(screenSize.width))) - screenSize.width / 2.0
            let y = CGFloat(arc4random_uniform(UInt32(screenSize.height))) - screenSize.height / 2.0
            square.position = CGPoint(x:x, y:y)
            node.addChild(square)
        }
        return node
     }
 }

这篇关于在SpriteKit中使用UIInterpolatingMotionEffect吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆