0

I want to make it so that a child sprite follows its corresponding parent sprite. The child sprite has a physics body which should not mess with the parent's physics body. I have tried to in the parent sprite's subclass to override these functions like this:

public override func run(_ action: SKAction) {
    super.run(action)
    physicsSprite.run(action)
}
public override func run(_ action: SKAction, withKey key: String) {
    super.run(action, withKey: key)
    physicsSprite.run(action, withKey: key)
}
public override func removeAllActions() {
    super.removeAllActions()
    physicsSprite.removeAllActions()
}

with no success however :(
I'am exclusively moving the parent sprite with SKActions. The result is that the sprites don't stick together.

physicsSprite.zPosition = 10
physicsSprite.physicsBody = SKPhysicsBody(circleOfRadius: playerInfo.width+2)
physicsSprite.physicsBody?.affectedByGravity = false
physicsSprite.physicsBody?.isDynamic = false
physicsSprite.physicsBody?.restitution = 0
physicsSprite.physicsBody?.collisionBitMask = 0
addChild(physicsSprite)

In this case physicsSprite is the childNode in a SKSpriteNode subclass. Thanks in advance

Arjun
  • 358
  • 5
  • 14
  • Is your `addChild(physicsSprite)` inside the definition of the parent sprite? How do you want it to follow and what is actually happening? Have you verified that physicsSprite really is a child of the sprite you want to be the parent? – Steve Ives Dec 27 '18 at 09:47

2 Answers2

0

You could try to use an SKConstraint object, that describes a mathematical constraint on a node's position or orientation.

From the docs:

Keep a node within a specified distance of another node or a point in the scene.

See https://developer.apple.com/documentation/spritekit/skconstraint

Giovanni Cappellotto
  • 4,597
  • 1
  • 30
  • 33
-1

Try this:

parentSprite.physicsBody?.categoryBitMask = 0
parentSprite.physicsBody?.collisionBitMask = 0

physicsSprite.physicsBody?.collisionBitMask = 1
physicsSprite.physicsBody?.categoryBitMask = 1

For more information check documentation

Alex2637
  • 1
  • 2
  • `categoryBitMask` should not be 0 - this means that the physics body has no category. You may as well not give it a physics body. You've also set the `physicsSprite` to only collide with itself. – Steve Ives Dec 27 '18 at 09:51
  • You can give it a value of 2 instead 0 – Alex2637 Dec 27 '18 at 10:07
  • Alex - you shouldn't assign categories arbitrarily. You should have a named list of categories in an enum or some other structure and assign the categoryBitMask via a name, not a number. – Steve Ives Dec 27 '18 at 10:42
  • The physiBody's bit masks are not the problem.The problem is getting the child node to follow the parent. – Steve Ives Dec 27 '18 at 10:44