3

I am trying to get my "word" to float across the screen; constant velocity, no impacts, no gravity, no friction. Everything works except the word slows down.

Code for creating word:

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
}

I call function like:

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

Any ideas why word slows down? Thanks.

hasan
  • 23,815
  • 10
  • 63
  • 101

2 Answers2

0

I worked with sprite a little bit. But, I am not an expert. Does it work for you to use an action instead of velocity as the following for example:

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

One thing to point out too. Why do you use applyImpulse. velocity should be enough. Did you try to remove it?

hasan
  • 23,815
  • 10
  • 63
  • 101
  • Thanks for the response. I will make your suggestions. I haven't tried an action because I wanted it to move across the screen and then reappear back where it started after it leaves the screen. It just seemed to me that a floating node that never slowed down would work. I'll try action next. As for the applyimpulse, just trying to solve my problem and adding any idea I could find. – Matthew Little Mar 19 '17 at 19:01
0

It looks like you are creating an object that doesn't need to be in the physics simulation. So you may want to reconsider simply dropping the physics related code and update the nodes position manually.

That being said if you wish this node to remain in the Physics simulation. You need to add the SKPhysicsBody to the SKSpriteNode before you start trying to modify properties. 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
cocojoe
  • 220
  • 1
  • 6
  • Thanks. That did it. I changed the psysicsbody parameters after adding to node, just like you suggested, and it worked perfectly. Much appreciated. – Matthew Little Mar 19 '17 at 19:06
  • Excellent @MatthewLittle if you can accept the answer that would be great. Thanks – cocojoe Mar 20 '17 at 10:32