Since I am working in spritekit, I decided not to use UIScrollview
because from what I could tell it is difficult to implement. Instead, based on research online I decided to create an SKNode
and add all of my sprites as children of the SKNode
. From there, in the touchesMoved
function, I moved my SKNode
to replicate the UIScrollview
. This part works perfect. The SKNode
with all of the sprites moved wherever I scroll simulating the action, however, if I print out -> print(spriteA.position)
, the value doesn't change from it's initial position. When i print out print(SKNode.position)
, the value DOES change from it's initial position.
blueBottle = SKSpriteNode(texture: SKTexture(imageNamed: "Bottle"), size: CGSize(width: 0.065*size.height, height: 0.2*size.height))
blueBottle.position = CGPoint(x: 0.8*size.width, y: topShelf.position.y + 0.5*topShelf.size.height + 0.5*blueBottle.size.height)
blueBottle.name = "Play"
blueBottle.zPosition = Layer.ui.rawValue
moveableNode.addChild(blueBottle)
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.location(in: self)
touchPoint = location
secondLocation = location
}
moveableNode.position.y = touchPoint.y - (firstLocation.y - moveableNodePos.y)
print(moveableNode.position.y)
print(bluebottle.position.y)
}
This is pretty odd to me, as the sprite is successfully moving, however the position doesn't update... The only thing I can think of is that the sprite is not attached to the SKNode
properly?
it is working properly and the results are as expected.
position is the position within the parent. so in your case the sprites position within the SKNode which never changes.
Think of it this way if I asked you to pack a cooler, and said to pack Cokes on the left of the cooler and Coronas on the right. When we move that cooler to the camp ground the cokes still have the same position in the cooler. ;)
Edit
further following with my same example
if I want to find out the Coke's position in relation to the campground and not the cooler I would have to convert it.
using...
convert(_:to:)
or convert(_:from:)
From Apple...
Converting Between Coordinate Spaces
When working with the node tree, sometimes you need to convert a position from one coordinate space to another. For example, when specifying joints in the physics system, the joint positions are specified in scene coordinates. So, if you have those points in a local coordinate system, you need to convert them to the scene’s coordinate space.
The following code shows how to convert a node’s position into the scene coordinate system. The scene is asked to perform the conversion. Remember that a node’s position is specified in its parent’s coordinate system, so the code passes node.parent as the node to convert from. You could perform the same conversion in reverse by calling the convert(_:to:)
method.