SpriteKit中的摩擦 [英] Friction in SpriteKit

查看:124
本文介绍了SpriteKit中的摩擦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift,SpriteKit和Xcode 6, 我的屏幕上有一个圆形节点,可以用手指更改节点的位置,但是我想对该节点实现摩擦效果,但是现在不知道如何操作,这是我的代码:

I'm working with Swift, SpriteKit and Xcode 6, I have a circle node on my screen and I can change the node's position with my finger, but I would like to implement a friction effect to this node, but I don't now how to do so, here's my code :

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

我试图将其放入我的代码中,但是没有用:

I tried to put this into my code but it doesn't worked :

firstCircle.physicsBody = SKPhysicsBody(circleOfRadius: 7)
firstCircle.physicsBody?.affectedByGravity = false
firstCircle.physicsBody?.friction = 0.4

我不需要重力,因为游戏是从上方观看的

I don't want gravity because the game is viewed from above

有人可以向我解释如何对这个节点施加摩擦吗? 例如,我希望当我以一定速度向上滑动并且松开手指时,节点会继续上升一点时间,但是如果我再次将手指放在屏幕上,则摩擦会立即停止,例如:

Can someone explain to me how I could implement friction to this node ? I want for example that when I slide up with a certain speed and when I release my finger, the node continue to go up a little amount of time, but if I put my finger on the screen again, the friction stop immediately, for example:

如果我将手指放在该节点下50像素,它什么也不会做,但是如果我将手指(该节点下为50像素)移动到该节点下80像素,则该节点将从30像素下移,这可行在任何x和y方向上,我都可以移动节点而无需触摸它,但是如果我用手指,它将沿着路径移动,例如,如果我将手指从30像素下移得很快,然后松开手指,节点将由于摩擦力而继续下降,但我的手指不再在屏幕上(效果看起来像当我们在iphone上滚动网站时,由于速度的限制,放开手指时会有一点摩擦您的滑动),但我不能使用UISwipeGesture,因为它只能检测上,右,下和左方向,而不是像我想要的每个方向. 希望您能理解我在说什么

If I put my finger 50pixels under the node, it will do nothing, but if I move my finger (which is 50pixels under the node) to 80pixels under the node, the node will move from 30 pixels down, and this works in any x and y directions, I can move the node without touching it, but it will follow the path if my finger, now if for example I move my finger from 30 pixels down really quickly and the I release my finger, the node will continue to go down due to the friction force, but my finger is no longer on the screen (the effect looks like when we scroll a website on an iphone, there is a little bit of friction when you release your finger due to the speed of your swipe) but I can't use UISwipeGesture because it only detects up, right, down and left directions, not every directions like I want. I hope that you can understand what I'm saying

推荐答案

下面是一个如何基于触摸移动精灵的示例.这种方法利用物理物体的velocity属性移动精灵,因此它将与场景中的其他物理物体适当地交互,并受到阻尼,摩擦等的影响.

Here's an example of how to move a sprite based on touch. This approach uses the velocity property of the physics body to move the sprite, so it will interact appropriately with other physics bodies in the scene and be affected by damping, friction, etc.

首先,定义比例和阻尼属性. scale控制圆朝触摸位置移动的速率.较大的值将使圆以更快的速度移动. damping属性用于在触摸结束时减慢精灵速度.较小的值将以更快的速度减慢精灵速度.

First, define the scale and damping properties. The scale controls the rate at which the circle moves toward the location of the touch. A larger value will move the circle at a faster rate. The damping property is used to slow the sprite when the touch ends. A smaller value will slow the sprite at a faster rate.

let scale:CGFloat = 2.0
let damping:CGFloat = 0.98

var point:CGPoint?

触摸开始时保存位置.精灵会移到这个位置.

Save the location when a touch begins. The sprite will move toward this location.

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

触摸结束后重置触摸位置

Reset the touch location when the touch ends

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

在渲染每一帧之前调用它.如果触摸处于活动状态,它将调用该功能以更新精灵的位置,否则会随着时间的推移而降低精灵的速度

This is called before each frame is rendered. It calls the function that updates the location of the sprite if a touch is active, else it slows the sprite over time

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

这篇关于SpriteKit中的摩擦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆