我正在使用Swift,SpriteKit和Xcode 6,我的屏幕上有一个圆形节点,可以用手指更改节点的位置,但是我想对该节点实现摩擦效果,但是现在不了怎么做,这是我的代码:
class PlayScene: SKScene
{
let firstCircle = SKSpriteNode(imageNamed: "Circle")
// theses 3 variables allows me to move the node according to my finger, but my finger don't have to touch the node
var departCircleLocation = CGPoint()
var departTouchLocation = CGPoint()
var currentTouchLocation = CGPoint()
override func didMoveToView(view: SKView)
{
addChild(firstCircle)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
{
for touch: AnyObject in touches
{
departTouchLocation = touch.locationInNode(self)
}
departCircleLocation = firstCircle.position
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent)
{
for touch: AnyObject in touches
{
currentTouchLocation = touch.locationInNode(self)
}
moveCircle()
}
func moveCircle()
{
firstCircle.position.x = departCircleLocation.x - departTouchLocation.x + currentTouchLocation.x
firstCircle.position.y = departCircleLocation.y - departTouchLocation.y + currentTouchLocation.y
}
}
我试图把它放到我的代码中,但是没有用:
firstCircle.physicsBody = SKPhysicsBody(circleOfRadius: 7)
firstCircle.physicsBody?.affectedByGravity = false
firstCircle.physicsBody?.friction = 0.4
我不需要重力,因为游戏是从上方观看的
有人可以向我解释如何对这个节点进行摩擦吗?例如,我希望当我以一定速度向上滑动并且松开手指时,节点会继续上升一点时间,但是如果我再次将手指放在屏幕上,则摩擦会立即停止,例如:
如果我将手指放在节点下方50像素,则不会执行任何操作,但是如果我将手指(节点下方为50像素)移动到节点下方80像素,则节点将从30像素向下移动,这适用于任何x和y方向,我可以在不触摸节点的情况下移动节点,但是如果我用手指,它将沿着路径移动,例如,如果我很快将手指从30像素向下移动并且松开手指,则节点将继续前进由于摩擦力而向下滑动,但手指不再出现在屏幕上(这种效果看起来像当我们在iphone上滚动网站时,由于滑动的速度而释放手指时会产生一点摩擦)但我不能使用UISwipeGesture,因为它只能检测上,右,下和左方向,而不是像我想要的每个方向。我希望你能理解我在说什么
这是一个如何基于触摸移动精灵的示例。这种方法利用velocity
物理物体的属性来移动精灵,因此它将与场景中的其他物理物体适当地交互,并受到阻尼,摩擦等的影响。
首先,定义比例和阻尼特性。该scale
控制的速率圆朝向触摸的位置移动。较大的值将使圆以更快的速度移动。该damping
属性用于在触摸结束时减慢精灵速度。较小的值将以更快的速度减慢精灵。
let scale:CGFloat = 2.0
let damping:CGFloat = 0.98
var point:CGPoint?
开始触摸时保存位置。子画面将移向该位置。
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
if let touch = touches.anyObject() as UITouch? {
let location = touch.locationInNode(self)
point = location
}
}
触摸移动时更新位置
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
if let touch = touches.anyObject() as UITouch? {
let location = touch.locationInNode(self)
point = location
}
}
触摸结束后重置触摸位置
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
point = nil
}
将精灵移至指定位置
func moveNodeToPoint(sprite:SKSpriteNode, point:CGPoint) {
var dx:CGFloat = (point.x - sprite.position.x) * scale
var dy:CGFloat = (point.y - sprite.position.y) * scale
sprite.physicsBody?.velocity = CGVectorMake(dx, dy)
}
在渲染每一帧之前调用此方法。如果触摸处于活动状态,它将调用该功能以更新精灵的位置,否则会随着时间的推移而减慢精灵的速度
override func update(currentTime: CFTimeInterval) {
if (point != nil) {
moveNodeToPoint(firstCircle, point: point!)
}
else {
// This will slow the circle over time when after the touch ends
let dx = firstCircle.physicsBody!.velocity.dx * damping
let dy = firstCircle.physicsBody!.velocity.dy * damping
firstCircle.physicsBody!.velocity = CGVectorMake(dx, dy)
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句