swiftsprite-kitcmmotionmanager

CMMotionManager Tilt To Move Node Left & Right Swift 4


I have a separate SKNode class called Balloon which is below.

import Foundation
import SpriteKit

class Balloon: SKNode {
    init(image: SKSpriteNode) {
        super.init()

        let atlas = SKTextureAtlas(named: "balloons")
        var textureArray = [SKTexture]()

        self.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "red_balloon1"), size: CGSize(width: image.size.width, height: image.size.height))
        self.physicsBody?.affectedByGravity = false
        self.physicsBody?.categoryBitMask = balloonCategory
        self.physicsBody?.contactTestBitMask = floorCategory
        self.physicsBody?.collisionBitMask = floorCategory

        self.position = CGPoint(x: 0, y: -UIScreen.main.bounds.height / 2)
        self.zPosition = 1

        for i in 1...atlas.textureNames.count {
            let Name = "red_balloon\(i).png"
            textureArray.append(SKTexture(imageNamed: Name))
        }

        image.run(SKAction.repeatForever(SKAction.animate(with: textureArray, timePerFrame: 0.1, resize: false, restore: true)))

        self.addChild(image)
    }

    // Enables the ability to drag the ballon along the x axis
    func move(touchLocation: CGPoint) {
        if self.calculateAccumulatedFrame().contains(touchLocation) {
            self.position.y = touchLocation.y
            self.position.x = touchLocation.x
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

I'm then using this class in my GameScene like below.

    let balloon = Balloon(image: SKSpriteNode(imageNamed: "red_balloon1"))


    override func didMove(to view: SKView) {
            self.addChild(balloon)
    }

    override func update(_ currentTime: TimeInterval) {
            if let accelerometerData = motionManager.accelerometerData {
                balloon.physicsBody?.velocity = CGVector(dx: accelerometerData.acceleration.y * 50, dy: accelerometerData.acceleration.x * 50)
       }
}

Basically what I basically would like to do is use the application in portrait mode and tile left or right to move the node which ever way I tilted my iPhone. Below is what I used back in Objective C days but I'm not sure how it is with Swift 4 these days or if there's anything better. Hopefully I provided enough info for some tips and anything helps!

#define BALLOON 25

    -(void)startAccelerometerData {
        motionManager = [[CMMotionManager alloc] init];
        motionManager.accelerometerUpdateInterval = 1/60.0;
        [motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {

          valueX = accelerometerData.acceleration.x*26.0;

            newX = (balloon.center.x +valueX);
            if (newX > 320-BALLOON)
                newX = 320-BALLOON;
            else if (newX < 0+BALLOON)
                newX = 0+BALLOON;

            balloon.center = CGPointMake(newX, balloon.center.y);
    } ];
    }

Solution

  • Nevermind, I got it.

    if let accelerometerData = motionManager.accelerometerData {
                if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft {
                    balloon.physicsBody?.velocity = CGVector(dx: accelerometerData.acceleration.x * -500.0, dy: 0)
                } else {
                    balloon.physicsBody?.velocity = CGVector(dx: accelerometerData.acceleration.x * 500.0, dy: 0)
                }
            }