gpt4 book ai didi

swift - CMMotionManager 倾斜以左右移动节点 Swift 4

转载 作者:搜寻专家 更新时间:2023-11-01 07:08:41 28 4
gpt4 key购买 nike

我有一个名为 Balloon 的单独 SKNode 类,如下所示。

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")
}
}

然后我在我的 GameScene 中使用这个类,如下所示。

    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)
}
}

基本上我基本上想做的是在纵向模式下使用应用程序并向左或向右平铺以移动节点,无论我倾斜我的 iPhone。以下是我在 Objective C 时代使用的内容,但我不确定这些天 Swift 4 的情况如何,或者是否有更好的东西。希望我提供了足够的信息以获得一些提示和任何帮助!

#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);
} ];
}

最佳答案

没关系,我明白了。

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)
}
}

关于swift - CMMotionManager 倾斜以左右移动节点 Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46353949/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com