ios - Swift Sprite-kit 移动对象
<p><p>我现在正在使用 coreMotion 来移动我的主角,但是在使用此代码之后我遇到了设备方向问题。</p>
<pre><code> manager.startAccelerometerUpdates()
let data = manager.accelerometerData
manager.accelerometerUpdateInterval = 0.0001
manager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()){
(data, error) in
self.plane.physicsBody!.applyForce(CGVectorMake(120.0 * CGFloat((data?.acceleration.x)!), 0))
}
</code></pre>
<p>所以我的新问题是,我怎样才能通过触摸屏幕左侧或触摸屏幕右侧来移动我的对象,以便角色平滑移动并且可以在 x 轴上向后和第四个没有有问题吗?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>每个 SKScene 都有 4 种触摸方法(开始、结束、移动、取消)可供您使用。</p>
<p>例如</p>
<pre><code>override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
if location.x < frame.midX { // left side
plane.physicsBody?.applyForce(CGVectorMake(-120.0, 0)) // move left
} else {
plane.physicsBody?.applyForce(CGVectorMake(120.0, 0)) // move right
}
}
}
</code></pre>
<p>将 120 调整为您喜欢的值,也取决于您的 Sprite 有多大。</p>
<p>希望对你有帮助</p></p>
<p style="font-size: 20px;">关于ios - Swift Sprite-kit 移动对象,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/39256670/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/39256670/
</a>
</p>
页:
[1]