SKSpriteNode减速但不应该减速

时间:2017-03-18 11:45:09

标签: ios swift skspritenode skphysicsbody

我试图让我的“字”漂浮在屏幕上;恒定速度,无冲击,无重力,无摩擦。一切都有效,除了减慢这个词。

创建单词的代码:

func createWordNode (word: String, atPos: CGPoint) -> SKSpriteNode {

    let doneSize = CGSize(width: 50, height: 50)

    let wordSprite = SKSpriteNode()
    wordSprite.size = CGSize(width: doneSize.width * CGFloat(word.len()), height: doneSize.height)
    wordSprite.position = atPos
    wordSprite.blendMode = .replace
    wordSprite.zPosition = zlvlBG + 1

    let ltrs = Array(word.uppercased().characters)

    for i in 0 ... ltrs.count - 1 {

        let done = SKSpriteNode(imageNamed: "LetterTiles/Tile" + String(ltrs[i]) + ".png")
        done.size = doneSize
        done.position = CGPoint(x: doneSize.width * CGFloat(Double(i) - 1.5), y: 0)
        done.blendMode = .replace
        done.zPosition = zlvlBG + 1

        wordSprite.addChild(done)

    }

    wordSprite.physicsBody?.restitution = 1
    wordSprite.physicsBody?.friction = 0
    wordSprite.physicsBody?.linearDamping = 0
    wordSprite.physicsBody?.angularDamping = 0
    wordSprite.physicsBody?.mass = 2000

    wordSprite.physicsBody = SKPhysicsBody(rectangleOf: wordSprite.size)
    wordSprite.physicsBody?.collisionBitMask = 0
    wordSprite.physicsBody?.contactTestBitMask = 0
    wordSprite.physicsBody?.categoryBitMask = categoryWords
    wordSprite.physicsBody?.fieldBitMask = 0

    wordSprite.physicsBody?.isDynamic = true
    wordSprite.physicsBody?.affectedByGravity = false
    wordSprite.physicsBody?.allowsRotation = false

    var velocity = CGVector()
    velocity.dx = 100
    velocity.dy = 0
    wordSprite.physicsBody?.velocity = velocity
    wordSprite.physicsBody?.applyImpulse(velocity)

    wordSprite.name = "Word:" + word


    return wordSprite
}

我称之为函数:

addChild (createWordNode(word: "Done", atPos: CGPoint(x:-500, y:450)))

为什么单词减速的任何想法? 感谢。

2 个答案:

答案 0 :(得分:0)

我用精灵工作了一下。但是,我不是专家。您是否可以使用动作而不是速度,例如:

var moveRight = SKAction.moveTo(CGPointMake(400, 0), duration:2.0)
wordSprite.runAction(moveRight)

有一点需要指出。为什么使用applyImpulse。速度应该足够了。你有没有尝试删除它?

答案 1 :(得分:0)

看起来您正在创建一个不需要在物理模拟中的对象。因此,您可能需要重新考虑简单地删除与物理相关的代码并手动更新节点position

如果您希望此节点保留在物理模拟中,那就说了。在开始尝试修改属性之前,您需要将SKPhysicsBody添加到SKSpriteNode。 e.g。

wordSprite.physicsBody = SKPhysicsBody(rectangleOf: wordSprite.size)
wordSprite.physicsBody?.restitution = 1
wordSprite.physicsBody?.friction = 0
wordSprite.physicsBody?.linearDamping = 0
wordSprite.physicsBody?.angularDamping = 0
wordSprite.physicsBody?.mass = 2000