用手指手势移动精灵
Moving sprite with finger gesture
每当我在设备屏幕上的任何地方移动手指时,我都在尝试移动精灵(红点)。下面的代码似乎只在一个小的不可见圆圈内起作用,而不是在屏幕的整个区域内起作用,这意味着精灵似乎遇到了某种墙并且没有继续前进。谁能知道为什么?
import SpriteKit
import GameplayKit
var positionX = CGFloat()
var positionY = CGFloat()
var newPositionX = CGFloat()
var newPositionY = CGFloat()
var redDotPosition = CGPoint()
var oldPosition = CGPoint()
var newPosition = CGPoint()
class GameScene: SKScene {
var redDot : SKSpriteNode?
var entities = [GKEntity]()
var graphs = [String : GKGraph]()
private var lastUpdateTime : TimeInterval = 0
private var label : SKLabelNode?
private var spinnyNode : SKShapeNode?
override func sceneDidLoad() {
redDot = childNode(withName: "redDot") as? SKSpriteNode
redDot?.texture = SKTexture(imageNamed: "redDot")
redDot?.position = CGPoint(x: frame.midX , y: frame.midX)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchMoved(toPoint: t.location(in: self))
oldPosition = t.previousLocation(in: self)
let oldPositionX = oldPosition.x
let oldPositionY = oldPosition.y
newPositionX = t.location(in: self).x
newPositionY = t.location(in: self).y
deltaX = newPositionX - oldPositionX
deltaY = newPositionY - oldPositionY
redDot?.position = CGPoint(x: redDot!.position.x + deltaX , y: redDot!.position.y + deltaY)
}
}
没关系!
问题在于我不小心将我的精灵锚定到场景中,因此它没有移动超过一定量。
每当我在设备屏幕上的任何地方移动手指时,我都在尝试移动精灵(红点)。下面的代码似乎只在一个小的不可见圆圈内起作用,而不是在屏幕的整个区域内起作用,这意味着精灵似乎遇到了某种墙并且没有继续前进。谁能知道为什么?
import SpriteKit
import GameplayKit
var positionX = CGFloat()
var positionY = CGFloat()
var newPositionX = CGFloat()
var newPositionY = CGFloat()
var redDotPosition = CGPoint()
var oldPosition = CGPoint()
var newPosition = CGPoint()
class GameScene: SKScene {
var redDot : SKSpriteNode?
var entities = [GKEntity]()
var graphs = [String : GKGraph]()
private var lastUpdateTime : TimeInterval = 0
private var label : SKLabelNode?
private var spinnyNode : SKShapeNode?
override func sceneDidLoad() {
redDot = childNode(withName: "redDot") as? SKSpriteNode
redDot?.texture = SKTexture(imageNamed: "redDot")
redDot?.position = CGPoint(x: frame.midX , y: frame.midX)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchMoved(toPoint: t.location(in: self))
oldPosition = t.previousLocation(in: self)
let oldPositionX = oldPosition.x
let oldPositionY = oldPosition.y
newPositionX = t.location(in: self).x
newPositionY = t.location(in: self).y
deltaX = newPositionX - oldPositionX
deltaY = newPositionY - oldPositionY
redDot?.position = CGPoint(x: redDot!.position.x + deltaX , y: redDot!.position.y + deltaY)
}
}
没关系!
问题在于我不小心将我的精灵锚定到场景中,因此它没有移动超过一定量。