如何使物体移动得更远



我正在尝试创建一个程序,在这个程序中,你按住屏幕的时间越长,对象就会移动得越高。我想在按住的时间越长,函数重复的时间就越长的地方进行,增加距离。然而,由于某种原因,它不起作用。

import SpriteKit
import GameplayKit
var calculateTimer = Timer()
var distance = 10
var choice = 1
class GameScene: SKScene, SKPhysicsContactDelegate {
var planet = SKNode()
override func didMove(to view: SKView) { // viewdidload
let planetTexture = SKTexture(imageNamed: "mercury.jpg")
planet = SKSpriteNode(texture: planetTexture)
planet.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
addChild(planet)
}
func calculateDistance() {
distance += 10
print(distance)
print(choice)
}
func touchDown(atPoint pos : CGPoint) {
calculateTimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.calculateDistance), userInfo: nil, repeats: true)
print("test")
}
func touchUp(atPoint pos : CGPoint) {
planet.physicsBody!.isDynamic = true
//planet.physicsBody?.velocity = (CGVector(dx: 0, dy: 0))
planet.physicsBody!.applyImpulse(CGVector(dx: 0, dy: distance))
}

override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}

这是我的密码。如果有人有任何建议,我们将不胜感激!

在touchDown中,设置一个标志以指示触摸已开始。在touchUp中,清除/移除标志。

在设置旗帜的同时,继续执行你的动作代码。

最新更新